画像スケーリングウォーターマーク PHP クラス

WBOY
リリース: 2016-07-25 08:46:08
オリジナル
974 人が閲覧しました
    /**
  1. * 画像ズーム透かしクラス
  2. *
  3. */
  4. class cls_photo
  5. {
  6. protected $waterrate = 0.2; //画像上の透かしアイコンの比率
  7. protected $width = 300; //サムネイルのデフォルトの幅
  8. protected $height = 200; // サムネイルのデフォルトの高さ
  9. protected $padding = 5; // ウォーターマーク画像から端までの距離
  10. protected $water_mark = "./water.png";
  11. protected $water_mark_pos = 5;ウォーターマーク画像の位置 ( 1= 左上隅、2= 右上隅、3= 左下隅、4= 右下隅、5 中央)
  12. protected $watermode = 0; // サムネイル 1 がウォーターマークを印刷する場合、0 はウォーターマークを印刷しません。 when サムネイル
  13. protected $magick_handle; // 画像操作ハンドル
  14. protected $format = array ( 'jpg', 'gif', 'png', 'jpeg' ); // 画像ファイル形式の制限
  15. protected $smallpic_mode = 2; / デフォルトモード 0 は生成されません サムネイル、1 はクロップスケーリング、2 はプロポーショナルスケーリング、3 はスケーリングフィルモードです
  16. ​​
  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 functionscale ( $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 ( !MagicReadImage ( $magic_water_handle, $src_file ) ) return false;
  73. //Type
  74. $srcext = strto lower ( 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; $ POS_Y = ($ SRC_HEIGHT -$src_heightc ) /2;
  93. }
  94. $magic_water_handle,$src_widthc,$src_heightc,$pos_x,$pos_y );//Crop
  95. //MagickCropImage 関数の後、Gif 画像は変更されますが、キャンバスは変更されないためです
  96. $ this->magick_handle = NewMagickWand();
  97. MagickNewImage ( $this->magick_handle,$src_widthc,$src_heightc,'#ffffff' );
  98. MagickSetFormat ( $this->magick_handle,$srcext );
  99. MagickCompositeImage ( $this->magick_handle,$magic_water_handle,MW_OverCompositeOp) ,0,0 );
  100. //Scale
  101. MagickScaleImage ( $this->magick_handle, $dst_width, $dst_height );
  102. }
  103. //比例スケーリングモード
  104. if ( $mode == 2 )
  105. {
  106. if ( $src_width/$src_height>$dst_width/$dst_height )
  107. {
  108. $dst_height=$dst_width*$src_height/$src_width;
  109. }
  110. else
  111. {
  112. $dst_width=$dst_height * $src_width/$src_height;
  113. }
  114. $ this->magick_handle=$magic_water_handle;//置換
  115. MagickScaleImage ( $this->magick_handle, $dst_width, $dst_height );//Scale
  116. }
  117. // スケールフィルモード
  118. if ( $mode == 3 )
  119. {
  120. if ( $src_width/$src_height>$dst_width/$dst_height )
  121. {
  122. $dst_heightc=$dst_width*$src_height/$src_width;
  123. $dst_widthc=$dst_width;
  124. }
  125. else
  126. {
  127. $dst_widthc=$dst_height *$src_width/$src_height;
  128. $dst_heightc=$dst_height;
  129. }
  130. MagickScaleImage ( $magic_water_handle, $dst_widthc, $dst_heightc );//缩放
  131. $this->magick_handle = NewMagickWand();
  132. MagickNewImage ( $this->magick_handle,$dst_width,$dst_height,$this->smallpic_bgcolor );
  133. MagickSetFormat ( $this->magick_handle,$srcext );
  134. MagickCompositeImage ( $this->magick_handle,$magic_water_handle,MW_OverCompositeOp, ( $dst_width-$dst_widthc ) /2, ( $dst_height-$dst_heightc ) /2 );
  135. }
  136. //打水印
  137. if ( $this->watermode == 1 )
  138. {
  139. $this->set_mark();
  140. }
  141. if ( empty ( $dst_file ) )
  142. {
  143. //建立時文件
  144. $dst_file = tempnam ( $_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG" );
  145. }
  146. MagickWriteImage ( $this->magick_handle, $dst_file );
  147. return $dst_file;
  148. }
  149. /**
  150. * Watermarking
  151. *
  152. * @param $src_file ウォーターマークを入れる画像のパス
  153. * @param $dst_file ウォーターマークを生成するファイルの保存パス 空の場合、ランダムな一時ファイルが生成されます
  154. * @returnウォーターマークファイルのパス/false
  155. */
  156. public function Water_mark ( $src_file,$dst_file="" )
  157. {
  158. $this->magick_handle = NewMagickWand();
  159. if ( !MagickReadImage ( $this->magick_handle, $src_file ) )
  160. return false;
  161. $this->set_mark();
  162. if ( empty ( $dst_file ) )
  163. {
  164. //建立時文件
  165. $dst_file = tempnam ( $_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG" );
  166. }
  167. MagickWriteImage ( $this->magick_handle, $dst_file );
  168. return $dst_file;
  169. }
  170. /**
  171. * 内部インターフェース
  172. * 透かし画像
  173. *
  174. */
  175. protected function set_mark()
  176. {
  177. //尺寸
  178. $dst_width = MagickGetImageWidth ( $this->magick_handle );
  179. $dst_height = MagickGetImageHeight ( $this->magick_handle );
  180. //处理水印图
  181. if ( $this->water_mark && is_file ( $this->water_mark ) )
  182. {
  183. $magic_water_handle = NewMagickWand();
  184. MagickRemoveImage ( $magic_water_handle );
  185. if ( MagickReadImage ( $magic_water_handle, $this->water_mark ) )
  186. {
  187. MagickScaleImage ( $magic_water_handle, $dst_width*$this->waterrate , $dst_width*$this->waterrate*MagickGetImageHeight ( $magic_water_handle ) /MagickGetImageWidth ( $magic_water_handle ) );//缩放水印に写真の1/5
  188. if ( $this->water_mark_pos == 1 )
  189. {
  190. $left = $this->padding;
  191. $top = $this->padding;
  192. }
  193. elseif ( $this->water_mark_pos == 2 )
  194. {
  195. $left = $dst_width-$this-> padding-MagickGetImageWidth ( $magic_water_handle );
  196. $top = $this->padding;
  197. }
  198. elseif ( $this->water_mark_pos == 3 )
  199. {
  200. $left = $this->padding;
  201. $top = $dst_height -$this->padding-MagickGetImageHeight ( $magic_water_handle );
  202. }
  203. elseif ( $this->water_mark_pos == 4 )
  204. {
  205. $left = $dst_width-$this->padding-MagickGetImageWidth ( $magic_water_handle );
  206. $top =$dst_height -$this->padding-MagickGetImageHeight ( $magic_water_handle );
  207. }
  208. elseif ( $this->water_mark_pos == 5 )
  209. {
  210. $left = ( $dst_width-MagickGetImageWidth ( $magic_water_handle ) ) /2;
  211. $top = ( $dst_height -MagickGetImageHeight ( $magic_water_handle ) ) /2;
  212. }
  213. MagickCompositeImage ( $this->magick_handle,$magic_water_handle,MW_OverCompositeOp,$left,$top );
  214. }
  215. }
  216. }
  217. }
复制代

PHP


関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!