Solved

How to create thumbnail of a transparent GIf in PHP?

Posted on 2010-11-30
13
771 Views
Last Modified: 2012-05-10
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
Comment
Question by:sigmatec_pk
  • 6
  • 6
13 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34239169
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34425494
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
 

Author Comment

by:sigmatec_pk
ID: 34433069
You can use any transparent gif images. Search google images and you will get many.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34435449
Please upload an example image that you want us to use.  It's not up to us to guess your requirements!  Thanks.
0
 

Author Comment

by:sigmatec_pk
ID: 34435817
These are the two sample gif images.
 a sample gif another sample gif
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34437003

<?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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:sigmatec_pk
ID: 34437480
Thanks for your reply. I'll try that and will update your soon.
0
 

Author Comment

by:sigmatec_pk
ID: 34446911
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
 

Author Comment

by:sigmatec_pk
ID: 34446912
Can you suggest some alternate to this?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34449070
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
 

Author Closing Comment

by:sigmatec_pk
ID: 34450467
Thanks for your assistance.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34450473
Thanks for the points.  It's a great question!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now