PHP classes that implement thumbnails and watermarks

Source: Internet
Author: User
  1. /**
  2. * Picture shrinkage Water printing type
  3. *
  4. * @version 1.0;
  5. *
  6. */
  7. Class Cls_photo
  8. {
  9. protected $waterrate = 0.2; The scale of the watermark icon on the picture
  10. protected $width = 300; Thumbnail default width
  11. protected $height = 200; Thumbnail default height
  12. protected $padding = 5; The distance from the watermark graph to the edge
  13. protected $water _mark = "./water.png";
  14. protected $water _mark_pos = 5;//watermark Picture position (1= upper left corner, 2 = upper right corner, 3 = lower left corner, 4 = lower right corner, 5 center)
  15. Protected $watermode = 0;//0 thumbnail does not hit watermark 1 thumbnail when drawing water
  16. Protected $magick _handle;//Picture manipulation handle
  17. Protected $format = array (' jpg ', ' gif ', ' PNG ', ' jpeg '); Picture file format Qualification
  18. protected $smallpic _mode = 2;//default mode 0 is not to generate thumbnails, 1 for crop scaling, 2 for scaling 3 for scale fill mode
  19. /**
  20. * Set Picture class parameters
  21. *
  22. * @param $arg Picture parameters can be placed in the array several times as follows
  23. * @param $protected parameter values
  24. * Array (
  25. * ' Waterrate ' =>0.2,
  26. * ' water_mark ' = './water.png ',
  27. * ' Water_mark_pos ' =>4,
  28. * ' Smallpic_mode ' =>1
  29. * );
  30. * @return Ture/false
  31. */
  32. Public Function Set_args ($arg, $val = "")
  33. {
  34. $params = Array (' waterrate ', ' Water_mark ', ' water_mark_pos ', ' smallpic_mode ', ' watermode ', ' width ', ' height ');
  35. if (Is_array ($arg))
  36. {
  37. foreach ($arg as $k = $v)
  38. {
  39. if (In_array ($k, $params))
  40. {
  41. $this $k = $v;
  42. }
  43. }
  44. }
  45. Else
  46. {
  47. if (empty ($val))
  48. {
  49. return false;
  50. }
  51. Else
  52. {
  53. if (In_array ($arg, $params))
  54. {
  55. $this $arg = $val;
  56. }
  57. }
  58. }
  59. return true;
  60. }
  61. /**
  62. * Image Zoom
  63. *
  64. * @param $src _file source file path
  65. * @param $dst _file destination file path
  66. * @return thumbnail image path/false
  67. */
  68. Public Function Scale ($src _file, $dst _file= "")
  69. {
  70. $DST _width = $this->width;
  71. $DST _height = $this->height;
  72. $mode = $this->smallpic_mode;
  73. $magic _water_handle = Newmagickwand ();
  74. if (! Magickreadimage ($magic _water_handle, $src _file)) return false;
  75. Type
  76. $srcext = Strtolower (Magickgetimageformat ($magic _water_handle));
  77. if ($srcext = = ' bmp ')
  78. {
  79. $srcext = ' jpeg ';
  80. }
  81. if (!in_array ($srcext, $this->format)) return false;
  82. Size
  83. $src _width = magickgetimagewidth ($magic _water_handle);
  84. $src _height = magickgetimageheight ($magic _water_handle);
  85. Crop Zoom mode
  86. if ($mode = = 1)
  87. {
  88. $pos _x= $pos _y = 0;//trim Temporary position
  89. $src _widthc = $src _width;//Cut temporary width
  90. $src _HEIGHTC = $src _height;//Cut Temporary height
  91. if ($src _width/$src _height> $dst _width/$dst _height)
  92. {
  93. $src _widthc = $src _height* $dst _width/$dst _height;
  94. $pos _x = ($src _width-$src _widthc)/2;
  95. }
  96. Else
  97. {
  98. $src _HEIGHTC = $src _width* $dst _height/$dst _width;
  99. $pos _y = ($src _height-$src _heightc)/2;
  100. }
  101. Magickcropimage ($magic _water_handle, $src _widthc, $src _heightc, $pos _x, $pos _y);//Cut
  102. Because the Gif image changes after the Magickcropimage function, the canvas does not change
  103. $this->magick_handle = Newmagickwand ();
  104. Magicknewimage ($this->magick_handle, $src _widthc, $src _heightc, ' #ffffff ');
  105. Magicksetformat ($this->magick_handle, $srcext);
  106. Magickcompositeimage ($this->magick_handle, $magic _water_handle,mw_overcompositeop,0,0);
  107. Scaling
  108. Magickscaleimage ($this->magick_handle, $dst _width, $dst _height);
  109. }
  110. Proportional Zoom Mode
  111. if ($mode = = 2)
  112. {
  113. if ($src _width/$src _height> $dst _width/$dst _height)
  114. {
  115. $DST _height= $dst _width* $src _height/$src _width;
  116. }
  117. Else
  118. {
  119. $DST _width= $dst _height* $src _width/$src _height;
  120. }
  121. $this->magick_handle= $magic _water_handle;//Replacement
  122. Magickscaleimage ($this->magick_handle, $dst _width, $dst _height);//Zoom
  123. }
  124. Scale fill Mode
  125. if ($mode = = 3)
  126. {
  127. if ($src _width/$src _height> $dst _width/$dst _height)
  128. {
  129. $DST _heightc= $dst _width* $src _height/$src _width;
  130. $DST _widthc= $DST _width;
  131. }
  132. Else
  133. {
  134. $DST _widthc= $dst _height* $src _width/$src _height;
  135. $DST _heightc= $DST _height;
  136. }
  137. Magickscaleimage ($magic _water_handle, $dst _widthc, $dst _heightc);//Zoom
  138. $this->magick_handle = Newmagickwand ();
  139. Magicknewimage ($this->magick_handle, $dst _width, $dst _height, $this->smallpic_bgcolor);
  140. Magicksetformat ($this->magick_handle, $srcext);
  141. Magickcompositeimage ($this->magick_handle, $magic _water_handle,mw_overcompositeop, ($dst _width-$dst _widthc)/2 , ($dst _height-$dst _heightc)/2);
  142. }
  143. Play Watermark
  144. if ($this->watermode = = 1)
  145. {
  146. $this->set_mark ();
  147. }
  148. if (Empty ($dst _file))
  149. {
  150. Create a temporary file
  151. $DST _file = Tempnam ($_server["Sinasrv_cache_dir"], "tmp_img");
  152. }
  153. Magickwriteimage ($this->magick_handle, $dst _file);
  154. return $DST _file;
  155. }
  156. /**
  157. * Water Stamp
  158. *
  159. * @param $src _file The image path to be watermarked
  160. * @param $dst _file production watermark File save path, empty the production of random temporary files
  161. * @return Watermark File path/false
  162. */
  163. Public Function Water_mark ($src _file, $dst _file= "")
  164. {
  165. $this->magick_handle = Newmagickwand ();
  166. if (! Magickreadimage ($this->magick_handle, $src _file))
  167. return false;
  168. $this->set_mark ();
  169. if (Empty ($dst _file))
  170. {
  171. Create a temporary file
  172. $DST _file = Tempnam ($_server["Sinasrv_cache_dir"], "tmp_img");
  173. }
  174. Magickwriteimage ($this->magick_handle, $dst _file);
  175. return $DST _file;
  176. }
  177. /**
  178. * Internal interface
  179. * Give the picture a water stamp
  180. *
  181. */
  182. protected function Set_mark ()
  183. {
  184. Size
  185. $DST _width = magickgetimagewidth ($this->magick_handle);
  186. $DST _height = magickgetimageheight ($this->magick_handle);
  187. Working with Watermark Maps
  188. if ($this->water_mark && is_file ($this->water_mark))
  189. {
  190. $magic _water_handle = Newmagickwand ();
  191. Magickremoveimage ($magic _water_handle);
  192. if (Magickreadimage ($magic _water_handle, $this->water_mark))
  193. {
  194. Magickscaleimage ($magic _water_handle, $dst _width* $this->waterrate, $dst _width* $this->waterrate* Magickgetimageheight ($magic _water_handle)/magickgetimagewidth ($magic _water_handle)),//water release to the picture of 1/5
  195. if ($this->water_mark_pos = = 1)
  196. {
  197. $left = $this->padding;
  198. $top = $this->padding;
  199. }
  200. ElseIf ($this->water_mark_pos = = 2)
  201. {
  202. $left = $dst _width-$this->padding-magickgetimagewidth ($magic _water_handle);
  203. $top = $this->padding;
  204. }
  205. ElseIf ($this->water_mark_pos = = 3)
  206. {
  207. $left = $this->padding;
  208. $top = $dst _height-$this->padding-magickgetimageheight ($magic _water_handle);
  209. }
  210. ElseIf ($this->water_mark_pos = = 4)
  211. {
  212. $left = $dst _width-$this->padding-magickgetimagewidth ($magic _water_handle);
  213. $top = $dst _height-$this->padding-magickgetimageheight ($magic _water_handle);
  214. }
  215. ElseIf ($this->water_mark_pos = = 5)
  216. {
  217. $left = ($dst _width-magickgetimagewidth ($magic _water_handle))/2;
  218. $top = ($dst _height-magickgetimageheight ($magic _water_handle))/2;
  219. }
  220. Magickcompositeimage ($this->magick_handle, $magic _water_handle,mw_overcompositeop, $left, $top);
  221. }
  222. }
  223. }
  224. }
Copy Code
and Add, 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.