Add watermarks to images and generate scaling charts using PHP

Source: Internet
Author: User
Add watermarks to images and generate scaling charts using PHP
1. add a watermark to the image
2. generate a new scaling chart

  1. Class Image {
  2. // Watermark configuration items
  3. Private $ wateon;
  4. Private $ waterImg;
  5. Private $ waterPos;
  6. Private $ waterPct;
  7. Private $ waterText;
  8. Private $ waterFont;
  9. Private $ waterTextSize;
  10. Private $ waterTextColor;
  11. Private $ qua;
  12. // Thumbnail configuration items
  13. Private $ thumbWidth;
  14. Private $ thumbHeight;
  15. Private $ thumbType;
  16. Private $ thumbEndfix;
  17. // Constructor
  18. Public function _ construct (){
  19. $ This-> wateon = C ("WATER_ON ");
  20. $ This-> waterImg = C ("WATER_IMG ");
  21. $ This-> waterPos = C ("WATER_POS ");
  22. $ This-> waterPct = C ("WATER_PCT ");
  23. $ This-> waterText = C ("WATER_TEXT ");
  24. $ This-> waterFont = C ("WATER_FONT ");
  25. $ This-> waterTextSize = C ("WATER_TEXT_SIZE ");
  26. $ This-> waterTextColor = C ("WATER_TEXT_COLOR ");
  27. $ This-> qua = C ("WATER_QUA ");
  28. // Scaling chart
  29. $ This-> thumbWidth = C ("THUMB_WIDTH ");
  30. $ This-> thumbHeight = C ("THUMB_HEIGHT ");
  31. $ This-> thumbType = C ("THUMB_TYPE ");
  32. $ This-> thumbEndFix = C ("THUMB_ENDFIX ");
  33. }
  34. /*
  35. * Verify that the image is valid.
  36. */
  37. Private function check ($ img ){
  38. Return is_file ($ img) & getimagesize ($ img) & extension_loaded ("gd ");
  39. }
  40. /*
  41. * Scaling chart
  42. * @ Param string $ img Source image
  43. * @ Param string $ the image stored after the outFile scaling rate
  44. * @ Param int $ thumbWidth shrinkage chart width
  45. * @ Param int $ thumbHeight shrinkage chart height
  46. * @ Param int $ thumbType scaling
  47. */
  48. Public function thumb ($ img, $ outFile = "", $ thumbWidth = "", $ thumbHeight = "", $ thumbType = ""){
  49. If (! $ This-> check ($ img )){
  50. Return false;
  51. }
  52. // Scaling chart processing method
  53. $ ThumbType = $ thumbType? $ ThumbType: $ this-> thumbType;
  54. // Scale chart width
  55. $ ThumbWidth = $ thumbWidth? $ ThumbWidth: $ this-> thumbWidth;
  56. // Scale chart height
  57. $ ThumbHeight = $ thumbHeight? $ ThumbHeight: $ this-> thumbHeight;
  58. // Obtain the source image information
  59. $ ImgInfo = getimagesize ($ img );
  60. // Original image width
  61. $ ImgWidth = $ imgInfo [0];
  62. // Original image height
  63. $ ImgHeight = $ imgInfo [1];
  64. // Obtain the source image type
  65. $ Imgtype = image_type_to_extension ($ imgInfo [2]);
  66. // Obtain the size of the source image and the size of the thumbnail based on different scaling methods)
  67. $ Thumb_size = $ this-> thumbsize ($ imgWidth, $ imgHeight, $ thumbWidth, $ thumbHeight, $ thumbType );
  68. // Create the source image
  69. $ Func = "imagecreatefrom". substr ($ imgtype, 1); // variable function
  70. $ ResImg = $ func ($ img );
  71. // Create a scaling chart
  72. If ($ imgtype = ". gif "){
  73. $ Res_thumb = imagecreate ($ thumb_size [2], $ thumb_size [3]);
  74. } Else {
  75. $ Res_thumb = imagecreatetruecolor ($ thumb_size [2], $ thumb_size [3]);
  76. }
  77. Imagecopyresized ($ res_thumb, $ resImg, 0, 0, 0, $ thumb_size [2], $ thumb_size [3], $ thumb_size [0], $ thumb_size [1]);
  78. $ FileInfo = pathinfo ($ img); // File information
  79. $ OutFile = $ outFile? $ OutFile: $ fileInfo ['filename']. $ this-> thumbEndFix. $ fileInfo ['extension']; // file name
  80. $ OutFile = $ fileInfo ["dirname"]. "/". $ outFile; // add the Directory
  81. $ Func = "image". substr ($ imgtype, 1 );
  82. $ Func ($ res_thumb, $ outFile );
  83. Return $ outFile;
  84. }
  85. Private function thumbSize ($ imgWidth, $ imgHeight, $ thumbWidth, $ thumbHeight, $ thumbType ){
  86. // Scale chart size
  87. $ W = $ thumbWidth;
  88. $ H = $ thumbHeight;
  89. // Original image size
  90. $ Img_w = $ imgWidth;
  91. $ Img_h = $ imgHeight;
  92. Switch ($ thumbType ){
  93. Case 1:
  94. // Fixed width and auto-increment height
  95. $ H = $ w/$ imgWidth * $ imgHeight;
  96. Break;
  97. Case 2: // fixed height, self-width
  98. $ W = $ h/$ imgHeight * $ imgWidth;
  99. Break;
  100. Case 3:
  101. If ($ imgHeight/$ thumbHeight> $ imgWidth/$ thumbWidth ){
  102. $ Img_h = $ imgWidth/$ thumbWidth * $ thumbHeight;
  103. } Else {
  104. $ Img_w = $ imgHeight/$ thumbHeight * $ thumbWidth;
  105. }
  106. }
  107. Return array ($ img_w, $ img_h, $ w, $ h );
  108. }
  109. /*
  110. * @ Param string $ img Source image
  111. * @ Param string $ image generated after adding the watermark to outImg
  112. * @ Param int $ pos Watermark Position
  113. * @ Param int $ pct transparency
  114. * @ Param text $ text watermark text
  115. * @ Param string $ waterImg watermark image
  116. */
  117. Public function water ($ img, $ outImg = null, $ pos = "", $ pct = "", $ text = "", $ waterImg = "", $ textColor = ""){
  118. If (! $ This-> check ($ img )){
  119. Return false;
  120. }
  121. // The image generated after the watermark is added
  122. $ OutImg = $ outImg? $ OutImg: $ img;
  123. // Watermark Position
  124. $ Pos = $ pos? $ Pos: $ this-> waterPos;
  125. // Transparency
  126. $ Pct = $ pct? $ Pct: $ this-> waterPct;
  127. // Watermark text
  128. $ Text = $ text? $ Text: $ this-> waterText;
  129. // Watermark image
  130. $ WaterImg = $ waterImg? $ WaterImg: $ this-> waterImg;
  131. // Verify the watermark image
  132. $ WaterImgOn = $ this-> check ($ waterImg );
  133. // Watermark text color
  134. $ TextColor = $ textColor? $ TextColor: $ this-> waterTextColor;
  135. // Original image information
  136. $ ImgInfo = getimagesize ($ img );
  137. // Original image width
  138. $ ImgWidth = $ imgInfo [0];
  139. // Original image height
  140. $ ImgHeight = $ imgInfo [1];
  141. Switch ($ imgInfo [2]) {
  142. Case 1:
  143. $ ResImg = imagecreatefromgif ($ img );
  144. Break;
  145. Case 2:
  146. $ ResImg = imagecreatefromjpeg ($ img );
  147. Break;
  148. Case 3:
  149. $ ResImg = imagecreatefrompng ($ img );
  150. Break;
  151. }
  152. If ($ waterImgOn) {// watermark image valid
  153. // Watermark information
  154. $ WaterInfo = getimagesize ($ waterImg );
  155. // Watermark width
  156. $ WaterWidth = $ waterInfo [0];
  157. // Watermark height
  158. $ WaterHeight = $ waterInfo [1];
  159. // Create different types of gif jpeg png based on different situations
  160. $ W_img = null;
  161. Switch ($ waterInfo [2]) {
  162. Case 1:
  163. $ W_img = imagecreatefromgif ($ waterImg );
  164. Break;
  165. Case 2:
  166. $ W_img = imagecreatefromjpeg ($ waterImg );
  167. Break;
  168. Case 3:
  169. $ W_img = imagecreatefrompng ($ waterImg );
  170. }
  171. } Else {// The watermark image is invalid. use a text watermark.
  172. If (empty ($ text) | strlen ($ textColor )! = 7 ){
  173. Return false;
  174. }
  175. // Obtain the text watermark box information
  176. $ TextInfo = imagettfbbox ($ this-> waterTextSize, 0, $ this-> waterFont, $ text );
  177. // Text information width
  178. $ TextWidth = $ textInfo [2]-$ textInfo [6];
  179. // Text height
  180. $ TextHeight = $ textInfo [3]-$ textInfo [7];
  181. }
  182. // Watermark Position
  183. $ X = $ y = 20;
  184. Switch ($ pos ){
  185. Case 1:
  186. Break;
  187. Case 2:
  188. $ X = ($ imgWidth-$ waterWidth)/2;
  189. Break;
  190. Case 3:
  191. $ Y = $ imgWidth-$ waterWidth-10;
  192. Break;
  193. Case 4:
  194. $ X = ($ imgHeight-$ waterHeight)/2;
  195. Break;
  196. Case 5:
  197. $ X = ($ imgWidth-$ waterWidth)/2;
  198. $ Y = ($ imgHeight-$ waterHeight)/2;
  199. Break;
  200. Case 6:
  201. $ X = $ imgWidth-$ waterWidth-10;
  202. $ Y = ($ imgHeight-$ waterHeight)/2;
  203. Break;
  204. Case 7:
  205. $ X = $ imgHeight-$ waterHeight-10;
  206. Break;
  207. Case 8:
  208. $ X = ($ imgWidth-$ waterWidth)/2;
  209. $ Y = $ imgHeight-$ waterHeight-10;
  210. Break;
  211. Case 9:
  212. $ X = $ imgWidth-$ waterWidth-10;
  213. $ Y = $ imgHeight-$ waterHeight-10;
  214. Break;
  215. Default:
  216. $ X = mt_rand (20, $ imgWidth-$ waterWidth );
  217. $ Y = mt_rand (20, $ imgHeight-$ waterHeight );
  218. }
  219. If ($ waterImgOn) {// add a watermark as an image when the watermark image is valid
  220. If ($ waterInfo [2] = 3 ){
  221. Imagecopy ($ resImg, $ w_img, $ x, $ y, 0, $ waterWidth, $ waterHeight );
  222. } Else {
  223. Imagecopymerge ($ resImg, $ w_img, $ x, $ y, 0, $ waterInfo, $ waterHeight, $ pct );
  224. }
  225. } Else {// The watermark image is invalid. add it as a text watermark.
  226. $ Red = hexdec (substr ($ this-> waterTextColor, 1, 2 ));
  227. $ Greem = hexdec (substr ($ this-> waterTextColor, 3, 2 ));
  228. $ Blue = hexdec (substr ($ this-> waterTextColor, 5, 2 ));
  229. $ Color = imagecolorallocate ($ resImg, $ red, $ greem, $ blue );
  230. Imagettftext ($ resImg, $ this-> waterTextSize, 0, $ x, $ y, $ color, $ this-> waterFont, $ text );
  231. }
  232. // Output image
  233. Switch ($ imgInfo [2]) {
  234. Case 1:
  235. Imagegif ($ resImg, $ outImg );
  236. Break;
  237. Case 2:
  238. Imagejpeg ($ resImg, $ outImg );
  239. Break;
  240. Case 3:
  241. Imagepng ($ resImg, $ outImg );
  242. Break;
  243. }
  244. // Garbage collection
  245. If (isset ($ resImg )){
  246. Imagedestroy ($ resImg );
  247. }
  248. If (isset ($ w_img )){
  249. Imagedestroy ($ w_img );
  250. }
  251. Return true;
  252. }
  253. }
  254. ?>


  1. Return array (
  2. // Watermark processing
  3. "WATER_ON" => 1, // watermark switch
  4. "WATER_IMG" => "./data/logo.png", // watermark image
  5. "WATER_POS" => 9, // Watermark Position
  6. "WATER_PCT" => 80, // watermark transparency
  7. "WATER_TEXT" => "http://www.caoxiaobin.cn ",
  8. "WATER_FONT" => "./data/simsunb. ttf", // watermark font
  9. "WATER_TEXT_COLOR" => "#333333", // hexadecimal representation of text color
  10. "WATER_TEXT_SIZE" => 16, // text size
  11. "WATER_QUA" => 80, // Image compression ratio
  12. // Thumbnail
  13. "THUMB_WIDTH" => 150, // scaling chart width
  14. "THUMB_HEIGHT" => 150, // Thumbnail height
  15. "THUMB_TYPE" => 1, // fixed width of thumbnail processing 1, fixed height auto-Increment 2, fixed width auto-increment // the size of the thumbnail remains unchanged, and the source image is cropped
  16. "THUMB_ENDFIX" => "_ thmub" // Thumbnail suffix
  17. );
  18. ?>


  1. /*
  2. * Case-insensitive data key detection
  3. */
  4. Function array_key_exists_d ($ key, $ arr ){
  5. $ _ Key = strtolower ($ key );
  6. Foreach ($ arr as $ k =>$ v ){
  7. If ($ _ key = strtolower ($ k )){
  8. Return true;
  9. }
  10. }
  11. }
  12. /*
  13. * Recursively change the KEY (KEY name) of the array)
  14. * @ Param array;
  15. * @ Stat int 0 lowercase 1 uppercase
  16. */
  17. Function array_change_key_case_d ($ arr, $ stat = 0 ){
  18. $ Func = $ stat? "Strtoupper": "strtolower ";
  19. $ _ NewArr = array ();
  20. If (! Is_array ($ arr) | empty ($ arr )){
  21. Return $ _ newArr;
  22. }
  23. Foreach ($ arr as $ k =>$ v ){
  24. $ _ K = $ func ($ k); // convert the case sensitivity of a KEY using a variable function
  25. $ _ NewArr [$ _ k] = is_array ($ v )? Array_change_key_case_d ($ v): $ v;
  26. }
  27. Return $ _ newArr;
  28. }
  29. /*
  30. * Read and set configuration items
  31. * @ Param $ name the void configuration item name. if not specified, all configuration items are returned.
  32. * @ Param $ value the value of the void configuration item
  33. * @ Param $ value: false null. only the $ name value is used.
  34. */
  35. Function C ($ name = null, $ value = null ){
  36. Static $ config = array (); // static variable $ config stores all configuration items
  37. If (is_null ($ name )){
  38. Return $ config;
  39. }
  40. // If $ name is an array
  41. If (is_array ($ name )){
  42. Return $ config = array_merge ($ config, array_change_key_case_d ($ name, 1 ));
  43. }
  44. // If $ name is a string, no value in $ value indicates that the value of the configuration item is obtained, and a value indicates that the configuration item is changed.
  45. If (is_string ($ name )){
  46. $ Name = strtoupper ($ name );
  47. // Obtain the configuration item value
  48. If (is_null ($ value )){
  49. Return array_key_exists_d ($ name, $ config )? $ Config [$ name]: null;
  50. } Else {
  51. // Set the value
  52. $ Config [$ name] = $ value;
  53. Return true;
  54. }
  55. }
  56. }


PHP, scaling chart

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.