Solved

How to create thumbnail of a transparent GIf in PHP?

Posted on 2010-11-30
13
768 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
Comment Utility
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
Comment Utility
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
Comment Utility
You can use any transparent gif images. Search google images and you will get many.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility

<?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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

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

Author Comment

by:sigmatec_pk
Comment Utility
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
Comment Utility
Can you suggest some alternate to this?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
Thanks for your assistance.
0
 
LVL 108

Expert Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 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

6 Experts available now in Live!

Get 1:1 Help Now