PHP class for image scaling watermarks

Source: Internet
Author: User
PHP class for image scaling watermarks

  1. /**
  2. * Image scaling watermark
  3. *
  4. */
  5. Class cls_photo
  6. {
  7. Protected $ waterrate = 0.2; // ratio of the watermark icon to the image
  8. Protected $ width = 300; // Default width of the thumbnail
  9. Protected $ height = 200; // default height of the thumbnail
  10. Protected $ padding = 5; // distance from the watermark image to the edge
  11. Protected $ water_mark = "./water.png ";
  12. Protected $ water_mark_pos = 5; // watermark image position (1 = upper left corner, 2 = upper right corner, 3 = lower left corner, 4 = lower right corner, 5 Center)
  13. Protected $ watermode = 0; // when the thumbnail is 0, no watermark is made. when the thumbnail is 1, the watermark is made.
  14. Protected $ magick_handle; // Image operation handle
  15. Protected $ format = array ('jpg ', 'GIF', 'PNG', 'jpeg '); // The format of the image file is limited.
  16. Protected $ smallpic_mode = 2; // Default mode 0: No thumbnail is generated, 1 is cropping and scaling, 2 is proportional scaling, and 3 is scaling and filling.
  17. /**
  18. * Set image parameters
  19. *
  20. * @ Param $ arg the image parameters can be put into the array multiple times as follows:
  21. * @ Param $ protected parameter value
  22. * Array (
  23. * 'Waterrate' = & gt; 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. * Image scaling
  61. *
  62. * @ Param $ src_file source file path
  63. * @ Param $ dst_file path of the target file
  64. * @ Return the thumbnail path/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. // Type
  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. // Size
  81. $ Src_width = MagickGetImageWidth ($ magic_water_handle );
  82. $ Src_height = MagickGetImageHeight ($ magic_water_handle );
  83. // Cropping and scaling mode
  84. If ($ mode = 1)
  85. {
  86. $ Pos_x = $ pos_y = 0; // crop the temporary position
  87. $ Src_widthc = $ src_width; // crop the temporary width.
  88. $ Src_heightc = $ src_height; // crop the temporary 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); // cropping
  100. // Because after the MagickCropImage function, the Gif image is changed, but the canvas remains unchanged.
  101. $ This-> magick_handle = NewMagickWand ();
  102. MagickNewImage ($ this-> magick_handle, $ src_widthc, $ src_heightc, '# ffff ');
  103. MagickSetFormat ($ this-> magick_handle, $ srcext );
  104. MagickCompositeImage ($ this-> magick_handle, $ magic_water_handle, MW_OverCompositeOp, 0, 0 );
  105. // Zoom
  106. MagickScaleImage ($ this-> magick_handle, $ dst_width, $ dst_height );
  107. }
  108. // Proportional scaling mode
  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; // replace
  120. MagickScaleImage ($ this-> magick_handle, $ dst_width, $ dst_height); // zoom
  121. }
  122. // Scaling and filling mode
  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); // zoom
  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, handle, ($ dst_width-$ dst_widthc)/2, ($ dst_height-$ dst_heightc)/2 );
  140. }
  141. // Watermark
  142. If ($ this-> watermode = 1)
  143. {
  144. $ This-> set_mark ();
  145. }
  146. If (empty ($ dst_file ))
  147. {
  148. // Create a temporary file
  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. * Watermark
  156. *
  157. * @ Param $ src_file: Path of the image to be watermark
  158. * @ Param $ dst_file: specifies the path for saving the watermark file. if it is null, a random temporary file is generated.
  159. * @ Return watermark file path/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. // Create a temporary file
  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. * Internal interface
  177. * Watermarking an image
  178. *
  179. */
  180. Protected function set_mark ()
  181. {
  182. // Size
  183. $ Dst_width = MagickGetImageWidth ($ this-> magick_handle );
  184. $ Dst_height = MagickGetImageHeight ($ this-> magick_handle );
  185. // Process the watermark image
  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 ($ resize, $ dst_width * $ this-> waterrate, $ dst_width * $ this-> waterrate * MagickGetImageHeight ($ resize)/MagickGetImageWidth ($ magic_water_handle )); // scale the watermark to 1/5 of the image
  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.