PHP Multi-Image Merge method sharing

Source: Internet
Author: User
Tags imagecopy transparent watermark
This article is mainly to share with you php multiple image merge method, PHP Imagecopymerge function can support two image overlay, set the transparency of the overlay, imagecopy function does not support overlay transparency, in fact, PHP internal source code, Imagecopymerge calls the Imagecopy function directly when the transparency parameter is 100.
However, the Imagecopy function copies the original transparency information of the PNG image, while Imagecopymerge does not support the transparent copy of the image itself,
Rather verbose, take a practical example to demonstrate the following:
Mark the image with a logo watermark.
In general, logos are made up of icons and URLs, such as a transparent PNG image, Logo.png,
Now if you want to put this logo on the picture,
Using the Imagecopymerge function, you can achieve a 30% transparent watermark icon, but the logo itself PNG will become like IE6 does not support PNG transparent, the background is not transparent, if you use the Imagecopy function, you can keep the logo itself transparent information, But cannot achieve 30% of the faint watermark overlay,
PHP official implementation of the method: using the Imagecopymerge_alpha function can directly implement the function of the two functions, preserving PNG itself transparent, while the implementation of custom transparency overlay, but the function of internal use $opacity = 100-$opacity; To achieve transparency, as if it were just reversed.

 $DST = Imagecreatefromstring (file_get_contents ($DST _path)); $src = Imagecreatefromstring (file_get_contents ($src _path)) imagecopy ($DST, $SRC, 100, 100, 0, 0, 100, 100);//Complete Merge 
 function Imagecopymerge_alpha ($dst _im, $src _im, $dst _x, $dst _y, $src _x, $src _y,        $src _w, $src _h, $pct) {$opacity = $pct;        Getting the watermark width $w = imagesx ($src _im);                Getting the watermark height $h = imagesy ($src _im);        Creating a cut resource $cut = Imagecreatetruecolor ($src _w, $src _h); Copying that sections of the background to the cut imagecopy ($cut, $dst _im, 0, 0, $dst _x, $dst _y, $src _w, $src _h)        ;                Inverting the opacity $opacity = 100-$opacity;        Placing the watermark now Imagecopy ($cut, $src _im, 0, 0, $src _x, $src _y, $src _w, $src _h);    Imagecopymerge ($dst _im, $cut, $dst _x, $dst _y, $src _x, $src _y, $src _w, $src _h, $opacity); }

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.