欢迎来到福编程网,本站提供各种互联网专业知识!

php实现常见图片格式的水印和缩略图制作(面向对象)

发布时间:2016-06-15 作者:D_aneil 来源:转载
这篇文章主要介绍了php实现常见图片格式jpg,png,gif的水印和缩略图制作,使用面向对象方法实现PHP图片水印和缩略图功能,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了php水印和缩略图制作代码,使用面向对象的方法来实现常见图片格式jpg,png,gif的水印和缩略图的制作,供大家参考,具体内容如下

代码
  1. <?php
  2. header('Content-Type:text/html;charset=utf-8');
  3. /*
  4. * To change this license header, choose License Headers in Project Properties.
  5. * To change this template file, choose Tools | Templates
  6. * and open the template in the editor.
  7. */
  8. //给图片添加水印
  9. Class Water{
  10. //开启水印
  11. private $watermark_on = '1';
  12. public $water_img;
  13. //水印位置
  14. public $pos = 1;
  15. //压缩比
  16. public $pct = 80;
  17. //透明度
  18. public $quality = 80;
  19. public $text = '乐趣网zlblog.sinaapp.com';
  20. public $size = 12;
  21. public $color = '#000000';
  22. public $font = 'font.ttf';
  23. //thumb的制作
  24. //默认缩略图功能开启
  25. private $thumb_on = 1;
  26. //生成缩略图的方式
  27. public $thumb_type = 1;
  28. //生成缩略图的宽度
  29. public $thumb_width;
  30. //生成缩略图的高度
  31. public $thumb_height;
  32. //生成缩略图的后缀名
  33. public $thumb_fix = '_dq';
  34. //缩略图函数处理
  35. public function thumb( $img,$outfile='',$t_type='',$t_w='',$t_h='' ){
  36. //验证图片是否符合要求
  37. if(!$this->check($img) || !$this->thumb_on) return FALSE;
  38. //定义缩略图的初始值
  39. $t_type = $t_type ? $t_type : $this->thumb_type;
  40. $t_w = $t_w ? $t_w : $this->thumb_width;
  41. $t_h = $t_h ? $t_h : $this->thumb_height;
  42. //获取到原图的信息
  43. $img_info = getimagesize($img);
  44. $img_w = $img_info[0];
  45. $img_h = $img_info[1];
  46. //取得图像类型的文件后缀
  47. $img_type = image_type_to_extension($img_info[2]);
  48. //获取到相关尺寸
  49. $thumb_size = $this->thumb_size($img_w,$img_h,$t_w,$t_h,$t_type);
  50. //确定原始图像类型
  51. //利用自定义函数来实现图片类型的确定
  52. $func = "imagecreatefrom".substr($img_type, 1);
  53. $res_img = $func($img);
  54. //缩略图资源 编辑图片资源moon
  55. if( $img_type == '.gif' || $img_type == '.png' ){
  56. $res_thumb = imagecreate($thumb_size[0], $thumb_size[1]);
  57. $color = imagecolorallocate($res_thumb, 255, 0, 0);
  58. }else{
  59. $res_thumb = imagecreatetruecolor($thumb_size[0], $thumb_size[1]);
  60. }
  61. //制作缩略图
  62. if(function_exists( "imagecopyresampled" ) ){
  63. imagecopyresampled($res_thumb, $res_img, 0, 0, 0, 0, $thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
  64. }else{
  65. imagecopyresized($res_thumb, $res_img, 0, 0, 0, 0, $thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
  66. }
  67. //处理透明色
  68. if( $img_type =='.gif' || $img_type == '.png' ){
  69. imagecolortransparent($res_thumb,$color);
  70. }
  71. //配置输出文件名
  72. $outfile = $outfile ? $outfile : $outfile.substr($img,0,strripos($img,'.')).$this->thumb_fix.$img_type;
  73. //文件的保存输出
  74. $func = "image".substr($img_type, 1);
  75. $func($res_thumb,$outfile);
  76. if(isset($res_thumb)) imagedestroy ($res_thumb);
  77. if(isset($res_img)) imagedestroy ($res_img);
  78. return $outfile;
  79. }
  80. public function watermark( $img,$pos='',$out_img='',$water_img='',$text='' ){
  81. if(!$this->check($img) || !$this->watermark_on) return false;
  82. $water_img = $water_img ? $water_img : $this->water_img;
  83. //水印的开启状态
  84. $waterimg_on = $this->check($water_img) ? 1 : 0;
  85. //判断是否在原图上操作
  86. $out_img = $out_img ? $out_img : $img;
  87. //判断水印的位置
  88. $pos = $pos ? $pos : $this->pos;
  89. //水印文字
  90. $text = $text ? $text : $this->text;
  91. $img_info = getimagesize($img);
  92. $img_w = $img_info[0];
  93. $img_h = $img_info[1];
  94. //判断水印图片的类型
  95. if( $waterimg_on ){
  96. $w_info = getimagesize($water_img);
  97. $w_w = $w_info[0];
  98. $w_h = $w_info[1];
  99. if ( $img_w < $w_w || $img_h < $w_h ) return false;
  100. switch ( $w_info[2] ){
  101. case 1:
  102. $w_img = imagecreatefromgif($water_img);
  103. break;
  104. case 2:
  105. $w_img = imagecreatefromjpeg($water_img);
  106. break;
  107. case 3:
  108. $w_img = imagecreatefrompng($water_img);
  109. break;
  110. }
  111. }else{
  112. if( empty($text) || strlen($this->color)!=7 ) return FALSE;
  113. $text_info = imagettfbbox($this->size, 0, $this->font, $text);
  114. $w_w = $text_info[2] - $text_info[6];
  115. $w_h = $text_info[3] - $text_info[7];
  116. }
  117. //建立原图资源
  118. switch ( $img_info[2] ){
  119. case 1:
  120. $res_img = imagecreatefromgif($img);
  121. break;
  122. case 2:
  123. $res_img = imagecreatefromjpeg($img);
  124. break;
  125. case 3:
  126. $res_img = imagecreatefrompng($img);
  127. break;
  128. }
  129. //确定水印的位置
  130. switch ( $pos ){
  131. case 1:
  132. $x = $y =25;
  133. break;
  134. case 2:
  135. $x = ($img_w - $w_w)/2;
  136. $y = 25;
  137. break;
  138. case 3:
  139. $x = $img_w - $w_w;
  140. $y = 25;
  141. break;
  142. case 4:
  143. $x = 25;
  144. $y = ($img_h - $w_h)/2;
  145. break;
  146. case 5:
  147. $x = ($img_w - $w_w)/2;
  148. $y = ($img_h - $w_h)/2;
  149. break;
  150. case 6:
  151. $x = $img_w - $w_w;
  152. $y = ($img_h - $w_h)/2;
  153. break;
  154. case 7:
  155. $x = 25;
  156. $y = $img_h - $w_h;
  157. break;
  158. case 8:
  159. $x = ($img_w - $w_w)/2;
  160. $y = $img_h - $w_h;
  161. break;
  162. case 9:
  163. $x = $img_w - $w_w;
  164. $y = $img_h - $w_h;
  165. break;
  166. default :
  167. $x = mt_rand(25, $img_w - $w_w);
  168. $y = mt_rand(25, $img_h - $w_h);
  169. }
  170. //写入图片资源
  171. if( $waterimg_on ){
  172. imagecopymerge($res_img, $w_img, $x, $y, 0, 0, $w_w, $w_h, $this->pct);
  173. }else{
  174. $r = hexdec(substr($this->color, 1,2));
  175. $g = hexdec(substr($this->color, 3,2));
  176. $b = hexdec(substr($this->color, 5,2));
  177. $color = imagecolorallocate($res_img, $r, $g, $b);
  178. imagettftext($res_img, $this->size, 0, $x, $y, $color, $this->font, $text);
  179. }
  180. //生成图片类型
  181. switch ( $img_info[2] ){
  182. case 1:
  183. imagecreatefromgif($res_img,$out_img);
  184. break;
  185. case 2:
  186. //imagecreatefromjpeg($res_img,$out_img);
  187. imagejpeg($res_img,$out_img);
  188. break;
  189. case 3:
  190. imagepng($res_img,$out_img);
  191. break;
  192. }
  193. if(isset($res_img)) imagedestroy ($res_img);
  194. if(isset($w_img)) imagedestroy($w_img);
  195. return TRUE;
  196. }
  197. //验证图片是否存在
  198. private function check($img){
  199. $type = array('.jpg','.jpeg','.png','.gif');
  200. $img_type = strtolower(strrchr($img, '.'));
  201. return extension_loaded('gd') && file_exists($img) && in_array($img_type, $type);
  202. }
  203. //获取缩略图的相关比例
  204. //获取到图片的处理类型
  205. private function thumb_size( $img_w,$img_h,$t_w,$t_h,$t_type){
  206. //定义缩略图尺寸
  207. $w = $t_w;
  208. $h = $t_h;
  209. //定义图片的原始尺寸
  210. $cut_w = $img_w;
  211. $cut_h = $img_h;
  212. //当要目标图像小于缩略图的尺寸时;
  213. if( $img_w <= $t_w && $img_h < $t_h ){
  214. $w = $img_w;
  215. $h = $img_h;
  216. }else{
  217. if( !empty($t_type) && $t_type>0 ){
  218. switch ( $t_type ){
  219. //当宽度固定时
  220. case 1:
  221. $h = $t_w/$img_w*$img_h;
  222. break;
  223. //高度固定时
  224. case 2:
  225. $w = $t_h/$img_h*$img_w;
  226. break;
  227. //宽度固定,高度裁切
  228. case 3:
  229. $cut_h = $img_w/$t_w*$t_h;
  230. break;
  231. //高度固定,宽度裁切
  232. case 4:
  233. $cut_w = $img_h/$t_h*$t_w;
  234. break;
  235. //等比例缩放
  236. default :
  237. if( ($img_w/$t_w) > ($img_h/$t_h) ){
  238. $h = $t_w/$img_w*$t_h;
  239. }elseif( ($img_w/$t_w) < ($img_h/$t_h) ){
  240. $w = $t_h/$img_h*$t_w;
  241. }else{
  242. $w = $t_w;
  243. $h = $t_h;
  244. }
  245. }
  246. }
  247. }
  248. $arr[0] = $t_w;
  249. $arr[1] = $t_h;
  250. $arr[2] = $cut_w;
  251. $arr[3] = $cut_h;
  252. return $arr;
  253. }
  254. }

以上就是本文的全部内容,希望对大家学习PHP程序设计有所帮助。

相关推荐