Php text watermark and php image watermark code example

Source: Internet
Author: User
Tags imagecopy
Php text watermark and php image watermark code example

  1. $ Dst_path = 'dst.jpg ';
  2. // Create an image instance
  3. $ Dst = imagecreatefromstring (file_get_contents ($ dst_path ));
  4. // Text
  5. $ Font = './simsun. ttc'; // font
  6. $ Black = imagecolorallocate ($ dst, 0x00, 0x00, 0x00); // font color
  7. Imagefttext ($ dst, 13, 0, 20, 20, $ black, $ font, 'Happy Project ');
  8. // Output image
  9. List ($ dst_w, $ dst_h, $ dst_type) = getimagesize ($ dst_path );
  10. Switch ($ dst_type ){
  11. Case 1: // GIF
  12. Header ('content-Type: image/gif ');
  13. Imagegif ($ dst );
  14. Break;
  15. Case 2: // JPG
  16. Header ('content-Type: image/jpeg ');
  17. Imagejpeg ($ dst );
  18. Break;
  19. Case 3: // PNG
  20. Header ('content-Type: image/png ');
  21. Imagepng ($ dst );
  22. Break;
  23. Default:
  24. Break;
  25. }
  26. Imagedestroy ($ dst );

Example 2,Php image watermarkImage watermark: adds an image to another image. it mainly uses imagecopy and imagecopymerge of the gd Library.

:

Code:

  1. $ Dst_path = 'dst.jpg ';
  2. $ Src_path = 'src.jpg ';
  3. // Create an image instance
  4. $ Dst = imagecreatefromstring (file_get_contents ($ dst_path ));
  5. $ Src = imagecreatefromstring (file_get_contents ($ src_path ));
  6. // Obtain the width and height of the watermark image
  7. List ($ src_w, $ src_h) = getimagesize ($ src_path );
  8. // Copy the watermark image to the target image. the last parameter 50 is to set transparency. The transparency is achieved here.
  9. Imagecopymerge ($ dst, $ src, 10, 10, 0, 0, $ src_w, $ src_h, 50 );
  10. // If the watermark image is transparent, use the imagecopy method.
  11. // Imagecopy ($ dst, $ src, 10, 10, 0, 0, $ src_w, $ src_h );
  12. // Output image
  13. List ($ dst_w, $ dst_h, $ dst_type) = getimagesize ($ dst_path );
  14. Switch ($ dst_type ){
  15. Case 1: // GIF
  16. Header ('content-Type: image/gif ');
  17. Imagegif ($ dst );
  18. Break;
  19. Case 2: // JPG
  20. Header ('content-Type: image/jpeg ');
  21. Imagejpeg ($ dst );
  22. Break;
  23. Case 3: // PNG
  24. Header ('content-Type: image/png ');
  25. Imagepng ($ dst );
  26. Break;
  27. Default:
  28. Break;
  29. }
  30. Imagedestroy ($ dst );
  31. Imagedestroy ($ src );

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.