Home Backend Development PHP Tutorial PHP class that implements thumbnails and watermarks

PHP class that implements thumbnails and watermarks

Jul 25, 2016 am 08:45 AM

  1. /**
  2. * Image scaling watermark class
  3. *
  4. * @version 1.0;
  5. *
  6. */
  7. class cls_photo
  8. {
  9. protected $waterrate = 0.2; //The ratio of the watermark icon on the image
  10. protected $width = 300; //The default width of the thumbnail
  11. protected $height = 200; //Default height of thumbnail
  12. protected $padding = 5; //Distance from watermark image to edge
  13. protected $water_mark = "./water.png";
  14. 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)
  15. protected $watermode = 0; // 0 does not print watermark when thumbnail 1 prints watermark when thumbnail
  16. protected $magick_handle; //Picture operation handle
  17. protected $format = array('jpg','gif','png','jpeg'); // Picture file format limitation
  18. protected $smallpic_mode = 2; //Default mode 0 is not generated Thumbnail, 1 is crop scaling, 2 is proportional scaling, 3 is scaling fill mode
  19. /**
  20. * Set image parameters
  21. *
  22. * @param $arg Image parameters can be put into the array multiple times as follows
  23. * @param $protected parameter value
  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 scaling
  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(MagicGetImageFormat($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 scaling mode
  86. if($mode == 1)
  87. {
  88. $pos_x=$pos_y = 0;//Crop Cut 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; dst_height/$ dst_width;
  95. $ POS_Y = ($ SRC_HEIGHT -$src_heightc)/2;
  96. }
  97. MagickCropImage($magic_water_handle,$src_widthc,$src_heightc,$pos_x,$pos_y);//Crop
  98. //Because after the MagickCropImage function, the Gif image changes, but the canvas remains unchanged
  99. $ this->magick_handle = NewMagickWand();
  100. MagickNewImage($this->magick_handle,$src_widthc,$src_heightc,'#ffffff');
  101. MagickSetFormat($this->magick_handle,$srcext);
  102. MagickCompositeImage($ this->magick_handle,$magic_water_handle,MW_OverCompositeOp,0,0);
  103. //Scale
  104. MagickScaleImage($this->magick_handle, $dst_width, $dst_height);
  105. }
  106. //Proportional scaling mode
  107. if($ mode == 2)
  108. {
  109. if($src_width/$src_height>$dst_width/$dst_height)
  110. {
  111. $dst_height=$dst_width*$src_height/$src_width;
  112. }
  113. else
  114. {
  115. $dst_width=$dst_height * $src_width/$src_height;
  116. }
  117. $this->magick_handle=$magic_water_handle;//替换
  118. MagickScaleImage($this->magick_handle, $dst_width, $dst_height);//缩放
  119. }
  120. //缩放填充模式
  121. if($mode == 3)
  122. {
  123. if($src_width/$src_height>$dst_width/$dst_height)
  124. {
  125. $dst_heightc=$dst_width*$src_height/$src_width;
  126. $dst_widthc=$dst_width;
  127. }
  128. else
  129. {
  130. $dst_widthc=$dst_height*$src_width/$src_height;
  131. $dst_heightc=$dst_height;
  132. }
  133. MagickScaleImage($magic_water_handle, $dst_widthc, $dst_heightc);//缩放
  134. $this->magick_handle = NewMagickWand();
  135. MagickNewImage($this->magick_handle,$dst_width,$dst_height,$this->smallpic_bgcolor);
  136. MagickSetFormat($this->magick_handle,$srcext);
  137. MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,($dst_width-$dst_widthc)/2,($dst_height-$dst_heightc)/2);
  138. }
  139. //打水印
  140. if($this->watermode == 1)
  141. {
  142. $this->set_mark();
  143. }
  144. if(empty($dst_file))
  145. {
  146. //建立临时文件
  147. $dst_file = tempnam($_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG");
  148. }
  149. MagickWriteImage($this->magick_handle, $dst_file);
  150. return $dst_file;
  151. }
  152. /**
  153. * Watermarking
  154. *
  155. * @param $src_file The path of the image to be watermarked
  156. * @param $dst_file The file saving path for producing watermarks. If it is empty, a random temporary file will be produced
  157. * @return The path of the watermark file/false
  158. */
  159. public function water_mark($src_file,$dst_file="")
  160. {
  161. $this->magick_handle = NewMagickWand();
  162. if (!MagickReadImage($this->magick_handle, $src_file))
  163. return false;
  164. $this->set_mark();
  165. if(empty($dst_file))
  166. {
  167. //建立临时文件
  168. $dst_file = tempnam($_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG");
  169. }
  170. MagickWriteImage($this->magick_handle, $dst_file);
  171. return $dst_file;
  172. }
  173. /**
  174. * Internal interface
  175. * Watermark pictures
  176. *
  177. */
  178. protected function set_mark()
  179. {
  180. //尺寸
  181. $dst_width = MagickGetImageWidth($this->magick_handle);
  182. $dst_height = MagickGetImageHeight($this->magick_handle);
  183. //处理水印图
  184. if ($this->water_mark && is_file($this->water_mark))
  185. {
  186. $magic_water_handle = NewMagickWand();
  187. MagickRemoveImage($magic_water_handle);
  188. if (MagickReadImage($magic_water_handle, $this->water_mark))
  189. {
  190. MagickScaleImage($magic_water_handle, $dst_width*$this->waterrate, $dst_width*$this->waterrate*MagickGetImageHeight($magic_water_handle)/MagickGetImageWidth($magic_water_handle));//缩放水印到图片的1/5
  191. if ($this->water_mark_pos == 1)
  192. {
  193. $left = $this->padding;
  194. $top = $this->padding;
  195. }
  196. elseif ($this->water_mark_pos == 2)
  197. {
  198. $left = $dst_width-$this->padding-MagickGetImageWidth($magic_water_handle);
  199. $top = $this->padding;
  200. }
  201. elseif ($this->water_mark_pos == 3)
  202. {
  203. $left = $this->padding;
  204. $top = $dst_height -$this->padding-MagickGetImageHeight($magic_water_handle);
  205. }
  206. elseif ($this->water_mark_pos == 4)
  207. {
  208. $left = $dst_width-$this->padding-MagickGetImageWidth($magic_water_handle);
  209. $top =$dst_height -$this->padding-MagickGetImageHeight($magic_water_handle);
  210. }
  211. elseif ($this->water_mark_pos == 5)
  212. {
  213. $left = ($dst_width-MagickGetImageWidth($magic_water_handle))/2;
  214. $top =($dst_height -MagickGetImageHeight($magic_water_handle))/2;
  215. }
  216. MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,$left,$top);
  217. }
  218. }
  219. }
  220. }
复制代码

Waka, php


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

See all articles