实现缩略图和加水印的php类

WBOY
Freigeben: 2016-07-25 08:45:09
Original
755 Leute haben es durchsucht
  1. /**
  2. * 图片缩放水印类
  3. *
  4. * @version 1.0 ;
  5. *
  6. */
  7. class cls_photo
  8. {
  9. protected $waterrate = 0.2; //水印图标在图片上的比例
  10. protected $width = 300; //缩略图默认宽度
  11. protected $height = 200; //缩略图默认高度
  12. protected $padding = 5; //水印图到边的距离
  13. protected $water_mark = "./water.png";
  14. protected $water_mark_pos = 5;//水印图片位置(1=左上角,2=右上角,3=左下角,4=右下角,5中央)
  15. protected $watermode = 0;// 0缩略图时不打水印 1缩略图时打水印
  16. protected $magick_handle;//图片操作句柄
  17. protected $format = array('jpg','gif','png','jpeg'); // 图片文件格式限定
  18. protected $smallpic_mode = 2;//默认模式 0为不生成缩略图, 1为裁切缩放 ,2为比例缩放 3为缩放填充模式
  19. /**
  20. * 设置图片类参数
  21. *
  22. * @param $arg 图片参数 多次可放入数组里 如下
  23. * @param $protected 参数值
  24. * array(
  25. * 'waterrate'=>0.2,
  26. * 'water_mark'=>'./water.png',
  27. * 'water_mark_pos'=>4,
  28. * 'smallpic_mode'=>1
  29. * );
  30. * @return ture/false
  31. */
  32. public function set_args($arg,$val="")
  33. {
  34. $params = array('waterrate','water_mark','water_mark_pos','smallpic_mode','watermode','width','height');
  35. if(is_array($arg))
  36. {
  37. foreach ($arg as $k =>$v)
  38. {
  39. if(in_array($k,$params))
  40. {
  41. $this->$k = $v;
  42. }
  43. }
  44. }
  45. else
  46. {
  47. if(empty($val))
  48. {
  49. return false;
  50. }
  51. else
  52. {
  53. if(in_array($arg,$params))
  54. {
  55. $this->$arg = $val;
  56. }
  57. }
  58. }
  59. return true;
  60. }
  61. /**
  62. * 图片缩放
  63. *
  64. * @param $src_file 源文件路径
  65. * @param $dst_file 目标文件路径
  66. * @return 缩略图片路径/false
  67. */
  68. public function scale($src_file,$dst_file="")
  69. {
  70. $dst_width = $this->width;
  71. $dst_height = $this->height;
  72. $mode = $this->smallpic_mode;
  73. $magic_water_handle = NewMagickWand();
  74. if (!MagickReadImage($magic_water_handle, $src_file))return false;
  75. //类型
  76. $srcext = strtolower(MagickGetImageFormat($magic_water_handle));
  77. if($srcext=='bmp')
  78. {
  79. $srcext = 'jpeg';
  80. }
  81. if(!in_array($srcext,$this->format))return false;
  82. //尺寸
  83. $src_width = MagickGetImageWidth($magic_water_handle);
  84. $src_height = MagickGetImageHeight($magic_water_handle);
  85. //裁切缩放模式
  86. if($mode == 1)
  87. {
  88. $pos_x=$pos_y = 0;//裁切临时位置
  89. $src_widthc = $src_width;//裁切临时宽度
  90. $src_heightc = $src_height;//裁切临时高度
  91. if($src_width/$src_height>$dst_width/$dst_height)
  92. {
  93. $src_widthc = $src_height*$dst_width/$dst_height;
  94. $pos_x = ($src_width-$src_widthc)/2;
  95. }
  96. else
  97. {
  98. $src_heightc = $src_width*$dst_height/$dst_width;
  99. $pos_y = ($src_height-$src_heightc)/2;
  100. }
  101. MagickCropImage($magic_water_handle,$src_widthc,$src_heightc,$pos_x,$pos_y);//裁切
  102. //因为MagickCropImage函数后,Gif 图像改,但画布不变
  103. $this->magick_handle = NewMagickWand();
  104. MagickNewImage($this->magick_handle,$src_widthc,$src_heightc,'#ffffff');
  105. MagickSetFormat($this->magick_handle,$srcext);
  106. MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,0,0);
  107. //缩放
  108. MagickScaleImage($this->magick_handle, $dst_width, $dst_height);
  109. }
  110. //比例缩放模式
  111. if($mode == 2)
  112. {
  113. if($src_width/$src_height>$dst_width/$dst_height)
  114. {
  115. $dst_height=$dst_width*$src_height/$src_width;
  116. }
  117. else
  118. {
  119. $dst_width=$dst_height*$src_width/$src_height;
  120. }
  121. $this->magick_handle=$magic_water_handle;//替换
  122. MagickScaleImage($this->magick_handle, $dst_width, $dst_height);//缩放
  123. }
  124. //缩放填充模式
  125. if($mode == 3)
  126. {
  127. if($src_width/$src_height>$dst_width/$dst_height)
  128. {
  129. $dst_heightc=$dst_width*$src_height/$src_width;
  130. $dst_widthc=$dst_width;
  131. }
  132. else
  133. {
  134. $dst_widthc=$dst_height*$src_width/$src_height;
  135. $dst_heightc=$dst_height;
  136. }
  137. MagickScaleImage($magic_water_handle, $dst_widthc, $dst_heightc);//缩放
  138. $this->magick_handle = NewMagickWand();
  139. MagickNewImage($this->magick_handle,$dst_width,$dst_height,$this->smallpic_bgcolor);
  140. MagickSetFormat($this->magick_handle,$srcext);
  141. MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,($dst_width-$dst_widthc)/2,($dst_height-$dst_heightc)/2);
  142. }
  143. //打水印
  144. if($this->watermode == 1)
  145. {
  146. $this->set_mark();
  147. }
  148. if(empty($dst_file))
  149. {
  150. //建立临时文件
  151. $dst_file = tempnam($_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG");
  152. }
  153. MagickWriteImage($this->magick_handle, $dst_file);
  154. return $dst_file;
  155. }
  156. /**
  157. * 打水印
  158. *
  159. * @param $src_file 要打水印的图片路径
  160. * @param $dst_file 生产水印的文件保存路径,为空则生产随机临时文件
  161. * @return 水印文件路径/false
  162. */
  163. public function water_mark($src_file,$dst_file="")
  164. {
  165. $this->magick_handle = NewMagickWand();
  166. if (!MagickReadImage($this->magick_handle, $src_file))
  167. return false;
  168. $this->set_mark();
  169. if(empty($dst_file))
  170. {
  171. //建立临时文件
  172. $dst_file = tempnam($_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG");
  173. }
  174. MagickWriteImage($this->magick_handle, $dst_file);
  175. return $dst_file;
  176. }
  177. /**
  178. * 对内接口
  179. * 给图片打水印
  180. *
  181. */
  182. protected function set_mark()
  183. {
  184. //尺寸
  185. $dst_width = MagickGetImageWidth($this->magick_handle);
  186. $dst_height = MagickGetImageHeight($this->magick_handle);
  187. //处理水印图
  188. if ($this->water_mark && is_file($this->water_mark))
  189. {
  190. $magic_water_handle = NewMagickWand();
  191. MagickRemoveImage($magic_water_handle);
  192. if (MagickReadImage($magic_water_handle, $this->water_mark))
  193. {
  194. MagickScaleImage($magic_water_handle, $dst_width*$this->waterrate, $dst_width*$this->waterrate*MagickGetImageHeight($magic_water_handle)/MagickGetImageWidth($magic_water_handle));//缩放水印到图片的1/5
  195. if ($this->water_mark_pos == 1)
  196. {
  197. $left = $this->padding;
  198. $top = $this->padding;
  199. }
  200. elseif ($this->water_mark_pos == 2)
  201. {
  202. $left = $dst_width-$this->padding-MagickGetImageWidth($magic_water_handle);
  203. $top = $this->padding;
  204. }
  205. elseif ($this->water_mark_pos == 3)
  206. {
  207. $left = $this->padding;
  208. $top = $dst_height -$this->padding-MagickGetImageHeight($magic_water_handle);
  209. }
  210. elseif ($this->water_mark_pos == 4)
  211. {
  212. $left = $dst_width-$this->padding-MagickGetImageWidth($magic_water_handle);
  213. $top =$dst_height -$this->padding-MagickGetImageHeight($magic_water_handle);
  214. }
  215. elseif ($this->water_mark_pos == 5)
  216. {
  217. $left = ($dst_width-MagickGetImageWidth($magic_water_handle))/2;
  218. $top =($dst_height -MagickGetImageHeight($magic_water_handle))/2;
  219. }
  220. MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,$left,$top);
  221. }
  222. }
  223. }
  224. }
复制代码

和加, php


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!