Solved

Resize GIF with transparent background

Posted on 2007-04-09
4
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 51

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 51

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

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

718 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