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); }