Home » Php » png – PHP imagecopy with transparent background

png – PHP imagecopy with transparent background

Posted by: admin July 12, 2020 Leave a comment


I use this code to create an image from another png image, the background is black by default. My question is how to set a transparent background?

$input = imagecreatefrompng('image.png');
$output = imagecreatetruecolor(50, 50);

imagecopy($output, $input, 4,0, 8,8, 8,8);
imagecopy... etc.

header('Content-Type: image/png');

Is there a easy way of doing this? Thanks

How to&Answers:

Sets the transparent color in the given image.

int imagecolortransparent ( resource $image [, int $color ] )

Here’s the link


Since the PHP function imagecopymerge doesn’t work with the Alpha channel, you’ll want to use the function from the first comment on this page imagecopymerge_alpha:

Just have the transparent image as the base and merge it together with the image you need.

I’ve tried it out and it works fine for a project of mine.


imagealphablending($input, true);
imagesavealpha($input, true);

imagealphablending($output, true);
imagesavealpha($output, true);


Or propably

int imagesavealpha($img,true);



None of the solutions worked for me, it would always convert transparent pixels on the source image to black on the destination image. What worked was changing imagecopy/imagecopymerge/imagecopymerge_alpha to imagecopyresampled and just passing the same width and height twice.

    //Create destination image.
    $png = imagecreatetruecolor(1024, 1024);
    imagealphablending($png, false);
    imagesavealpha($png, true);
    //Make destination image be all transparent.
    $color = imagecolorallocatealpha($png, 0, 0, 0, 127); //127 means completely transparent.
    imagefill($png, 0, 0, $color);

    //Load source image.
    $png2 = imagecreatefrompng($sourceurl);
    imagealphablending($png2, false);
    imagesavealpha($png2, true);
    $sizex = imagesx($png2);
    $sizey = imagesy($png2);

    //Copy to destination and save to file.
    imagecopyresampled( $png, $png2, 
    0, 0,
    0, 0, 
    $sizex, $sizey, 
    $sizex, $sizey);
    imagepng($png, "result.png");


Full credit goes to:

The following code will overlay the foreground image onto the background image while preserving the transparency of the overlay:

//set the source image (foreground)
$sourceImage = 'table.png';

//set the destination image (background)
$destImage = 'create-a-surreal-head-of-tree-photo-manipulation.jpg';

//get the size of the source image, needed for imagecopy()
list($srcWidth, $srcHeight) = getimagesize($sourceImage);

//create a new image from the source image
$src = imagecreatefrompng($sourceImage);

//create a new image from the destination image
$dest = imagecreatefromjpeg($destImage);

//set the x and y positions of the source image on top of the destination image
$src_xPosition = 75; //75 pixels from the left
$src_yPosition = 50; //50 pixels from the top

//set the x and y positions of the source image to be copied to the destination image
$src_cropXposition = 0; //do not crop at the side
$src_cropYposition = 0; //do not crop on the top

//merge the source and destination images

//output the merged images to a file
 * '100' is an optional parameter,
 * it represents the quality of the image to be created,
 * if not set, the default is about '75'

//destroy the source image

//destroy the destination image