亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home Backend Development PHP Tutorial PHP code to add watermark to multiple image uploads

PHP code to add watermark to multiple image uploads

Jul 25, 2016 am 08:51 AM

  1. //php watermark function
  2. function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000 ")
  3. {
  4. $isWaterImage = FALSE;
  5. $formatMsg = "This file format is not supported yet. Please use image processing software to convert the image to GIF, JPG, or PNG format.";
  6. //Read the watermark file
  7. if(!empty($waterImage) && file_exists($waterImage))
  8. {
  9. $isWaterImage = TRUE;
  10. $water_info = getimagesize($waterImage); //What you get is an array
  11. $water_w = $water_info[0];//Get the width of the watermark image
  12. $water_h = $water_info[1];//Get the height of the watermark image
  13. switch($water_info[2])//Get the format of the watermark image
  14. {
  15. case 1:$water_im = imagecreatefromgif($waterImage);break; //Convert the image into an encoding process that can be recognized by PHP
  16. case 2:$water_im = imagecreatefromjpeg($waterImage);break; //Convert the image into PHP-recognizable encoding process
  17. case 3:$water_im = imagecreatefrompng($waterImage);break; //Convert the image into PHP-recognizable encoding process
  18. default:die($formatMsg);
  19. }
  20. }
  21. //Read Get the background image
  22. if(!empty($groundImage) && file_exists($groundImage))
  23. {
  24. $ground_info = getimagesize($groundImage);
  25. $ground_w = $ground_info[0];//Get the width of the background image
  26. $ ground_h = $ground_info[1]; //Get the height of the background image
  27. switch($ground_info[2]) //Get the format of the background image
  28. {
  29. case 1:$ground_im = imagecreatefromgif($groundImage);break;
  30. case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
  31. case 3:$ground_im = imagecreatefrompng($groundImage);break;
  32. default:die($formatMsg);
  33. }
  34. }
  35. else
  36. {
  37. die("required The watermarked picture does not exist! ");
  38. }
  39. //Watermark position
  40. if($isWaterImage)//Picture watermark
  41. {
  42. $w = $water_w;
  43. $h = $water_h;
  44. $label = "Picture";
  45. }
  46. else/ /Text watermark
  47. {
  48. $temp = imagettfbbox(ceil($textFont*2.5),0,"c:/windows/fonts/stcaiyun.ttf",$waterText);//Get the range of text using TrueType fonts
  49. $ w = $temp[2] - $temp[6];
  50. $h = $temp[3] - $temp[7];
  51. unset($temp);
  52. $label = "text area";
  53. }
  54. if ( ($ground_w<$w) || ($ground_h<$h) )
  55. {
  56. echo "The length or width of the image that needs to be watermarked is smaller than the watermark ".$label.", and the watermark cannot be generated! ";
  57. return;
  58. }
  59. switch($waterPos)
  60. {
  61. case 0://random
  62. $posX = rand(0,($ground_w - $w));
  63. $posY = rand(0,($ground_h - $h));
  64. break;
  65. case 1://1 means top left
  66. $posX = 0;
  67. $posY = 0;
  68. break;
  69. case 2://2 means top center
  70. $posX = ($ ground_w - $w) / 2;
  71. $posY = 0;
  72. break;
  73. case 3://3 is the top right
  74. $posX = $ground_w - $w;
  75. $posY = 0;
  76. break;
  77. case 4 ://4 means center left
  78. $posX = 0;
  79. $posY = ($ground_h - $h) / 2;
  80. break;
  81. case 5://5 means center center
  82. $posX = ($ground_w - $w ) / 2;
  83. $posY = ($ground_h - $h) / 2;
  84. break;
  85. case 6://6 is the middle right
  86. $posX = $ground_w - $w;
  87. $posY = ($ground_h - $h) / 2;
  88. break;
  89. case 7://7 is bottom left
  90. $posX = 0;
  91. $posY = $ground_h - $h;
  92. break;
  93. case 8://8 is bottom center
  94. $posX = ($ground_w - $w) / 2;
  95. $posY = $ground_h - $h;
  96. break;
  97. case 9://9 is bottom right
  98. $posX = $ground_w - $w;
  99. $posY = $ground_h - $h;
  100. break;
  101. default://random
  102. $posX = rand(0,($ground_w - $w));
  103. $posY = rand(0,($ground_h - $h) ; posY, 0, 0, $water_w,$water_h);//Copy watermark to target file
  104. }
  105. else//Text watermark
  106. {
  107. if( !empty($textColor) && (strlen($textColor)==7) )
  108. {
  109. $R = hexdec(substr($textColor,1,2));
  110. $G = hexdec(substr($textColor,3,2));
  111. $B = hexdec(substr($textColor,5) );
  112. }
  113. else
  114. {
  115. die("The watermark text color format is incorrect!");
  116. }
  117. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  118. }
  119. //The image after generating the watermark
  120. @unlink($groundImage);
  121. switch($ground_info[2])//Get the format of the background image
  122. {
  123. case 1:imagegif($ground_im,$groundImage);break; //Create an image in gif format
  124. case 2 :imagejpeg($ground_im,$groundImage);break; //Create a picture in jpeg format
  125. case 3:imagepng($ground_im,$groundImage);break; //Create a picture in png format
  126. default:die($errorMsg);
  127. }
  128. //Release memory
  129. if(isset($water_info)) unset($water_info);
  130. if(isset($water_im)) imagedestroy($water_im);
  131. unset($ground_info);
  132. imagedestroy($ground_im) ;
  133. }
  134. ?>
  135. php image upload code:
  136. for ($i=0;$i {
  137. $upfile="./img/".($i+1).".png";//Change the path here to yours
  138. if(move_uploaded_file($_FILES['userfile']['tmp_name'] [$i],$upfile)){
  139. imageWaterMark($upfile,9,"./shuiyin.png","Made By Chenduan",5,"#FF0000");
  140. /*
  141. * Function: Image watermark ( Watermark supports pictures or text)
  142. * imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000")
  143. * Parameters:
  144. * $ groundImage background image, that is, the image that needs to be watermarked, currently only supports GIF, JPG, and PNG formats;
  145. * $waterPos watermark position, there are 10 states, 0 is a random position;
  146. * 1 means top left, 2 means top center, 3 means top is on the right;
  147. * 4 is middle on the left, 5 is middle on the center, 6 is middle on the right;
  148. * 7 is bottom on the left, 8 is bottom on the center, 9 is bottom on the right;
  149. * $waterImage picture Watermark, that is, the image used as a watermark, currently only supports GIF, JPG, and PNG formats;
  150. * $waterText text watermark, that is, text is used as a watermark, supports ASCII code, does not support Chinese;
  151. * $textFont text size, value is 1 , 2, 3, 4 or 5, the default is 5;
  152. * $textColor text color, the value is a hexadecimal color value, the default is #FF0000 (red);
  153. */
  154. echo "";
  155. echo "No.".($i+1)."Picture operation successful
    ";
  156. }
  157. else{
  158. echo "No.". ($i+1)."Unable to upload picture
    ";
  159. }
  160. }
  161. ?>
Copy code


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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

Hot Topics

PHP Tutorial
1488
72
PHP Variable Scope Explained PHP Variable Scope Explained Jul 17, 2025 am 04:16 AM

Common problems and solutions for PHP variable scope include: 1. The global variable cannot be accessed within the function, and it needs to be passed in using the global keyword or parameter; 2. The static variable is declared with static, and it is only initialized once and the value is maintained between multiple calls; 3. Hyperglobal variables such as $_GET and $_POST can be used directly in any scope, but you need to pay attention to safe filtering; 4. Anonymous functions need to introduce parent scope variables through the use keyword, and when modifying external variables, you need to pass a reference. Mastering these rules can help avoid errors and improve code stability.

How to handle File Uploads securely in PHP? How to handle File Uploads securely in PHP? Jul 08, 2025 am 02:37 AM

To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.

Commenting Out Code in PHP Commenting Out Code in PHP Jul 18, 2025 am 04:57 AM

There are three common methods for PHP comment code: 1. Use // or # to block one line of code, and it is recommended to use //; 2. Use /.../ to wrap code blocks with multiple lines, which cannot be nested but can be crossed; 3. Combination skills comments such as using /if(){}/ to control logic blocks, or to improve efficiency with editor shortcut keys, you should pay attention to closing symbols and avoid nesting when using them.

How Do Generators Work in PHP? How Do Generators Work in PHP? Jul 11, 2025 am 03:12 AM

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

Tips for Writing PHP Comments Tips for Writing PHP Comments Jul 18, 2025 am 04:51 AM

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

Quick PHP Installation Tutorial Quick PHP Installation Tutorial Jul 18, 2025 am 04:52 AM

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre

How to access a character in a string by index in PHP How to access a character in a string by index in PHP Jul 12, 2025 am 03:15 AM

In PHP, you can use square brackets or curly braces to obtain string specific index characters, but square brackets are recommended; the index starts from 0, and the access outside the range returns a null value and cannot be assigned a value; mb_substr is required to handle multi-byte characters. For example: $str="hello";echo$str[0]; output h; and Chinese characters such as mb_substr($str,1,1) need to obtain the correct result; in actual applications, the length of the string should be checked before looping, dynamic strings need to be verified for validity, and multilingual projects recommend using multi-byte security functions uniformly.

Learning PHP: A Beginner's Guide Learning PHP: A Beginner's Guide Jul 18, 2025 am 04:54 AM

TolearnPHPeffectively,startbysettingupalocalserverenvironmentusingtoolslikeXAMPPandacodeeditorlikeVSCode.1)InstallXAMPPforApache,MySQL,andPHP.2)Useacodeeditorforsyntaxsupport.3)TestyoursetupwithasimplePHPfile.Next,learnPHPbasicsincludingvariables,ech

See all articles