Solved

How to create thumbnail of a transparent GIf in PHP?

Posted on 2010-11-30
13
783 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 109

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 109

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 109

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 109

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
 

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 109

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 109

Expert Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress French and English Site 6 79
What does this formatting equate to? 7 18
Text field validation on text fields used for multiple inserts 7 22
PHP 5.6 and 7.x 4 23
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

820 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