Solved

Resize GIF with transparent background

Posted on 2007-04-09
4
291 Views
Last Modified: 2011-10-06
I have a series of gifs that have transparent backgrounds that I want to create thumbnails of when they are uploaded. When I try to resize them, the background becomes black. Can someone tell me how to preserve the transparency of the image? Here is my code:

$img = $_SERVER['DOCUMENT_ROOT'] . "/images/test.gif";
$thumb = $_SERVER['DOCUMENT_ROOT'] . "/images/tn/test.gif";
#------------------------------------------------------------------------------------------#
$source = imagecreatefromgif($img);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagegif($thumb,$thumbnail);
chmod($thumbnail, 0644);
return str_replace($_SERVER['DOCUMENT_ROOT'],"",$thumbnail);
0
Comment
Question by:rbichon
  • 2
4 Comments
 
LVL 50

Accepted Solution

by:
Steve Bink earned 250 total points
Comment Utility
I've read quite a bit about palette vs truecolor images.  I'm left with the impression that most of the palette-based functions do not carry over transparency.  Here's a comment I found in the PHP manual that may be of some use:

http://www.php.net/manual/en/function.imagecopy.php#48355

I've also notice this function being used a bit in the context of dealing with transparency:

http://www.php.net/manual/en/function.imagecreatetruecolor.php
0
 
LVL 29

Expert Comment

by:rdivilbiss
Comment Utility
The manual states you must tell imagegif the color of the transparancy using imagecolortransparent() http://us3.php.net/manual/en/function.imagecolortransparent.php.

0
 
LVL 50

Expert Comment

by:Steve Bink
Comment Utility
The imagecolortransparent() function maps a single color as transparent on an existing image.  That's fine for what it does, but he's getting his transparency lost when he creates the copy.  The notes below the function explanation detail what he needs:

----------------------------------------------
    Note:  Transparency is copied only with imagecopymerge() and true color images, not with imagecopy() or pallete images.

    Note: The transparent color is a property of the image, transparency is not a property of the color. Once you have set a color to be the transparent color, any regions of the image in that color that were drawn previously will be transparent.
----------------------------------------------

So, worst case scenario, use imagecreatetruecolor() to create a new, 100% transparent image, then merge it with the GIF you are opening.  The resulting picture should retain the transparency.
0
 
LVL 1

Author Comment

by:rbichon
Comment Utility
The transparency became too much. The thumbnail is on a white background anyway so I decided to merge the image with a white one. Here is my code:

$img = $_SERVER['DOCUMENT_ROOT'] . "/images/test.gif";
$thumbnail = $_SERVER['DOCUMENT_ROOT'] . "/images/thumbs/test.gif";
$new = imagecreatetruecolor($newWidth, $newHeight);
$source = imagecreatefromgif($img);
$white = imagecolorallocate($new, 255, 255, 255);
imagefill($new,0,0,$white);
imagecopyresized($new, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagegif($new,$thumbnail);
chmod($thumbnail, 0644);
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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…
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

9 Experts available now in Live!

Get 1:1 Help Now