views:

1249

answers:

5

How Can I get this working for transparent gif's and png's?

function resizeImage($image,$newImage,$target_width,$target_height, $type="") {
 if (is_file($image)) {
  if($type == ".gif"){
   $image_org=@imagecreatefromgif($image);
  }else{
   $image_org=@imagecreatefromjpeg($image);
  }
  if ($image_org) {
   list($w,$h,$type,$attr) = getimagesize($image);
   $factor=C_Image_Custom::calcRescaleFactor($w,$h,$target_width,$target_height);

   if ($factor>1) {
    $image_w = $w / $factor;
    $image_h = $h / $factor;
   } else {
    $image_w = $w;
    $image_h = $h;
   }  

     //Note: PHP with GD2.0 required for imagecreatetruecolor
     $img_copy = imagecreatetruecolor($image_w, $image_h);
     imagecopyresampled($img_copy, $image_org, 0, 0, 0, 0, $image_w, $image_h, $w, $h);

   if (@imagejpeg($img_copy, $newImage, 80)) {
    chmod($newImage,0777);
   } else {
    echo("<b>Error: </b>Unable to create image $newImage. Check directory permissions.");
   } 

    imagedestroy($image_org);
   imagedestroy($img_copy);
  } 
 }
+2  A: 

It looks like you're only outputting to jpeg - which doesn't have transparency. If you want to output the transparency, you need to output a gif or png.

If you want to replace the transparency with a colour, I think you want the php function imagecolorallocatealpha

adam
A: 

Why only a jpeg?

This is also for gif:

if($type == ".gif"){
                    $image_org=@imagecreatefromgif($image);
            }else{
                    $image_org=@imagecreatefromjpeg($image);
            }
sanders
That's the input image, the output is JPEG only, look at this line: "if (@imagejpeg($img_copy, $newImage, 80)) {".
Milen A. Radev
A: 

I have altered the script a bit. But the transparent gif's still show a black background.

Any idea's?

function resizeImage($img,$newImage,$target_width,$target_height, $type="") {
  if (!extension_loaded('gd') && !extension_loaded('gd2')) {
  trigger_error("GD is not loaded", E_USER_WARNING);
  return false;
  }


 if (is_file($img)) {

    $imgInfo = getimagesize($img);
    switch ($imgInfo[2]) {
  case 1: $image_org = imagecreatefromgif($img); break;
  case 2: $image_org = imagecreatefromjpeg($img);  break;
  case 3: $image_org = imagecreatefrompng($img); break;
  default:  trigger_error('Unsupported filetype!', E_USER_WARNING);  break;
   }

  if ($image_org) {
   list($w,$h,$type,$attr) = getimagesize($img);
   $factor=C_Image_Custom::calcRescaleFactor($w,$h,$target_width,$target_height);

   if ($factor>1) {
    $image_w = $w / $factor;
    $image_h = $h / $factor;
   } else {
    $image_w = $w;
    $image_h = $h;
   }  

     //Note: PHP with GD2.0 required for imagecreatetruecolor
     $img_copy = imagecreatetruecolor($image_w, $image_h);

     /* Check if this image is PNG or GIF, then set if Transparent*/  
     if(($imgInfo[2] == 1) OR ($imgInfo[2]==3)){
     imagealphablending($img_copy, false);
   imagesavealpha($img_copy,true);
   $transparent = imagecolorallocatealpha($img_copy, 255, 255, 255, 127);
   imagefilledrectangle($img_copy, 0, 0, $image_w, $image_h, $transparent);
   echo "hier";
   }




     imagecopyresampled($img_copy, $image_org, 0, 0, 0, 0, $image_w, $image_h, $w, $h);
   switch ($imgInfo[2]) {
    case 1: imagegif($img_copy,$newImage); break;
   case 2: imagejpeg($img_copy,$newImage);  break;
   case 3: imagepng($img_copy,$newImage); break;
   default:  trigger_error('Failed resize image!', E_USER_WARNING);  break;

   }
   chmod($newImage,0777);
   //if (@imagejpeg($img_copy, $newImage, 80)) {

   //} else {
   // echo("<b>Error: </b>Unable to create image $newImage. Check directory permissions.");
   //} 

    imagedestroy($image_org);
   imagedestroy($img_copy);
  } 
 } 
}
sanders
+1  A: 
Andrew Swift
A: 

Hi all!

OK this issue has also been driving me crazy..

In attempt to try the above solution, this line causes a problem:

for($y=0; $y>24) & 0x7F) >= 100) imagesetpixel($imageResized, $x, $y, $transindex);

Where does the $x value come from?

I'm not familiar with this sort of for loop code, however all I know is that there's a run time error

Any help would be great