Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Resize GIF with transparent background

Posted on 2007-04-09
4
Medium Priority
?
297 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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.
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 look for a specific file type in a local or remote server directory using PHP.

715 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