ホームページ  >  記事  >  バックエンド開発  >  サムネイルとウォーターマークを実装する PHP クラス

サムネイルとウォーターマークを実装する PHP クラス

WBOY
WBOYオリジナル
2016-07-25 08:45:09734ブラウズ
    /**
  1. * 画像スケーリングウォーターマーククラス
  2. *
  3. * @version 1.0;
  4. *
  5. */
  6. class cls_photo
  7. {
  8. protected $waterrate = 0.2; //画像上の透かしアイコンの比率
  9. protected $width = 300; //サムネイルのデフォルトの幅
  10. protected $height = 200; // サムネイルのデフォルトの高さ
  11. protected $padding = 5; // ウォーターマーク画像から端までの距離
  12. protected $water_mark = "./water.png";
  13. protected $water_mark_pos = 5;ウォーターマーク画像の位置 ( 1= 左上隅、2= 右上隅、3= 左下隅、4= 右下隅、5 中央)
  14. protected $watermode = 0; // サムネイル 1 がウォーターマークを印刷する場合、0 はウォーターマークを印刷しません。サムネイル時
  15. protected $magick_handle; // 画像操作ハンドル
  16. protected $format = array('jpg','gif','png','jpeg'); // 画像ファイル形式の制限
  17. protected $smallpic_mode = 2; /デフォルトモード 0は生成されません サムネイル、1はトリミングスケーリング、2は比例スケーリング、3はスケーリングフィルモード
  18. /**
  19. * 画像パラメータを設定します
  20. *
  21. * @param $arg 画像パラメータは次のように配列に複数回入れることができます
  22. * @param $protected パラメータ値
  23. * array(
  24. * 'waterrate'=>0.2,
  25. * ' Water_mark '=>'./water.png',
  26. * 'water_mark_pos'=>4,
  27. * 'smallpic_mode'=>1
  28. * );
  29. * @return ture/false
  30. */
  31. public function set_args($arg,$val="")
  32. {
  33. $params = array( 'waterrate','water_mark','water_mark_pos','smallpic_mode','watermode','width','height');
  34. if(is_array($arg))
  35. {
  36. foreach ($ arg as $k => ;$v)
  37. {
  38. if(in_array($k,$params))
  39. {
  40. $this->$k = $v;
  41. }
  42. }
  43. }
  44. else
  45. {
  46. if (empty($val) )
  47. {
  48. return false;
  49. }
  50. else
  51. {
  52. if(in_array($arg,$params))
  53. {
  54. $this->$arg = $val;
  55. }
  56. }
  57. }
  58. return true;
  59. }
  60. /**
  61. * 画像のスケーリング
  62. *
  63. * @param $src_file ソースファイルパス
  64. * @param $dst_file 宛先ファイルパス
  65. * @return サムネイル画像パス/false
  66. */
  67. public functionscale($src_file,$dst_file="")
  68. {
  69. $dst_width = $this->width;
  70. $dst_height = $this ->高さ;
  71. $mode = $this->smallpic_mode;
  72. $magic_water_handle = NewMagickWand();
  73. if (!MagickReadImage($magic_water_handle, $src_file))return false;
  74. //Type
  75. $srcext = strto lower (MagicGetImageFormat($magic_water_handle) );
  76. if($srcext=='bmp')
  77. {
  78. $srcext = 'jpeg';
  79. }
  80. if(!in_array($srcext,$this->format))return false ;
  81. //サイズ
  82. $src_width = MagickGetImageWidth($magic_water_handle);
  83. $src_height = MagickGetImageHeight($magic_water_handle);
  84. //トリミングスケーリングモード
  85. if($mode == 1)
  86. {
  87. $pos_x=$pos_y = 0;//トリミング 一時的な位置をカット
  88. $src_widthc = $src_width;//一時的な幅をカット
  89. $src_heightc = $src_height;//一時的な高さをカット
  90. if($src_width/$src_height>$dst_width/$dst_height)
  91. {
  92. $src_widthc = $ SRC_HEIGHT*$ dst_width/$ dst_height;
  93. $ POS_X = ($ src_width-$ src_widthc)/2;
  94. $ POS_Y = ($ SRC_HEIGHT -$src_heightc)/2;
  95. }
  96. $magic_water_handle,$src_widthc,$src_heightc,$pos_x,$pos_y);//Crop
  97. //MagickCropImage 関数の後、Gif 画像は変更されますが、キャンバスは変更されないためです
  98. $ this->magick_handle = NewMagickWand();
  99. MagickNewImage($this->magick_handle,$src_widthc,$src_heightc,'#ffffff');
  100. MagickSetFormat($this->magick_handle,$srcext);
  101. MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp) ,0,0);
  102. //スケール
  103. MagickScaleImage($this->magick_handle, $dst_width, $dst_height);
  104. }
  105. //比例スケーリングモード
  106. if($ mode == 2)
  107. {
  108. if( $src_width/$src_height>$dst_width/$dst_height)
  109. {
  110. $dst_height=$dst_width*$src_height/$src_width;
  111. }
  112. else
  113. {
  114. $dst_width=$dst_height * $src_width/$src_height;
  115. }
  116. $this->magick_handle=$magic_water_handle;//代替
  117. MagickScaleImage($this->magick_handle, $dst_width, $dst_height);//缩放
  118. }
  119. //缩放充填モード
  120. if($mode == 3 )
  121. {
  122. if($src_width/$src_height>$dst_width/$dst_height)
  123. {
  124. $dst_heightc=$dst_width*$src_height/$src_width;
  125. $dst_widthc=$dst_width;
  126. }
  127. else
  128. {
  129. $dst_widthc= $dst_height*$src_width/$src_height;
  130. $dst_heightc=$dst_height;
  131. }
  132. MagickScaleImage($magic_water_handle, $dst_widthc, $dst_heightc);//缩放
  133. $this->magick_handle = NewMagickWand();
  134. MagickNewImage($ this->magick_handle,$dst_width,$dst_height,$this->smallpic_bgcolor);
  135. MagickSetFormat($this->magick_handle,$srcext);
  136. MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,( $dst_width-$dst_widthc)/2,($dst_height-$dst_heightc)/2);
  137. }
  138. //打水印
  139. if($this->watermode == 1)
  140. {
  141. $this->set_mark( );
  142. }
  143. if(empty($dst_file))
  144. {
  145. //建立時文件
  146. $dst_file = tempnam($_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG");
  147. }
  148. MagickWriteImage($this-> magick_handle, $dst_file);
  149. return $dst_file;
  150. }
  151. /**
  152. * Watermarking
  153. *
  154. * @param $src_file ウォーターマークを入れる画像のパス
  155. * @param $dst_file ウォーターマークを生成するファイルの保存パス 空の場合、ランダムな一時ファイルが生成されます
  156. * @returnウォーターマークファイルのパス/false
  157. */
  158. public function Water_mark($src_file,$dst_file="")
  159. {
  160. $this->magick_handle = NewMagickWand() ;
  161. if (!MagickReadImage($this->magick_handle, $src_file))
  162. return false;
  163. $this->set_mark();
  164. if(empty($dst_file))
  165. {
  166. //建立临時文件
  167. $dst_file = tempnam($_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG");
  168. }
  169. MagickWriteImage($this->magick_handle, $dst_file);
  170. return $dst_file;
  171. }
  172. /**
  173. * 内部インターフェース
  174. * 透かし画像
  175. *
  176. * /
  177. protected function set_mark()
  178. {
  179. //尺寸
  180. $dst_width = MagickGetImageWidth($this->magick_handle);
  181. $dst_height = MagickGetImageHeight($this->magick_handle);
  182. //处理水印图
  183. if ($this->water_mark && is_file($this->water_mark))
  184. {
  185. $magic_water_handle = NewMagickWand();
  186. MagickRemoveImage($magic_water_handle);
  187. if (MagickReadImage($magic_water_handle, $this->water_mark) )
  188. {
  189. MagickScaleImage($magic_water_handle, $dst_width*$this->waterrate, $dst_width*$this->waterrate*MagickGetImageHeight($magic_water_handle)/MagickGetImageWidth($magic_water_handle));//画像への放水印の1 /5
  190. if ($this->water_mark_pos == 1)
  191. {
  192. $left = $this->padding;
  193. $top = $this->padding;
  194. }
  195. elseif ($this->water_mark_pos == 2)
  196. {
  197. $left = $dst_width-$this->padding-MagickGetImageWidth($magic_water_handle);
  198. $top = $this->padding;
  199. }
  200. elseif ($this->water_mark_pos == 3)
  201. {
  202. $left = $this->padding;
  203. $top = $dst_height -$this->padding-MagickGetImageHeight($magic_water_handle);
  204. }
  205. elseif ($this->water_mark_pos == 4)
  206. {
  207. $left = $dst_width-$this->padding-MagickGetImageWidth($magic_water_handle);
  208. $top =$dst_height -$this->padding-MagickGetImageHeight($magic_water_handle);
  209. }
  210. elseif ($this- >water_mark_pos == 5)
  211. {
  212. $left = ($dst_width-MagickGetImageWidth($magic_water_handle))/2;
  213. $top =($dst_height -MagickGetImageHeight($magic_water_handle))/2;
  214. }
  215. MagickCompositeImage($this ->magick_handle,$magic_water_handle,MW_OverCompositeOp,$left,$top);
  216. }
  217. }
  218. }
  219. }
复制代

和加, php


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。