为了防止自己辛苦制作的作品被别人窃取,经常给作品添加水印,以此保证作品的唯一性,那么该怎么给图片添加水印呢,如果作品尺寸过大,该如何处理呢,下面小编给
给图片添加水印,其实就是把原来的图片和水印添加在一起,下面小编把最近整理的资料分享给大家。
php对图片文件的操作主要是利用gd库扩展。当我们频繁利用php对图片进行操作时,会自然封装很多函数,否则会写太多重复的代码。当有很多对图片的相关函数的时候,我们可以考虑将这些函数也整理一下,,因而就有了封装成类的想法。
操作图片主要历经四个步骤:
第一步:打开图片
第二步:操作图片
第三步:输出图片
第四步:销毁图片
1,3,4三个步骤每次都要写,每次又都差不多。真正需要变通的只有操作图片的这一步骤了。操作图片又往往通过1或多个主要的gd函数来完成。
本文封装类里面的四种方法,文字水印(imagettftext()),图片水印(imagecopymerge()),图片压缩,图片剪切(imagecopyresampled()),其余的常用gd函数便不赘述。
直接上代码:
info=getimagesize($src);$this->type=image_type_to_extension($this->info['2'],false);$fun=imagecreatefrom{$this->type};$this->image=$fun($src); } /*** 文字水印* @param [type] $font 字体* @param [type] $content 内容* @param [type] $size 文字大小* @param [type] $col 文字颜色(四元数组)* @param array $location 位置* @param integer $angle 倾斜角度* @return [type]*/ public function fontmark($font,$content,$size,$col,$location,$angle=0){$col=imagecolorallocatealpha($this->image, $col['0'], $col['1'], $col['2'],$col['3']);imagettftext($this->image, $size, $angle, $location['0'], $location['1'], $col,$font,$content); } /*** 图片水印* @param [type] $imagemark 水印图片地址* @param [type] $dst 水印图片在原图片中的位置* @param [type] $pct 透明度* @return [type]*/ public function imagemark($imagemark,$dst,$pct){$info2=getimagesize($imagemark);$type=image_type_to_extension($info2['2'],false);$func2=imagecreatefrom.$type;$water=$func2($imagemark);imagecopymerge($this->image, $water, $dst[0], $dst[1], 0, 0, $info2['0'], $info2['1'], $pct);imagedestroy($water); } /*** 压缩图片* @param [type] $thumbsize 压缩图片大小* @return [type][description]*/ public function thumb($thumbsize){$imagethumb=imagecreatetruecolor($thumbsize[0], $thumbsize[1]);imagecopyresampled($imagethumb, $this->image, 0, 0, 0, 0, $thumbsize[0], $thumbsize[1], $this->info['0'], $this->info['1']);imagedestroy($this->image);$this->image=$imagethumb; } /** * 裁剪图片* @param [type] $cutsize 裁剪大小* @param [type] $location 裁剪位置* @return [type][description]*/public function cut($cutsize,$location){$imagecut=imagecreatetruecolor($cutsize[0],$cutsize[1]);imagecopyresampled($imagecut, $this->image, 0, 0, $location[0], $location[1],$cutsize[0],$cutsize[1],$cutsize[0],$cutsize[1]);imagedestroy($this->image);$this->image=$imagecut;} /*** 展现图片* @return [type] [description]*/ public function show(){header(content-type:.$this->info['mime']);$funn=image.$this->type;$funn($this->image); } /*** 保存图片 * @param [type] $newname 新图片名 * @return [type][description] */public function save($newname){header(content-type:.$this->info['mime']);$funn=image.$this->type;$funn($this->image,$newname.'.'.$this->type);}public function __destruct(){imagedestroy($this->image);} } ?>
如果还需要其他操作,只需要再往这个类里面添加就好啦~~
给图片添加水印代码:
先看文件check_image_addwatermark.php代码
here is your pic!your image has been saved!.jpg alt=>here is your pic!so how does it feel to be famous?here is the picture you just uploaded to your servers:
alt=>image save as:
height:
widht:
upload date:
you may apply a special effect to your image from the list of option below. note:saving an image with any of the filters applied can be undone
filter:noneblack and white';echo 'blur';echo 'emboss';echo 'negative';?>
embed caption in image?';echo '
';//添加水印选项echo 'embed watermarked logo in image?';?>
这里面主要是添加水印选项,如果选中添加水印则将logo.png作为水印图片和原来的图片合并在一起。
在预览文件中添加了对应的逻辑,代码如下: