图像添加水印在php中有很多种办法可以实现,他这些功能都是基于php中的GD库的,如果没有开户GD库是不可以使用水印功能的.
imagecopymerge() 函数用于拷贝并合并图像的一部分,成功返回 TRUE,否则返回 FALSE .
Windows下开启PHP的GD库支持,找到php.ini,打开内容,找到:;extension=php_gd2.dll,把最前面的分号“;”去掉,再保存即可,如果本来就没有分号,那就是已经开启了.
基本的语法:bool imagecopymerge( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, int pct )
参数说明:参数 说明
dst_im 目标图像
src_im 被拷贝的源图像
dst_x 目标图像开始 x 坐标
dst_y 目标图像开始 y 坐标,x,y同为 0 则从左上角开始
src_x 拷贝图像开始 x 坐标
src_y 拷贝图像开始 y 坐标,x,y同为 0 则从左上角开始拷贝
src_w (从 src_x 开始)拷贝的宽度
src_h (从 src_y 开始)拷贝的高度
pct 图像合并程度,取值 0-100,当 pct=0 时,实际上什么也没做,反之完全合并.
当为 pct = 100 时对于调色板图像本函数和 imagecopy() 完全一样,知道了用法,要实现我们的功能就简单了,用下面的代码就可以轻松实现,代码如下:
- <?php
- header("Content-type: image/jpeg");
-
-
- $dst = "images/flower_1.jpg";
-
-
- $dst_im = imagecreatefromjpeg($dst);
- $dst_info = getimagesize($dst);
-
-
- $src = "images/logo.gif";
- $src_im = imagecreatefromgif($src);
- $src_info = getimagesize($src);
-
-
- $alpha = 30;
-
-
- imagecopymerge($dst_im,$src_im,$dst_info[0]-$src_info[0],$dst_info[1]-$src_info[1],0,0,$src_info[0],
- $src_info[1],$alpha);
-
-
- imagejpeg($dst_im);
- imagedestroy($dst_im);
- imagedestroy($src_im);
- ?>
新版本之后imagecopymerge函数几乎不使用了,我们可直接使用imagecopy来生成水印两个函数的功能是完全一样的,代码如下:
-
- $watermark =1;
- $watertype =2;
- $waterstring ='';
- $waterimg="z.png";
- $sFilePath ='aa.jpg';
- if($watermark==1)
- {
- $image_size = getimagesize($sFilePath);
- $water_size = getimagesize($waterimg);
- $iinfo=getimagesize($sFilePath,$iinfo);
- $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
- $white=imagecolorallocate($nimage,255,255,255);
- $black=imagecolorallocate($nimage,0,0,0);
- $red=imagecolorallocate($nimage,255,0,0);
- imagefill($nimage,0,0,$white);
- switch ($iinfo[2])
- {
- case 1:
- $simage =imagecreatefromgif($sFilePath);
- break;
- case 2:
- $simage =imagecreatefromjpeg($sFilePath);
- break;
- case 3:
- $simage =imagecreatefrompng($sFilePath);
- break;
-
-
-
- default:
- die("不支持的文件类型");
- exit;
- }
-
- imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
-
- switch($watertype)
- {
- case 1:
- imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
- break;
- case 2:
- $simage1 =imagecreatefrompng($waterimg);
- $x= $image_size[0]-$water_size[0];
- $y= $image_size[1]-$water_size[1];
- imagecopy($nimage,$simage1,$x,$y,0,0,240,65);
- imagedestroy($simage1);
- break;
- }
-
- switch ($iinfo[2])
- {
- case 1:
- imagegif($nimage, $sFilePath);
-
- break;
- case 2:
- imagejpeg($nimage, $sFilePath);
- break;
- case 3:
- imagepng($nimage, $sFilePath);
- break;
-
-
-
-
- }
-
-
- imagedestroy($nimage);
- imagedestroy($simage);
- }
一个更好的功能,可以生成缩略并且还可以给图片添加水印,想操作图片,先得把图片的大小,类型信息得到,水印:就是把指定的水印复制到目标上,并加透明效果,缩略图:就是把大图片复制到小尺寸画面上,代码如下:
- class ImageTool {
-
-
- public static function imageInfo($image) {
-
- if (!file_exists($image)) {
- return false;
- }
-
- $info = getimagesize($image);
-
- if ($info == false) {
- return false;
- }
-
-
- $img['width'] = $info[0];
- $img['height'] = $info[1];
- $img['ext'] = substr($info['mime'], strpos($info['mime'], '/') + 1);
-
- return $img;
- }
-
-
-
-
-
-
-
- public static function water($dst, $water, $save = NULL, $pos = 2, $alpha = 50) {
-
- if (!file_exists($dst) || !file_exists($water)) {
- return false;
- }
-
-
- $dinfo = self::imageInfo($dst);
- $winfo = self::imageInfo($water);
-
- if ($winfo['height'] > $dinfo['height'] || $winfo['width'] > $dinfo['width']) {
- return false;
- }
-
-
- $dfunc = 'imagecreatefrom' . $dinfo['ext'];
- $wfunc = 'imagecreatefrom' . $winfo['ext'];
-
- if (!function_exists($dfunc) || !function_exists($wfunc)) {
- return false;
- }
-
-
- $dim = $dfunc($dst);
-
- $wim = $wfunc($water);
-
-
-
- switch($pos) {
- case 0 :
-
- $posx = 0;
- $posy = 0;
- break;
-
- case 1 :
-
- $posx = $dinfo['width'] - $winfo['width'];
- $posy = 0;
- break;
-
- case 3 :
-
- $posx = 0;
- $posy = $dinfo['height'] - $winfo['height'];
- break;
-
- default :
- $posx = $dinfo['width'] - $winfo['width'];
- $posy = $dinfo['height'] - $winfo['height'];
- }
-
-
- imagecopymerge($dim, $wim, $posx, $posy, 0, 0, $winfo['width'], $winfo['height'], $alpha);
-
-
- if (!$save) {
- $save = $dst;
- unlink($dst);
-
- }
-
- $createfunc = 'image' . $dinfo['ext'];
- $createfunc($dim, $save);
-
- imagedestroy($dim);
- imagedestroy($wim);
-
- return true;
- }
-
-
-
-
-
- public static function thumb($dst, $save = NULL, $width = 200, $height = 200) {
-
- $dinfo = self::imageInfo($dst);
- if ($dinfo == false) {
- return false;
- }
-
-
- $calc = min($width / $dinfo['width'], $height / $dinfo['height']);
-
-
- $dfunc = 'imagecreatefrom' . $dinfo['ext'];
- $dim = $dfunc($dst);
-
-
- $tim = imagecreatetruecolor($width, $height);
-
-
- $white = imagecolorallocate($tim, 255, 255, 255);
-
-
- imagefill($tim, 0, 0, $white);
-
-
- $dwidth = (int)$dinfo['width'] * $calc;
- $dheight = (int)$dinfo['height'] * $calc;
-
- $paddingx = (int)($width - $dwidth) / 2;
- $paddingy = (int)($height - $dheight) / 2;
-
- imagecopyresampled($tim, $dim, $paddingx, $paddingy, 0, 0, $dwidth, $dheight, $dinfo['width'],
-
- $dinfo['height']);
-
-
- if (!$save) {
- $save = $dst;
- unlink($dst);
- }
-
- $createfunc = 'image' . $dinfo['ext'];
- $createfunc($tim, $save);
-
- imagedestroy($dim);
- imagedestroy($tim);
-
- return true;
-
- }
-
- }
|