I am trying to create a jpeg file by merging a transparent png over a jpeg. I have tried the net & other questions but I didn't find what I was looking for. The below code successfully runs in my local environment, but as soon as I upload it to my server[Godaddy/Linux] the transparent background of png turns white, thus, covering the jpeg. I have tried using 'imagecopyresampled' in place of 'imagecopy' as suggested here but that did not work. Is there something that I am doing here wrong? Checked server GD, it is installed. Any suggestion will be appreciated.
$dest = imagecreatefromjpeg($jpegFileURL);
$src = imagecreatefrompng($pngFileURL);
list($destWidth, $destHeight) = getimagesize($jpegFileURL);
list($srcWidth, $srcHeight) = getimagesize($pngFileURL);
/***
* create base image
*/
$base_image = imagecreatetruecolor($destWidth,$destHeight);
/***
* make $base_image transparent
*/
imagealphablending($base_image, false);
$col = imagecolorallocatealpha($base_image,255,255,255,127);
imagefilledrectangle($base_image,0,0,$destWidth,$destHeight,$col);
imagealphablending($base_image,true);
imagesavealpha($base_image, true);
/***
* Copy JPEG & then PNG over JPEG
*/
imagecopy($base_image, $dest, 0, 0, 0, 0, $destWidth, $destHeight);
imagecopy($base_image, $src, 0, 0, 0, 0, $srcWidth, $srcWidth);
/***
* Output the file
*/
header('Content-Type: image/jpeg');
imagejpeg($base_image,$urlToSave);
imagedestroy($dest);
imagedestroy($src);
EDIT: The jpeg and png used here are also generated by a script which I then use here to merge.
To make sure that the file generation wasn't the issue, I tested by using a png with a transparent background not generated by my code and it worked.
Thus I am guessing the issue could be from the png generation.
Image attached for one that worked.
Code used to generate the png.
$image = imagecreatefromjpeg( $fileJpegURL );
imagealphablending($image, true);
$transparentcolour = imagecolorallocate($image, 255,255,255);
imagecolortransparent($image, $transparentcolour);
header( 'Content-Type: image/png' );
imagepng( $image, $pngFileSavePath, 1 );