Home Backend Development PHP Tutorial Image scaling watermark PHP class

Image scaling watermark PHP class

Jul 25, 2016 am 08:46 AM

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

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 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.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

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.

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.

What is REST API design principles? What is REST API design principles? Apr 04, 2025 am 12:01 AM

RESTAPI design principles include resource definition, URI design, HTTP method usage, status code usage, version control, and HATEOAS. 1. Resources should be represented by nouns and maintained at a hierarchy. 2. HTTP methods should conform to their semantics, such as GET is used to obtain resources. 3. The status code should be used correctly, such as 404 means that the resource does not exist. 4. Version control can be implemented through URI or header. 5. HATEOAS boots client operations through links in response.

How do you handle exceptions effectively in PHP (try, catch, finally, throw)? How do you handle exceptions effectively in PHP (try, catch, finally, throw)? Apr 05, 2025 am 12:03 AM

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

What are anonymous classes in PHP and when might you use them? What are anonymous classes in PHP and when might you use them? Apr 04, 2025 am 12:02 AM

The main function of anonymous classes in PHP is to create one-time objects. 1. Anonymous classes allow classes without names to be directly defined in the code, which is suitable for temporary requirements. 2. They can inherit classes or implement interfaces to increase flexibility. 3. Pay attention to performance and code readability when using it, and avoid repeatedly defining the same anonymous classes.

See all articles