1

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 );
Community
  • 1
  • 1
ram.si
  • 11
  • 3

2 Answers2

0

A lot of your code is unnecessary, but otherwise it works. It can be reduced to:

$dest = imagecreatefromjpeg($jpegFileURL);
$src = imagecreatefrompng($pngFileURL);

list($srcWidth, $srcHeight) = getimagesize($pngFileURL);

/***
* make $base_image transparent
*/
imagealphablending($dest, true);

/***
* Copy JPEG & then PNG over JPEG
*/
imagecopy($dest, $src, 0, 0, 0, 0, $srcWidth, $srcWidth);

/***
* Output the file
*/
imagejpeg($dest, $urlToSave);

imagedestroy($dest);
imagedestroy($src);

Verify there are no errors when you run the script on your server.

timclutton
  • 12,682
  • 3
  • 33
  • 43
  • Tried your snippet, worked perfectly in local but no luck in server. No errors when I ran the script. – ram.si Apr 26 '16 at 10:36
  • Have you checked the server error log? Normally `error_reporting` is off on production sites. – timclutton Apr 26 '16 at 12:24
  • Yes, checked the php error log also checked error_reporting is on. Other php errors were logged but none were logged when the above snippet was run. – ram.si Apr 27 '16 at 03:16
0

Issue resolved! As I guessed in the edit, the issue was with how the transparent png was being created.
The change that worked for me was in that section.
Although I am not entirely able to make sense of the reason, but this gave the output I was looking for. Maybe I stretched the coding lines unnecessarily.
Thankful for the input I received and any enlightenment is welcomed.


Below is the extra snippet that I needed to add that I found from HERE.
$image = imagecreatefromjpeg( $fileJpegURL );
imagealphablending($image, false); //-- Change Here
//-- 3 New lines added
$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparency);
imagesavealpha($image, true);

$transparentcolour = imagecolorallocate($image, 255,255,255);
imagecolortransparent($image, $transparentcolour);

header( 'Content-Type: image/png' );
imagepng( $image, $pngFileSavePath, 1 );
Community
  • 1
  • 1
ram.si
  • 11
  • 3