Re: Acceptably Converting a 24bit PNG to a JPEG?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



using imagecopyresampled did the trick :)

This will be great for my css files.
If all goes well, the below will be used to dynamically convert PNG images to Jpegs for IE6 and below. For some bizarre reason, you can not use the background-position property with PNGs for <= IE 6. So, in my css, I'l dynamically convert the PNG to a JPG and replace the transparency with a background color

many thanks

in case anyone is interested, here is the code

<?php

isset($_GET["type"]) ? $type=htmlspecialchars($_GET["type"] ): $type="jpg";
isset($_GET["url"]) ? $url=realpath($_GET["url"] ): $url="blank.png";
isset($_GET["color"]) ?$color= htmlspecialchars($_GET["color"]): $color="#ffffff";

//get path info on the $url
$path_parts= pathinfo($url);
$file= explode(".",$path_parts['basename']);
$image_name=$file[0];
list($sx, $sy)=getimagesize($url);

// Create a bg image from the color and image size variables
$bg_image=imagecreatetruecolor($sx,$sy);
list($R,$G,$B)= (HEX2RGB($color));
$mycolor= ImageColorAllocate($bg_image, $R,$G,$B);
ImageFill($bg_image, 0, 0, $mycolor);

//Create the PNG image from the $url variable
$image = imagecreatefrompng($url);
imageAlphaBlending($image,true	); imageSaveAlpha($image, true);

//Resample the merging of the background image and the original PNG file
imagecopyresampled($bg_image, $image, 0, 0, 0, 0, $sx, $sy, $sx, $sy);

// Send out as GIF or JPG
Switch ($type){
case "jpg": header('Content-type: image/jpeg'); imagejpeg ($bg_image,'',95); break; case "gif": header('Content-type: image/gif'); imagegif($image); break;
}

//Remove bg image and PNG from memory
imagedestroy($bg_image);
imagedestroy($image);


function HEX2RGB($color){
$color_array = array();
$hex_color = strtoupper($color);
for($i = 0; $i < 6; $i++){
  $hex = substr($hex_color,$i,1);
  switch($hex){
   case "A": $num = 10; break;
   case "B": $num = 11; break;
   case "C": $num = 12; break;
   case "D": $num = 13; break;
   case "E": $num = 14; break;
   case "F": $num = 15; break;
   default: $num = $hex; break;
  }
  array_push($color_array,$num);
}
$R = (($color_array[0] * 16) + $color_array[1]);
$G = (($color_array[2] * 16) + $color_array[3]);
$B = (($color_array[4] * 16) + $color_array[5]);
return array($R,$G,$B);
unset($color_array,$hex,$R,$G,$B);
}



?>

On Dec 2, 2006, at 1:33 PM, Richard Lynch wrote:

I suspect that if you do an "imagecopyresampled" or somesuch into a
fresh NEW image, you'd get what you want.

Not saying you haven't found a bug ; Just suggesting a work-around.

On Fri, December 1, 2006 4:26 pm, Graham Anderson wrote:
Is there some trick to getting PHP GD to properly convert a 24bit PNG
[with alpha]  into a JPEG without garbling the image output?

The below will output the jpg image, but it appears that the alpha
channel from the original png  is garbling the jpeg a bit
To no avail, I tried setting imageAlphaBlending and imageSaveAlpha to
'false' and 'true'
I also tried setting the jpeg quality to 90 and 100.

$image_output = "myimage.jpeg";
$image = imagecreatefrompng($original_24bit_png_image);

imageAlphaBlending($image, true);
imageSaveAlpha($image, true);

header("Content-type: image/jpeg");
imagejpeg($image,'',100);
imagedestroy($image);


many thanks in advance
g

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux