• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 873
  • Last Modified:

How to create thumbnail of a transparent GIf in PHP?

Hi,

I want to create thumbnails of transparent gif images and want to keep it transparent aswell.
Can you please how can this be done without loosing image quality?

Thanks
0
sigmatec_pk
Asked:
sigmatec_pk
  • 6
  • 6
1 Solution
 
Ray PaseurCommented:
Simply put, it cannot be done without losing image quality.  If you want to post some examples of the images, I may be able to help with some of it.
0
 
Ray PaseurCommented:
If you will post some examples of the images, I will try to get you an answer.  I asked for that, but never saw any response.  Surely you have SOME test images for your project?!  Please post those images, and tell us the dimensions you want to use for the thumbnails.

Also, understand that an image that is large has more image information than an image that is small, and so when you say "without losing image quality" you are defining into existence a condition that is physically impossible.  That said, there may be solutions that will work for you, but we cannot help you unless you respond to our requests.

Thanks and regards, ~Ray
0
 
sigmatec_pkAuthor Commented:
You can use any transparent gif images. Search google images and you will get many.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Ray PaseurCommented:
Please upload an example image that you want us to use.  It's not up to us to guess your requirements!  Thanks.
0
 
sigmatec_pkAuthor Commented:
These are the two sample gif images.
 a sample gif another sample gif
0
 
Ray PaseurCommented:

<?php // RAY_image_resize_transparent_gif.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO GENERATE A THUMBNAIL FROM A GIF WHILE PRESERVING TRANSPARENCY


// GET THE IMAGE RESOURCE (TESTED OK WITH BOTH)
$src = imageCreateFromGif("http://filedb.experts-exchange.com/incoming/2010/12_w01/383952/4.gif");
$src = imageCreateFromGif("http://filedb.experts-exchange.com/incoming/2010/12_w01/383953/5.gif");

// GET THE ORIGINAL DIMENSIONS
$src_wide = imagesX($src);
$src_high = imagesY($src);

// USE LOWER LEFT PIXEL TO DETERMINE ITS TRANSPARENCY
$pixel = imageColorAt($src, 0, $src_high-1);
$clear = imageColorsForIndex($src, $pixel);

// COMPUTE DIMENSIONS FOR THE THUMBNAIL
$dst_wide = $src_wide / 4;
$dst_high = $src_high / 4;

// CREATE A NEW IMAGE RESOURCE AT THE THUMBNAIL SIZE
$dst = imageCreateTrueColor($dst_wide, $dst_high);

// FILL THE IMAGE WITH THE CLEAR BACKROUND COLOR
$clear = imageColorAllocate($dst, $clear["red"], $clear["green"], $clear["blue"]);
imagefill($dst, 0, 0, $clear);

// COPY THE IMAGE
imageCopyResampled
( $dst
, $src
, 0
, 0
, 0
, 0
, $dst_wide
, $dst_high
, $src_wide
, $src_high
)
;

// SET BACKGROUND TO COMPLETELY TRANSPARENT
imageColorTransparent($dst, $clear);

// STORE THE IMAGE ON THE SERVER
imagePNG($dst, 'RAY_temp_png.png', 9, PNG_ALL_FILTERS);

// SEND THE IMAGE TO THE BROWSER
header('Content-type: image/png');
imagePNG($dst);

// FREE THE RESOURCES
imageDestroy($src);
imageDestroy($dst);

Open in new window

0
 
sigmatec_pkAuthor Commented:
Thanks for your reply. I'll try that and will update your soon.
0
 
sigmatec_pkAuthor Commented:
Thanks for your reply Ray_Paseur.
It works very fine but there is only one issue. I need thumbnail in gif format, while you are making png thumb.

So I change
imagePNG($dst, 'RAY_temp_png.png', 9, PNG_ALL_FILTERS);
to
imagegif($dst, 'thumb,gif');

But that reduces quality of thumbnail.
0
 
sigmatec_pkAuthor Commented:
Can you suggest some alternate to this?
0
 
Ray PaseurCommented:
Yes, I can suggest an alternative.  GIF is ancient history, and it's the wrong tool for the job.  Forget about it and use PNG thumbnails.  Here is why:  

GIF was considered a state-of-the-art technology for 1991, when (1) a high-speed internet connection was about 14KB/second and (2) computer monitors could display 256 colors and (3) you had to squeeze every bit of information out of graphics to avoid slow page loads.  GIF made sense in the earliest days of the internet.  GIF achieved its compression in part by using a palette of only 256 colors.  As you have observed in your tests, the color limitation makes the GIF format unsuitable for reproducing photographs or images with continuous color.
http://en.wikipedia.org/wiki/Graphics_Interchange_Format 

PNG became an international standard in 2003.  Today, the average internet connection speed is measured in megabytes instead of kilobytes.  Computer monitors display 16 million colors, not 256.  And the PNG standard is the open-source, state-of-the-art lossless compression technique for graphics and images.  
http://www.libpng.org/pub/png/

Times have changed.  GIF, and to some extent, JPG are out-of-date compression formats that guarantee inferior images.
0
 
sigmatec_pkAuthor Commented:
Thanks for your assistance.
0
 
Ray PaseurCommented:
Thanks for the points.  It's a great question!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now