PHP对图片进行水印添加以及生成缩率图

原创
2016-07-25 08:42:31 569浏览
1 给图片进行水印添加
2生成一个新的缩率图
  1. class Image{
  2. //水印配置项
  3. private $waterOn;
  4. private $waterImg;
  5. private $waterPos;
  6. private $waterPct;
  7. private $waterText;
  8. private $waterFont;
  9. private $waterTextSize;
  10. private $waterTextColor;
  11. private $qua;
  12. //缩略图配置项
  13. private $thumbWidth;
  14. private $thumbHeight;
  15. private $thumbType;
  16. private $thumbEndfix;
  17. //构造函数
  18. public function __construct(){
  19. $this->waterOn=C("WATER_ON");
  20. $this->waterImg=C("WATER_IMG");
  21. $this->waterPos=C("WATER_POS");
  22. $this->waterPct=C("WATER_PCT");
  23. $this->waterText=C("WATER_TEXT");
  24. $this->waterFont=C("WATER_FONT");
  25. $this->waterTextSize=C("WATER_TEXT_SIZE");
  26. $this->waterTextColor=C("WATER_TEXT_COLOR");
  27. $this->qua=C("WATER_QUA");
  28. //缩率图
  29. $this->thumbWidth=C("THUMB_WIDTH");
  30. $this->thumbHeight=C("THUMB_HEIGHT");
  31. $this->thumbType=C("THUMB_TYPE");
  32. $this->thumbEndFix=C("THUMB_ENDFIX");
  33. }
  34. /*
  35. *验证图片是否合法
  36. */
  37. private function check($img){
  38. return is_file($img)&&getimagesize($img)&&extension_loaded("gd");
  39. }
  40. /*
  41. *缩率图
  42. *@param string $img 原图
  43. *@param string $outFile 缩率之后存储的图片
  44. *@param int $thumbWidth 缩率图宽度
  45. *@param int $thumbHeight 缩率图高度
  46. *@param int $thumbType 那种方式进行缩略处理
  47. */
  48. public function thumb($img,$outFile="",$thumbWidth="",$thumbHeight="",$thumbType=""){
  49. if(!$this->check($img)){
  50. return false;
  51. }
  52. //缩率图处理方式
  53. $thumbType=$thumbType?$thumbType:$this->thumbType;
  54. //缩率图宽度
  55. $thumbWidth=$thumbWidth?$thumbWidth:$this->thumbWidth;
  56. //缩率图高度
  57. $thumbHeight=$thumbHeight?$thumbHeight:$this->thumbHeight;
  58. //获取原图信息
  59. $imgInfo=getimagesize($img);
  60. //原图宽度
  61. $imgWidth=$imgInfo[0];
  62. //原图高度
  63. $imgHeight=$imgInfo[1];
  64. //获得原图类型
  65. $imgtype=image_type_to_extension($imgInfo[2]);
  66. //根据不同的缩略处理方式,获得尺寸(原图和缩略图相应的尺寸)
  67. $thumb_size=$this->thumbsize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType);
  68. //创建原图
  69. $func="imagecreatefrom".substr($imgtype,1);//变量函数
  70. $resImg=$func($img);
  71. //创建缩率图画布
  72. if($imgtype==".gif"){
  73. $res_thumb=imagecreate($thumb_size[2],$thumb_size[3]);
  74. }else{
  75. $res_thumb=imagecreatetruecolor($thumb_size[2],$thumb_size[3]);
  76. }
  77. imagecopyresized($res_thumb,$resImg,0,0,0,0,$thumb_size[2],$thumb_size[3],$thumb_size[0],$thumb_size[1]);
  78. $fileInfo=pathinfo($img);//文件信息
  79. $outFile=$outFile?$outFile:$fileInfo['filename'].$this->thumbEndFix.$fileInfo['extension'];//文件名称
  80. $outFile=$fileInfo["dirname"]."//m.sbmmt.com/m/".$outFile;//加上目录
  81. $func="image".substr($imgtype,1);
  82. $func($res_thumb,$outFile);
  83. return $outFile;
  84. }
  85. private function thumbSize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType){
  86. //缩率图尺寸
  87. $w=$thumbWidth;
  88. $h=$thumbHeight;
  89. //原图尺寸
  90. $img_w=$imgWidth;
  91. $img_h=$imgHeight;
  92. switch($thumbType){
  93. case 1:
  94. //宽度固定,高度自增
  95. $h=$w/$imgWidth*$imgHeight;
  96. break;
  97. case 2://高度固定,宽度自
  98. $w=$h/$imgHeight*$imgWidth;
  99. break;
  100. case 3:
  101. if($imgHeight/$thumbHeight>$imgWidth/$thumbWidth){
  102. $img_h=$imgWidth/$thumbWidth*$thumbHeight;
  103. }else{
  104. $img_w=$imgHeight/$thumbHeight*$thumbWidth;
  105. }
  106. }
  107. return array($img_w,$img_h,$w,$h);
  108. }
  109. /*
  110. *@param string $img 原图
  111. *@param string $outImg 加完水印后生成的图
  112. *@param int $pos 水印位置
  113. *@param int $pct 透明度
  114. *@param text $text 水印文字
  115. *@param string $waterImg水印图片
  116. */
  117. public function water($img,$outImg=null,$pos="",$pct="",$text="",$waterImg="",$textColor=""){
  118. if(!$this->check($img)){
  119. return false;
  120. }
  121. //加完水印后生成的图
  122. $outImg=$outImg?$outImg:$img;
  123. //水印位置
  124. $pos=$pos?$pos:$this->waterPos;
  125. //透明度
  126. $pct=$pct?$pct:$this->waterPct;
  127. //水印文字
  128. $text=$text?$text:$this->waterText;
  129. //水印图片
  130. $waterImg=$waterImg?$waterImg:$this->waterImg;
  131. //验证水印图片
  132. $waterImgOn=$this->check($waterImg);
  133. //水印文字颜色
  134. $textColor=$textColor?$textColor:$this->waterTextColor;
  135. //原图信息
  136. $imgInfo=getimagesize($img);
  137. //原图宽度
  138. $imgWidth=$imgInfo[0];
  139. //原图高度
  140. $imgHeight=$imgInfo[1];
  141. switch($imgInfo[2]){
  142. case 1:
  143. $resImg=imagecreatefromgif($img);
  144. break;
  145. case 2:
  146. $resImg=imagecreatefromjpeg($img);
  147. break;
  148. case 3:
  149. $resImg=imagecreatefrompng($img);
  150. break;
  151. }
  152. if($waterImgOn){//水印图片有效
  153. //水印信息
  154. $waterInfo=getimagesize($waterImg);
  155. //水印宽度
  156. $waterWidth=$waterInfo[0];
  157. //水印高度
  158. $waterHeight=$waterInfo[1];
  159. //根据不同的情况创建不同的类型 gif jpeg png
  160. $w_img=null;
  161. switch($waterInfo[2]){
  162. case 1:
  163. $w_img=imagecreatefromgif($waterImg);
  164. break;
  165. case 2:
  166. $w_img=imagecreatefromjpeg($waterImg);
  167. break;
  168. case 3:
  169. $w_img=imagecreatefrompng($waterImg);
  170. }
  171. }else{//水印图片失效,使用文字水印
  172. if(empty($text)||strlen($textColor)!==7){
  173. return false;
  174. }
  175. //获得文字水印盒子信息
  176. $textInfo=imagettfbbox($this->waterTextSize,0,$this->waterFont,$text);
  177. //文字信息宽度
  178. $textWidth=$textInfo[2]-$textInfo[6];
  179. //文字信息高度
  180. $textHeight=$textInfo[3]-$textInfo[7];
  181. }
  182. //水印位置
  183. $x=$y=20;
  184. switch($pos){
  185. case 1:
  186. break;
  187. case 2:
  188. $x=($imgWidth-$waterWidth)/2;
  189. break;
  190. case 3:
  191. $y=$imgWidth-$waterWidth-10;
  192. break;
  193. case 4:
  194. $x=($imgHeight-$waterHeight)/2;
  195. break;
  196. case 5:
  197. $x=($imgWidth-$waterWidth)/2;
  198. $y=($imgHeight-$waterHeight)/2;
  199. break;
  200. case 6:
  201. $x=$imgWidth-$waterWidth-10;
  202. $y=($imgHeight-$waterHeight)/2;
  203. break;
  204. case 7:
  205. $x=$imgHeight-$waterHeight-10;
  206. break;
  207. case 8:
  208. $x=($imgWidth-$waterWidth)/2;
  209. $y=$imgHeight-$waterHeight-10;
  210. break;
  211. case 9:
  212. $x=$imgWidth-$waterWidth-10;
  213. $y=$imgHeight-$waterHeight-10;
  214. break;
  215. default:
  216. $x=mt_rand(20,$imgWidth-$waterWidth);
  217. $y=mt_rand(20,$imgHeight-$waterHeight);
  218. }
  219. if($waterImgOn){//当水印图片有效时,以图片形式加水印
  220. if($waterInfo[2]==3){
  221. imagecopy($resImg,$w_img,$x,$y,0,0,$waterWidth,$waterHeight);
  222. }else{
  223. imagecopymerge($resImg,$w_img,$x,$y,0,0,$waterInfo,$waterHeight,$pct);
  224. }
  225. }else{//水印图片失效,以文字水印加
  226. $red=hexdec(substr($this->waterTextColor,1,2));
  227. $greem=hexdec(substr($this->waterTextColor,3,2));
  228. $blue=hexdec(substr($this->waterTextColor,5,2));
  229. $color=imagecolorallocate($resImg,$red,$greem,$blue);
  230. imagettftext($resImg,$this->waterTextSize,0,$x,$y,$color,$this->waterFont,$text);
  231. }
  232. //输出图片
  233. switch($imgInfo[2]){
  234. case 1:
  235. imagegif($resImg,$outImg);
  236. break;
  237. case 2:
  238. imagejpeg($resImg,$outImg);
  239. break;
  240. case 3:
  241. imagepng($resImg,$outImg);
  242. break;
  243. }
  244. //垃圾回收
  245. if(isset($resImg)){
  246. imagedestroy($resImg);
  247. }
  248. if(isset($w_img)){
  249. imagedestroy($w_img);
  250. }
  251. return true;
  252. }
  253. }
  254. ?>
复制代码

  1. return array(
  2. //水印处理
  3. "WATER_ON"=>1,//水印开关
  4. "WATER_IMG"=>"./data/logo.png",//水印图片
  5. "WATER_POS"=>9,//水印位置
  6. "WATER_PCT"=>80,//水印透明度
  7. "WATER_TEXT"=>"http://www.caoxiaobin.cn",
  8. "WATER_FONT"=>"./data/simsunb.ttf",//水印字体
  9. "WATER_TEXT_COLOR"=>"#333333",//文字颜色 16进制表示
  10. "WATER_TEXT_SIZE"=>16,//文字大小
  11. "WATER_QUA"=>80,//图片压缩比
  12. //缩略图
  13. "THUMB_WIDTH"=>150,//缩率图宽度
  14. "THUMB_HEIGHT"=>150,//缩略图高度
  15. "THUMB_TYPE"=>1,//缩略图处理 1宽度固定,高度自增 2高度固定,宽度自增 //缩略图尺寸不变,对原图进行裁切
  16. "THUMB_ENDFIX"=>"_thmub"//缩略图后缀
  17. );
  18. ?>
复制代码

  1. /*
  2. * 不区分大小写的数据键检测
  3. */
  4. function array_key_exists_d($key,$arr){
  5. $_key=strtolower($key);
  6. foreach ($arr as $k=>$v){
  7. if($_key==strtolower($k)){
  8. return true;
  9. }
  10. }
  11. }
  12. /*
  13. * 递归更改数组的KEY(键名)
  14. * @param array;
  15. * @stat int 0小写 1大写
  16. */
  17. function array_change_key_case_d($arr,$stat=0){
  18. $func=$stat?"strtoupper":"strtolower";
  19. $_newArr=array();
  20. if(!is_array($arr)||empty($arr)){
  21. return $_newArr;
  22. }
  23. foreach($arr as $k=>$v){
  24. $_k=$func($k);//通过变量函数转换KEY大小写
  25. $_newArr[$_k]= is_array($v)?array_change_key_case_d($v):$v;
  26. }
  27. return $_newArr;
  28. }
  29. /*
  30. * 读取与设置配置项
  31. * @param $name void 配置项名称,如果不填写返回所有配置项
  32. * @param $value void 配置项的值
  33. * @param $value 值 false null 只取$name值
  34. */
  35. function C($name=null,$value=null){
  36. static $config=array();//静态变量$config存储所有配置项
  37. if(is_null($name)){
  38. return $config;
  39. }
  40. //如果$name为数组
  41. if(is_array($name)){
  42. return $config=array_merge($config,array_change_key_case_d($name,1));
  43. }
  44. //$name为字符串 2种情况 $value无值表示获得配置项的值,有值表示更改配置项
  45. if(is_string($name)){
  46. $name= strtoupper($name);
  47. //获得配置项的值
  48. if(is_null($value)){
  49. return array_key_exists_d($name,$config)?$config[$name]:null;
  50. }else{
  51. //设置值
  52. $config[$name]=$value;
  53. return true;
  54. }
  55. }
  56. }
复制代码

PHP, 缩率图


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。