How to merge transparent PNG with image using PHP?

后端 未结 3 1663
误落风尘
误落风尘 2020-11-27 16:33

The situation is this: I have a small 50x50 pic. I also have a small 50x50 transparent picture which contains a frame for the 50x50 pic, so I basically want to put the trans

相关标签:
3条回答
  • 2020-11-27 16:38

    You can do it using ImageMagick :: Composite. The first user contributed note should be enough to grasp the concept.

    0 讨论(0)
  • 2020-11-27 16:56

    You can merge the two images together using the PHP GD2 library.

    Example:

    <?php
     # If you don't know the type of image you are using as your originals.
     $image = imagecreatefromstring(file_get_contents($your_original_image));
     $frame = imagecreatefromstring(file_get_contents($your_frame_image));
    
     # If you know your originals are of type PNG.
     $image = imagecreatefrompng($your_original_image);
     $frame = imagecreatefrompng($your_frame_image);
    
     imagecopymerge($image, $frame, 0, 0, 0, 0, 50, 50, 100);
    
     # Save the image to a file
     imagepng($image, '/path/to/save/image.png');
    
     # Output straight to the browser.
     imagepng($image);
    ?>
    
    0 讨论(0)
  • 2020-11-27 17:03

    Add imagealphablending($frame,true); before imagecopymerge() if you want to keep PNG frame transparancy over the image.

    0 讨论(0)
提交回复
热议问题