Maison  >  Article  >  développement back-end  >  图片缩放水印PHP类

图片缩放水印PHP类

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

PHP


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn