Home » Php » php – GD Image overlay transparent png

php – GD Image overlay transparent png

Posted by: admin July 12, 2020 Leave a comment


I have transparent image that I want to overlay over the entire image to give it the boarder effect. In this code it crops an existing image. It merges the two but the final over the top is not showing the alpha. How can I fix this?

    $dst_x = 0;   // X-coordinate of destination point. 
    $dst_y = 0;   // Y --coordinate of destination point. 
    $src_x = 163; // Crop Start X position in original image
    $src_y = 0;   // Crop Srart Y position in original image
    $dst_w = 469; // Thumb width
    $dst_h = 296; // Thumb height
    $src_w = 469; // $src_x + $dst_w Crop end X position in original image
    $src_h = 296; // $src_y + $dst_h Crop end Y position in original image

    // Creating an image with true colors having thumb dimensions.( to merge with the original image )
    $dst_image = imagecreatetruecolor($dst_w,$dst_h);
    // Get original image
    $src_image = imagecreatefromjpeg("http://www.ucatholic.com/wp-content/uploads/2012/10/Sallixtus-631x295.jpg");
    // Cropping 
    imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
    // Saving 
    imagejpeg($dst_image, "images/crop.jpg");

    $src = imagecreatefrompng('http://www.EdVizenor.com/images/saintCover.png');

    /// THIS LINE NOT WORKING - -   // Copy and merge
    imagecopymerge($dst_image, $src, 0, 0, 0, 0, 469, 296, 100);

    header('Content-Type: image/png');
How to&Answers:

Dont use imagecopymerge, use imagecopy, then it will works normally and your watermark will be shown with alpha.

imagecopy($dst_image, $src, 0, 0, 0, 0, 469, 296);


If used the imagecopy instead of imagecopymerge you will lose control over alpha channel.

I think you could use Jaguar , if you want a real fast overlay action using the Gd extension.

see my answer Here if you want to see how

if you want a brushed border you can use Jaguar like this:

use Jaguar\Canvas,

    $canvas = new Canvas('your image');
    $brush = new Canvas('style image');

    $border = new Border(20);

    $canvas->save('path to save')->show();