PHP imagecopy function Add image watermark

Source: Internet
Author: User
Tags imagecopy
  1. Add picture watermark to background image (location random), JPEG for background image, GIF for watermark picture format

  2. function watermark ($filename, $water) {

  3. Get the width and height of the background picture

  4. List ($b _w, $b _h) = getimagesize ($filename);

  5. Get the width and height of a watermark picture

  6. List ($w _w, $w _h) = getimagesize ($water);

  7. The position of the image in the background image is randomly starting position

  8. $posX = rand (0, ($b _w-$w _w));
  9. $posY = rand (0, ($b _h-$w _h));

  10. Create a resource for a background picture

  11. $back = Imagecreatefromjpeg ($filename);

  12. Create a resource for a watermark picture

  13. $water = Imagecreatefromgif ($water);

  14. Use the Imagecopy () function to copy the watermark picture to the location specified in the background picture

  15. Imagecopy ($back, $water, $posX, $posY, 0, 0, $w _w, $w _h);

  16. Save a background picture with a watermark picture

  17. Imagejpeg ($back, $filename);
  18. Imagedestroy ($back);
  19. Imagedestroy ($water);
  20. }

  21. Output watermark Image

  22. Watermark ("Brophp.jpg", "logo.gif");
  23. ?>

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.