Jump to content

[SOLVED] Make thumbnail transparent help


pocobueno1388

Recommended Posts

I have a script that will upload an image, then create a thumbnail of that image. When I upload an image with a transparent background the normal sized image comes out with a transparent background, but the thumbnail image comes out with a black background.

 

I am thinking I need the function imagecolortransparent(), but I'm not really sure how to incorporate it into the thumbnail generation code.

 

<?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); 
  	  
      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); 
           
   }

?>

 

Any help with getting the thumbnail image to come out transparent, if the original picture is transparent, will be much appreciated =]

 

Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/71287-solved-make-thumbnail-transparent-help/
Share on other sites

Added a couple of lines (commented). Not tested. If it doesn't work we'll try plan B.

 

<?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;
      }
      
     $transparentColor = imagecolortransparent($src_img);                               // get transparent color in source
      
     $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
     imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

     imagecolortransparent($dst_img, $transparentColor);                                // apply it to the dest image 
     	  
      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); 
           
   }

?>

Here is the updated code with Barands modifications in it:

 

<?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;
      }
      
      if ($thumb_w > $old_x && $thumb_h > $old_y){
         $thumb_w = $old_x;
         $thumb_h = $old_y;
      }
      
     $transparentColor = imagecolortransparent($src_img);                               // get transparent color in source
      
     $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
     imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

     imagecolortransparent($dst_img, $transparentColor);                                // apply it to the dest image 
     	  
      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); 
           
   }

?>

 

Without Barands modifications:

 

<?php

<?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;
      }
      if ($thumb_w > $old_x && $thumb_h > $old_y){
         $thumb_w = $old_x;
         $thumb_h = $old_y;
      }
      
     $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
     imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
  	  
      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); 
           
   }

?>

The images I have been testing with are GIF files. BEFORE they are resized, they are perfectly transparent...it's just when they are converted to thumbnails the parts that are supposed to be transparent come out black.

does copying the palette help?

<?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;
      }
      
      if ($thumb_w > $old_x && $thumb_h > $old_y){
         $thumb_w = $old_x;
         $thumb_h = $old_y;
      }
      
     $transparentColor = imagecolortransparent($src_img);                               // get transparent color in source
      
     $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
     imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

     imagepalettecopy($dst_img, $src_img);                                              // copy the pallette
     imagecolortransparent($dst_img, $transparentColor);                                // apply it to the dest image 
     	  
      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); 
           
   }

?>

Plan B

 

<?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); 
           
   }

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.