本文实例讲述了ThinkPHP打水印及设置水印位置的方法。分享给大家供大家参考,具体如下:
最近在用Thinkphp的打水印的功能,发现只能打在左下角PHP。打水印功还是很容易的,最要是用到复制代码代码如下: bool imagecopy合并(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)
将src_im图像中坐标从src_x,src_y开始,宽度为src_w,高度为src_h的一部分拷贝到dst_im图像中坐标为dst_x和dst_y的位置上。两图像将根据降钙素原来决定合并程度,其值范围从0 到100。当pct=0时,实际上什么也没做,当为100 时对于调色板图像本函数和imagecopy()完全一样,它对真彩色图像实现了希腊字母的第一个字母透明。
水印演示图:
我需要把水印打到图片的真中间,查看Thinkphp代码。发现,作者居然是写死了,我只能做一个修改
/*** 为图片添加水印* @静态公共* @param字符串$source原文件名* @param string $water水印图片* @param字符串$$savename添加水印后的图片名* @param字符串$ postion水印的具体位置左下右下左上右上中新增* @param字符串$alpha水印的透明度* @ return void */static public function water($ source,$water,$savename=null,$ postion=' center ',$alpha=80) {//检查文件是否存在if(!file_exists($source) ||!file_exists($water))返回false//图片信息$ SinFo=self :3360 getimageinfo($ source);$ WinFo=self :3360 getimageinfo($ water);//如果图片小于水印图片,不生成图片如果($ SinFo[' width ']$ WinFo[' width ']| | $ SinFo[' height ']$ WinFo[' height '])返回false//建立图像$sCreateFun='imagecreatefrom ' .$ SinFo[' type '];$ sImage=$ sCreateFun($ source);$wCreateFun='imagecreatefrom ' .$ WinFo[' type '];$ Wimage=$ wCreateFun($ water);//设定图像的混色模式imagealphablending($wImage,true);//图像位置,默认为右下角右对齐$ posArr=$ this-water位置($ position,$sInfo,$ WinFo);//新增//生成混合图像imagecopymerge($sImage,$wImage,$posArr[0],$posArr[1],0,0,$wInfo['width'],$wInfo['height'],$ alpha);//输出图像$ImageFun='Image ' .$ SinFo[' type '];//如果没有给出保存文件名,默认为原图像名if(!$ save name){ $ save name=$ source;@ unlink($ source);} //保存图像$ImageFun($sImage,$ save name);图像破坏($ sImage);}私人功能水位置($ position,$sInfo,$ wInfo){ $ posY=$ sInfo[' height ']-$ wInfo[' height '];$ POSx=$ SinFo[' width ']-$ WinFo[' width '];switch($ position){ case ' right bottom ' : return array($ POSx,$ POSy);打破;案例'左下' :返回数组($wInfo['width'],$ POSy);打破;案例' lefttop':返回数组($wInfo['width'],$ WinFo[' height ']);打破;案例'右上方' :返回数组($posX,$ WinFo[' height ']);打破;案例“中心”:返回数组($posX/2,$ POSy/2);打破;}}更多关于thinkPHP相关内容感兴趣的读者可查看本站专题: 《ThinkPHP入门教程》 、 《thinkPHP模板操作技巧总结》 、 《ThinkPHP常用方法总结》 、 《smarty模板入门基础教程》 及《PHP模板技术总结》 。
希望本文所述对大家基于ThinkPHP框架的服务器端编程语言(专业超文本预处理器的缩写)程序设计有所帮助。