Solved

Resize GIF with transparent background

Posted on 2007-04-09
4
292 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
ID: 18876923
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
ID: 18878933
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
ID: 18879339
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
ID: 18881859
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

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.

Question has a verified solution.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

805 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