PHP image watermarking code example parsing _ PHP Tutorial

Source: Internet
Author: User
Tags imagecopy imagejpeg
PHP image watermark code example parsing. When you need to upload images to a regular website, you often need to add the LOGO watermark of your website to the image. So how to implement this step? First of all, let's take a look at a regular website. when you need to upload images, you often need to add LOGO watermarks to your website. So how to implement this step? First, let's get to know

Create an image by determining the file type, copy it to the original image, fill it with and create a rectangle for writing it into imagestring (), or determine the watermark type in the original image program: one is a string, and the other is to add a graphic object to it. Below is a PHP image watermark reprinted!

Parameter description:

$ Max_file_size: size limit of the uploaded file, in bytes
$ Destination_folder: File Upload path
$ Watermark: whether to add a watermark. (1 indicates adding a watermark; others indicates not adding a watermark );

PHP image watermark instructions:

1. remove the number before the line "extension = php_gd2.dll" in the PHP. ini file because the GD library is used;
2. change extension_dir = to the directory where your php_gd2.dll is located;

PHP image watermarking code example:

 
 
  1. // Upload file type list
  2. $ Uptypes = array (
  3. 'Image/jpg ',
  4. 'Image/jpeg ',
  5. 'Image/png ',
  6. 'Image/pjpeg ',
  7. 'Image/GIF ',
  8. 'Image/bmp ',
  9. 'Image/x-png'
  10. );
  11. $ Max_file_size = 2000000; // size limit of uploaded files, in bytes
  12. $ Destination_folder = "uploadimg/"; // file Upload path
  13. $ Watermark = 1; // whether to add a watermark. (1 indicates adding a watermark. otherwise, no watermark is added );
  14. $ Watertype = 1; // watermark type (1 is text, 2 is image)
  15. $ Waterposition = 1; // watermark position (1 indicates the lower left corner and 2 indicates the lower right corner
  16. , 3 is the upper left corner, 4 is the upper right corner, and 5 is the center );
  17. $ Waterstring = "<A href =" http://www.xplore.cn/">
  18. Http://www.xplore.cn/</A> "; // watermark string
  19. $ Waterimg = "xplore.gif"; // watermark image
  20. $ Imgpreview = 1; // whether to generate a preview image (1 is generated, others are not generated );
  21. $ Imgpreviewsize = 1/2; // Thumbnail ratio
  22. ?>
  23. <Html>
  24. <Head>
  25. <Title> ZwelL image upload program
  26. <Style type = "text/css">
  27. </Style>
  28. </Head>
  29. <Body>
  30. <Form enctype = "multipart/form-data"
  31. Method = "post" name = "upform">
  32. Upload files:
  33. <Input type = "submit" value = "upload"> <br>
  34. The file types that can be uploaded are:
  35. </Form>
  36. <? Php
  37. If ($ _ SERVER ['request _ method'] = 'post ')
  38. {
  39. If (! Is_uploaded_file ($ _ FILES ["upfile"]
  40. [Tmp_name])
  41. // Whether a file exists
  42. {
  43. Echo "the image does not exist! ";
  44. Exit;
  45. }
  46. $ File = $ _ FILES ["upfile"];
  47. If ($ max_file_size <$ file ["size"])
  48. // Check the file size
  49. {
  50. Echo "the file is too large! ";
  51. Exit;
  52. }
  53. If (! In_array ($ file ["type"], $ uptypes ))
  54. // Check the file type
  55. {
  56. Echo "file type does not match! ". $ File [" type "];
  57. Exit;
  58. }
  59. If (! File_exists ($ destination_folder ))
  60. {
  61. Mkdir ($ destination_folder );
  62. }
  63. $ Filename = $ file ["tmp_name"];
  64. $ Image_size = getimagesize ($ filename );
  65. $ Pinfo = pathinfo ($ file ["name"]);
  66. $ Ftype = $ pinfo ['extension'];
  67. $ Destination = $ destination_folder.
  68. Time (). ".". $ ftype;
  69. If (file_exists ($ destination )&&
  70. $ Overwrite! = True)
  71. {
  72. Echo "a file with the same name already exists ";
  73. Exit;
  74. }
  75. If (! Move_uploaded_file ($ filename,
  76. $ Destination ))
  77. {
  78. Echo "an error occurred while moving the file ";
  79. Exit;
  80. }
  81. $ Pinfo = pathinfo ($ destination );
  82. $ Fname = $ pinfo [basename];
  83. Echo"Uploaded successfully
  84. </Font>
    File name:
  85. <Font color = blue> ". $ destination_folder.
  86. $ Fname. "</font> <br> ";
  87. Echo "width:". $ image_size [0];
  88. Echo "length:". $ image_size [1];
  89. Echo"
    Size: ". $ file [" size "]." bytes ";
  90. If ($ watermark = 1)
  91. {
  92. $ Iinfo = getimagesize ($ destination, $ iinfo );
  93. $ Nimage = imagecreatetruecolor ($ image_size [0]
  94. , $ Image_size [1]);
  95. $ White = imagecolorallocate ($ nimage, 255,255,255 );
  96. $ Black = imagecolorallocate ($ nimage, 0, 0 );
  97. $ Red = imagecolorallocate ($ nimage, 255, 0, 0 );
  98. Imagefill ($ nimage, 0, 0, $ white );
  99. Switch ($ iinfo [2])
  100. {
  101. Case 1:
  102. $ Simage = imagecreatefromgif ($ destination );
  103. Break;
  104. Case 2:
  105. $ Simage = imagecreatefromjpeg ($ destination );
  106. Break;
  107. Case 3:
  108. $ Simage = imagecreatefrompng ($ destination );
  109. Break;
  110. Case 6:
  111. $ Simage = imagecreatefromwbmp ($ destination );
  112. Break;
  113. Default:
  114. Die ("unsupported file types ");
  115. Exit;
  116. }
  117. Imagecopy ($ nimage, $ simage, 0, 0, 0,
  118. $ Image_size [0], $ image_size [1]);
  119. Imagefilledrectangle ($ nimage, 1,
  120. $ Image_size [1]-15, 80, $ image_size [1], $ white );
  121. Switch ($ watertype)
  122. {
  123. Case 1: // add a watermark string
  124. Imagestring ($ nimage, 2, 3, $ image_size [1]-15,
  125. $ Waterstring, $ black );
  126. Break;
  127. Case 2: // add a watermark image
  128. $ Simage1 = imagecreatefromgif ("xplore.gif ");
  129. Imagecopy ($ nimage, $ simage1 );
  130. Imagedestroy ($ simage1 );
  131. Break;
  132. }
  133. Switch ($ iinfo [2])
  134. {
  135. Case 1:
  136. // Imagegif ($ nimage, $ destination );
  137. Imagejpeg ($ nimage, $ destination );
  138. Break;
  139. Case 2:
  140. Imagejpeg ($ nimage, $ destination );
  141. Break;
  142. Case 3:
  143. Imagepng ($ nimage, $ destination );
  144. Break;
  145. Case 6:
  146. Imagewbmp ($ nimage, $ destination );
  147. // Imagejpeg ($ nimage, $ destination );
  148. Break;
  149. }
  150. // Overwrite the original uploaded File
  151. Imagedestroy ($ nimage );
  152. Imagedestroy ($ simage );
  153. }
  154. If ($ imgpreview = 1)
  155. {
  156. Echo "<br> image preview:
    ";
  157. Echo "<ccid_file values =" "width = ".
  158. ($ Image_size [0] * $ imgpreviewsize )."
  159. Height = ". ($ image_size [1] * $ imgpreviewsize );"
  160. Echo "alt =" image preview: r File name :".
  161. $ Destination. "r Upload Time:"/> ";
  162. }
  163. }
  164. ?>
  165. </Body>
  166. </Html>

The above code example shows how to add watermarks to PHP images.


Bytes. So how to implement this step? First let's get to know through...

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.