Home > Backend Development > PHP Tutorial > How to merge multiple pictures in PHP

How to merge multiple pictures in PHP

小云云
Release: 2023-03-20 13:58:01
Original
2695 people have browsed it

This article mainly shares with you the method of merging multiple images in PHP. The PHP imagecopymerge function can support setting the transparency of the overlay when two images are overlaid. The imagecopy function does not support overlay transparency. In fact, in the PHP internal source code, the transparency of imagecopymerge is When the parameter is 100, the imagecopy function is called directly.
However, the imagecopy function can retain the original transparent information of the png image when copying, but imagecopymerge does not support the transparent copy of the image itself.
It is rather wordy. Let’s demonstrate the following with a practical example:
In Add a LOGO watermark to the image.
Generally speaking, a logo consists of an icon and a URL, such as a transparent png image, logo.png,
Now if you want to put this logo on the picture,
Use the imagecopymerge function to achieve it. A light watermark icon with a transparency of 30%, but the png of the logo itself will become opaque as IE6 does not support png transparency. If you use the imagecopy function, you can retain the transparent information of the logo itself, but it cannot achieve 30% Light watermark overlay,
php official implementation method: use the imagecopymerge_alpha function to directly implement the functions of these two functions, while retaining the transparency of png itself, while achieving custom transparency overlay, but the internal use of this function is $opacity = 100 - $opacity; to achieve transparency, it seems to be just the opposite

$dst = imagecreatefromstring(file_get_contents($dst_path));
$src = imagecreatefromstring(file_get_contents($src_path));
imagecopy($dst, $src, 100, 100, 0, 0, 100, 100);//完成合并
Copy after login
  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 section 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);
    }
Copy after login

Related recommendations:

php realizes image merging in WeChat-Jiugongge picture

The above is the detailed content of How to merge multiple pictures in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template