Picture Shrink print PHP class

Source: Internet
Author: User
  1. /**
  2. * Picture shrinkage Water printing type
  3. *
  4. */
  5. Class Cls_photo
  6. {
  7. protected $waterrate = 0.2; The scale of the watermark icon on the picture
  8. protected $width = 300; Thumbnail default width
  9. protected $height = 200; Thumbnail default height
  10. protected $padding = 5; The distance from the watermark graph to the edge
  11. protected $water _mark = "./water.png";
  12. 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)
  13. Protected $watermode = 0;//0 thumbnail does not hit watermark 1 thumbnail when drawing water
  14. Protected $magick _handle;//Picture manipulation handle
  15. Protected $format = array (' jpg ', ' gif ', ' PNG ', ' jpeg '); Picture file format Qualification
  16. protected $smallpic _mode = 2;//default mode 0 is not to generate thumbnails, 1 for crop scaling, 2 for scaling 3 for scale fill mode
  17. /**
  18. * Set Picture class parameters
  19. *
  20. * @param $arg Picture parameters can be placed in the array several times as follows
  21. * @param $protected parameter values
  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. * Image Zoom
  61. *
  62. * @param $src _file source file path
  63. * @param $dst _file destination file path
  64. * @return thumbnail image 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. Crop Zoom mode
  84. if ($mode = = 1)
  85. {
  86. $pos _x= $pos _y = 0;//trim Temporary position
  87. $src _widthc = $src _width;//Cut temporary width
  88. $src _HEIGHTC = $src _height;//Cut 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);//Cut
  100. Because the Gif image changes after the Magickcropimage function, the canvas does not change
  101. $this->magick_handle = Newmagickwand ();
  102. Magicknewimage ($this->magick_handle, $src _widthc, $src _heightc, ' #ffffff ');
  103. Magicksetformat ($this->magick_handle, $srcext);
  104. Magickcompositeimage ($this->magick_handle, $magic _water_handle,mw_overcompositeop,0,0);
  105. Scaling
  106. Magickscaleimage ($this->magick_handle, $dst _width, $dst _height);
  107. }
  108. Proportional Zoom 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;//Replacement
  120. Magickscaleimage ($this->magick_handle, $dst _width, $dst _height);//Zoom
  121. }
  122. Scale fill 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,mw_overcompositeop, ($dst _width-$dst _widthc)/2 , ($dst _height-$dst _heightc)/2);
  140. }
  141. Play 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. * Water Stamp
  156. *
  157. * @param $src _file The image path to be watermarked
  158. * @param $dst _file production watermark File save path, empty the production of random temporary files
  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. * Give the picture a water stamp
  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. Working with Watermark Maps
  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 ($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
  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. }
Copy Code
Php
  • Related Article

    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.