Combine a transparent PNG image on top of another image with PHP

May 31, 2014 | Last tested: May 2015 | 2156 views | Comments

Retain the transparency of a PNG image when merging it to a JPG or GIF image using PHP.

For example.

Images used (with permission) on these examples are the following:

  1. PNG Image - table.png. From this link.
  2. JPG Image - create-a-surreal-head-of-tree-photo-manipulation.jpg. From this link.

 

1. Use the function imagecopy() to merge a transparent PNG image with a JPG image using PHP.

//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
imagecopy($dest,$src,$src_xPosition,$src_yPosition,$src_cropXposition,$src_cropYposition,$srcWidth,$srcHeight);

//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'
 */
imagejpeg($dest,'combine-a-transparent-png-image-on-top-of-another-image-with-php-01.jpg',100);

//destroy the source image
imagedestroy($src);

//destroy the destination image
imagedestroy($dest);

Result.

Please mouse over here to view the image.

 

2. Merge a transparent PNG image with a JPG image using the imagecopymerge() function in PHP .

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

//set the transparency of the source image
$srcTransparency = 50; //the higher the clearer, max is 100

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

//get the size of the source image, needed for imagecopymerge()
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 = 450; //450 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

/*
 * get the index of the color of a pixel of the source image (imagecolorat),
 * and define a color as transparent (imagecolortransparent)
 */
imagecolortransparent($src,imagecolorat($src,0,0));
						
//merge the source and destination images
imagecopymerge($dest,$src,$src_xPosition,$src_yPosition,$src_cropXposition,$src_cropYposition,$srcWidth,$srcHeight,$srcTransparency);

//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'
 */
imagejpeg($dest,'combine-a-transparent-png-image-on-top-of-another-image-with-php-02.jpg',100);

//destroy the source image
imagedestroy($src);

//destroy the destination image
imagedestroy($dest);

Result.

Please mouse over here to view the image.