Solved

Resize GIF with transparent background

Posted on 2007-04-09
4
293 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
The viewer will learn how to dynamically set the form action using jQuery.

821 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