Jump to content

[SOLVED] Transparent GIF


The Little Guy

Recommended Posts

OK, I am trying to resize transparent GIF images, so I was reading, and they say to use fopen, fread, and read the first 13 bytes... OK, so I read in a transparent gif, and the first 13 bytes... and echo it out to the screen, and I get this:

 

GIF89a›�s�÷��

 

 

What am I supposed to do with that?

 

they then say "allocate that color as transparent, and use imagecopy(); to set the transparency back."

 

Can someone help me?? not exactly sure what to do

Link to comment
Share on other sites

Here is a function Barand helped me out with

 

<?php

   function uploadThumb($name, $filename, $new_w, $new_h, $ext) {
         
      if ($ext == 'jpg'){
         $src_img = imagecreatefromjpeg($name);
      } else if ($ext == 'gif'){
         $src_img = imagecreatefromgif($name);
      } else if ($ext == 'png'){
       $src_img = imagecreatefrompng($name);
      }
      
      $old_x=imageSX($src_img);
      $old_y=imageSY($src_img);
      
      if ($old_x > $old_y) {
      	$thumb_w=$new_w;
      	$thumb_h=$old_y*($new_h/$old_x);
      }
      if ($old_x < $old_y) {
      	$thumb_w=$old_x*($new_w/$old_y);
      	$thumb_h=$new_h;
      }
      if ($old_x == $old_y) {
      	$thumb_w=$new_w;
      	$thumb_h=$new_h;
      }
      
     $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
     imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

     imagetruecolortopalette($dst_img,false,256);                                         // convert to palletted image
     $color_00 = imagecolorat($dst_img,0,0);                                              // get background color at 0,0
     imagecolortransparent($dst_img, $color_00);                                          // make it transparent
    	  
      if ($ext == 'jpg'){
         imagejpeg($dst_img,$filename);
      } else if ($ext == 'gif'){
         imagegif($dst_img,$filename);
      } else if ($ext == 'png'){
         imagepng($dst_img,$filename);
      }

     imagedestroy($dst_img); 
     imagedestroy($src_img); 
           
   }

?>

 

It works pretty good. The only problem with it is if you have a pure black area on the picture, it is going to make it transparent.

Link to comment
Share on other sites

Here is the one I found!!! IT WORKS TOO!

 

<?php
function transparentGif($imageDirectory,$thumbDirectory, $imageName, $thumbWidth){
$image = imagecreatefromgif("$imageDirectory/$imageName");
$details = getimagesize("$imageDirectory/$imageName");
$thumbHeight = $details[1] * ($thumbWidth / $details[0]);
$resized = imagecreatetruecolor($thumbWidth, $thumbHeight);
$colorTransparent = imagecolortransparent($image);
imagepalettecopy($image, $resized);
imagefill($resized, 0, 0, $colorTransparent);
imagecolortransparent($resized, $colorTransparent);
imagecopyresized($resized, $image, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $details[0], $details[1]);
imagegif($resized,"$thumbDirectory/$imageName");
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.