1

I have an image.png with white background and some trasparceny over it.

I tried this to convert the image into jpg:

$data = file_get_contents('image.png');
$resource = imagecreatefromstring($data);
imagejpeg($resource); //> I TRIED WITH QUALITY = 100 TOO

Problem is where the png got the trasparency now the jpg got a pretty huge black zone. This is how jpg looks:

http://img861.imageshack.us/img861/20/context.jpg

Any way to solve the problem?

Edit1:

As suggested by Abiusx I tried this too:

$data = file_get_contents('image.png');
$resource = imagecreatefromstring($data);
imagealphablending($data, false);
imagesavealpha($data, true);
imagejpeg($resource);

But the result was the same. Please note The source .png image is:


(source: tipradar.com)

Thanks to Patrick comment: here the trick: GD! Converting a png image to jpeg and making the alpha by default white and not black

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • i dont exactly recall but help is provided on php website (via comments), i'll look for it and post here in a while. – AbiusX Mar 15 '11 at 00:58
  • Oh, Jpeg does not support transparency. I didnt read ur title. only PNG and GIf support transparency. – AbiusX Mar 15 '11 at 00:59
  • @abiusx: yes i don't want to keep traspareceny in my jpg, I just want that the final jpg without that black patch –  Mar 15 '11 at 01:27
  • 2
    take a look at this SO [question](http://stackoverflow.com/questions/2569970/gd-converting-a-png-image-to-jpeg-and-making-the-alpha-by-default-white-and-not) – Patrick Mar 15 '11 at 01:31

2 Answers2

1

Answer here:

GD! Converting a png image to jpeg and making the alpha by default white and not black

Community
  • 1
  • 1
dynamic
  • 46,985
  • 55
  • 154
  • 231
-1

This is the function I use to resize a PNG but preserve transparency, if it doesnt help, tell me to extract the parts necessary for you:

function Resize($ImageFile,$OriginalFile)
{
    $ext=basename($OriginalFile);
    $ext=explode(".",$ext);
    $ext=array_pop($ext);
    $ext=strtolower($ext);
    if ($ext=="jpg" or $ext=="jpeg" or $ext=="jpe")
        $img=imagecreatefromjpeg($ImageFile);
    elseif ($ext=="png")
        $img=imagecreatefrompng($ImageFile);
    elseif ($ext=="gif")
        $img=imagecreatefromgif($ImageFile);
    else
        return false;
    list($w,$h)=getimagesize($ImageFile);
    $dstimg=imagecreatetruecolor(140,100);

    imagealphablending($dstimg, false);
    imagecopyresampled($dstimg,$img,0,0,0,0,140,100,$w,$h);
    imagesavealpha($dstimg, true);
    imagepng($dstimg,$ImageFile);
    return true;
}
AbiusX
  • 2,379
  • 20
  • 26
  • Man i don't want to print out a png from another png.. I need to convert a png to a .jpg without having black patch on it :) – dynamic Mar 15 '11 at 01:30
  • check the 4 last functions, mainly imagealphablending and imagecopyresampled and imagesavealpha – AbiusX Mar 15 '11 at 01:31
  • Ok I used them, the result was still with that black patch on it. I did: imagecreatefromstring, imagealphablendin, imagesavealpha and imagejpeg (i don't need a resampled so i didn't use it) – dynamic Mar 15 '11 at 09:44
  • please take a peek at PHP website for those functions, comments would help you. – AbiusX Mar 15 '11 at 13:49