PHP text watermark and PHP image watermark code example

Source: Internet
Author: User
Tags imagecopy imagejpeg transparent color
    1. $dst _path = ' dst.jpg ';
    2. Create an instance of a picture
    3. $DST = imagecreatefromstring (file_get_contents ($DST _path));
    4. Make a text.
    5. $font = './SIMSUN.TTC ';//font
    6. $black = Imagecolorallocate ($DST, 0x00, 0x00, 0x00);//Font Color
    7. Imagefttext ($DST, 0, $black, $font, ' happy programming ');
    8. Output picture
    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);
Copy Code

Example 2,PHP image watermark image watermark, a picture is added to another picture, mainly using the GD library imagecopy and Imagecopymerge.

Code:

  1. $dst _path = ' dst.jpg ';
  2. $src _path = ' src.jpg ';
  3. Create an instance of a picture
  4. $DST = imagecreatefromstring (file_get_contents ($DST _path));
  5. $SRC = imagecreatefromstring (file_get_contents ($src _path));
  6. Get the width height of the watermark picture
  7. List ($src _w, $src _h) = getimagesize ($src _path);
  8. The watermark picture is copied to the target image, and the last parameter 50 is the set transparency, which enables the translucent effect
  9. Imagecopymerge ($DST, $SRC, ten, 0, 0, $src _w, $src _h, 50);
  10. If the watermark picture itself has a transparent color, use the Imagecopy method
  11. Imagecopy ($DST, $SRC, ten, 0, 0, $src _w, $src _h);
  12. Output picture
  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);
Copy Code
  • Related Article

    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.