圖片縮放浮水印PHP類

WBOY
發布: 2016-07-25 08:46:08
原創
1033 人瀏覽過
  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. for ( $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_array ($. 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-this>height;
  70. $mode = $this->smallpic_mode;
  71. $magic_water_handle = NewMagickWand();
  72. if ( !MagickReadImage ( $magic_water_handle, $src_file ) ) return false;
  73. $srcext = strtolower ( MagickGetImageFormat ( $magic_water_handle ) );
  74. if ( $srcext=='bmp' )
  75. {
  76. $srcext = 'jpeg'; }format ) ) return false;
  77. //尺寸
  78. $src_width = MagickGetImageWidth ( $magic_water_handle );
  79. $src_heightWidth ( $magic_water_handle );
  80. $src_heightage = Magic.Imhage); 🎜> //裁切縮放模式
  81. if ( $mode == 1 )
  82. {
  83. $pos_x=$pos_y = 0;//裁切暫存位置
  84. $src_widthc = $src_width; /裁切臨時寬度
  85. $src_heightc = $src_height;//裁切臨時高度
  86. if ( $src_width/$src_height>$dst_width/$dst_height )
  87. {
  88. $height🎜 $dst_width/$dst_height;
  89. $pos_x = ( $src_width-$src_widthc ) /2;
  90. }
  91. else
  92. {
  93. $wheight dst_width;
  94. $pos_y = ( $src_height-$src_heightc ) /2;
  95. }
  96. MagickCropImage ( $magic_water_handle,$src_widthc,$src_heightc,$pos_water_handle,$src_widthc,$src_heightc,$pos_heightc,$posx,$/pos; //因為MagickCropImage函數後,Gif 影像改,但畫布不變
  97. $this->magick_handle = NewMagickWand();
  98. MagickNewImage ( $this->magick_handle,$src_widthc,$src_heffightc,'#ffffight' ) ;
  99. MagickSetFormat ( $this->magick_handle,$srcext );
  100. MagickCompositeImage ( $this->magick_handle,$magic_water_handle,MW_OverCompositeOp,0,0 );
  101. ->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. $this->magick_handle=$magic_water_handle;//替換
  112. MagickScaleImage ( $this->magick_handle, $dst_width, $dst_height ); > //縮放填滿模式
  113. if ( $mode == 3 )
  114. {
  115. if ( $src_width/$src_height>$dst_width/$dst_height )
  116. {
  117. $dst_height *$src_height/$src_width;
  118. $dst_widthc=$dst_width;
  119. }
  120. else
  121. {
  122. $dst_widthc=$dst_height*🎜> {
  123. $dst_widthc=$dst_height*$src_height* dst_height;
  124. }
  125. MagickScaleImage ( $magic_water_handle, $dst_widthc, $dst_heightc );//縮放
  126. $this->magick_handle = NewMagickWand();
  127. MagickNewImage ($this-dth_handwidw ->smallpic_bgcolor );
  128. MagickSetFormat ( $this->magick_handle,$srcext );
  129. MagickCompositeImage ( $this->magick_handle,$magic_water_handle,MW_OverpositeOp, (Comst_Op. $dst_heightc ) /2 );
  130. }
  131. //打水印
  132. if ( $this->watermode == 1 )
  133. {
  134. $this->; set_mark();
  135. }
  136. if (empty ( $dst_file ) )
  137. {
  138. //建立暫存檔案
  139. $dst_file = tempnam ( $_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG"]" );
  140. }
  141. MagickWriteImage ( $this->magick_handle, $dst_file );
  142. return $dst_file;
  143. }
  144. /**
  145. * 打浮水印
  146. *
  147. * @param $src_file 要打浮水印的圖片路徑
  148. * @param $dst_file 生產浮水印的檔案儲存路徑,為空則生產隨機暫存檔案
  149. * @return 浮水印檔案路徑/false
  150. function water_mark ( $src_file,$dst_file="" )
  151. {
  152. $this->magick_handle = NewMagickWand();
  153. if ( !MagickReadImage ( $this->magick_handle, $src_handle, $src_ ) ) return false;
  154. $this->set_mark();
  155. if (empty ( $dst_file ) )
  156. {
  157. //建立暫存檔案
  158. $dst_file = tempnam ( $ _SERVER["SINASRV_CHEDIRDIR "],"TMP_IMG" );
  159. }
  160. MagickWriteImage ( $this->magick_handle, $dst_file );
  161. return $dst_file;
  162. }
  163. return $dst_file;
  164. }
  165. / protected function set_mark()
  166. {
  167. //
  168. $dst_width = MagickGetImageWidth ( $this->magick_handle ); //處理浮水印圖
  169. if ( $this->water_mark && is_file ( $this->water_mark ) )
  170. {
  171. $magic_water_handle = NewMagickWand();
  172. MagickoveImage $ magic_water_handle );
  173. if ( MagickReadImage ( $magic_water_handle, $this->water_mark ) )
  174. {
  175. MagickScaleImage ( $magic_water_handle, $dst_width -thisd-wid waterrate *MagickGetImageHeight ( $magic_water_handle ) /MagickGetImageWidth ( $magic_water_handle ) );//將水印縮放到圖片的1/5
  176. if ( $this->water_mark_pos == 1 )
  177. if ( $this->water_mark_pos == 1 )
  178. $ this->padding;
  179. $top = $this->padding;
  180. }
  181. elseif ( $this->water_mark_pos == 2 )
  182. {
  183. $left = $dst_width-$ this ->padding-MagickGetImageWidth ( $magic_water_handle );
  184. $top = $this->padding;
  185. }
  186. elseif ( $this ->water_mark_pos == 3 )
  187. {padding;
  188. $top = $dst_height -$this->padding-MagickGetImageHeight ( $magic_water_handle );
  189. }
  190. elseif ( $this->water_mark_pos == 4 )
  191. {
  192. $left = $dst_width-$this->padding-MagickGetImageWidth ( $magic_water_handle );
  193. $top =$dst_height -$this->padding-MagickGetImageHeight ( $top =$dst_height -$this->padding-MagickGetImageHeight ($sese $. ( $this->water_mark_pos == 5 )
  194. {
  195. $left = ( $dst_width-MagickGetImageWidth ( $ magic_water_handle ) ) /2;
  196. $top = ( $dst_height -MagickGet_He 2 ;
  197. }
  198. MagickCompositeImage ( $this->magick_handle,$magic_water_handle,MW_OverCompositeOp,$left,$頂部);
  199. }
  200. }
  201. }
  202. }
  203. >
  204. 複製程式碼
PHP

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板