472,965 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,965 software developers and data experts.

Resizing GIFs preserving transparency

Hi!

I want to resize uploaded GIF images. Currently I do it like this:

<?php
// ...
$img = imagecreatefromgif($path);
$img_scaled = imagecreate($new_width, $new_height);
imagecopyresampled($img_scaled, $img, 0, 0, 0, 0, $new_width, $new_height,
$old_width, $old_height);
imagegif($img_scaled, $new_path);
// ...
?>

This works like expected except for transparency, which is gone after the
resizing. Furthermore the resulting smaller image doesnt appear to be
resampled nicely. For me its looking exactly like the result of
imagecopyresized(). I think this has something to do with the paletted
nature of GIFs but Im far from an expert in image manipulation. What I want
is a nice looking (i.e. resampled) downsized GIF with transparency if the
original image did have it. TIA for any help!

Regards
Matthias
Jul 17 '05 #1
4 3266
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Matthias Czapla wrote:

| Hi!
|
| I want to resize uploaded GIF images. Currently I do it like this:
|
| <?php
| // ...
| $img = imagecreatefromgif($path);
| $img_scaled = imagecreate($new_width, $new_height);
| imagecopyresampled($img_scaled, $img, 0, 0, 0, 0, $new_width, $new_height,
| $old_width, $old_height);
| imagegif($img_scaled, $new_path);
| // ...
| ?>
|
| This works like expected except for transparency, which is gone after the
| resizing. Furthermore the resulting smaller image doesnt appear to be
| resampled nicely. For me its looking exactly like the result of
| imagecopyresized(). I think this has something to do with the paletted
| nature of GIFs but Im far from an expert in image manipulation. What I
want
| is a nice looking (i.e. resampled) downsized GIF with transparency if the
| original image did have it. TIA for any help!
|
| Regards
| Matthias

I haven't done anything with GIF images, but I have a feeling you'll
need at least one additional function call for GIF images:

imagepalettecopy -- Copy the palette from one image to another

- --
Justin Koivisto - ju****@koivi.com
http://www.koivi.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFB0uKVm2SxQ7JEbpoRAlT6AJ4sdSGbE9uypqiY1PojKa RFQgPT4gCeLu6I
HkajvRuK2iuGzRMoEni0H9o=
=taJH
-----END PGP SIGNATURE-----
Jul 17 '05 #2
On 2004-12-29, Justin Koivisto <ju****@koivi.com> wrote:
Matthias Czapla wrote:

| I want to resize uploaded GIF images. Currently I do it like this:
|
| <?php
| // ...
| $img = imagecreatefromgif($path);
| $img_scaled = imagecreate($new_width, $new_height);
| imagecopyresampled($img_scaled, $img, 0, 0, 0, 0, $new_width, $new_height,
| $old_width, $old_height);
| imagegif($img_scaled, $new_path);
| // ...
| ?>
|
| This works like expected except for transparency, which is gone after the
| resizing. Furthermore the resulting smaller image doesnt appear to be
| resampled nicely. For me its looking exactly like the result of
| imagecopyresized(). I think this has something to do with the paletted
| nature of GIFs but Im far from an expert in image manipulation. What I
want
| is a nice looking (i.e. resampled) downsized GIF with transparency if the
| original image did have it. TIA for any help!

I haven't done anything with GIF images, but I have a feeling you'll
need at least one additional function call for GIF images:

imagepalettecopy -- Copy the palette from one image to another


Thanks for the reply. But unfortunately calling imagepalettecopy() after
imagecreate() does not solve the problem.

Regards
Matthias
Jul 17 '05 #3
"Matthias Czapla" <la*@dexato.mine.nu> wrote in message
news:sl****************@maus.intern...
Hi!

I want to resize uploaded GIF images. Currently I do it like this:

<?php
// ...
$img = imagecreatefromgif($path);
$img_scaled = imagecreate($new_width, $new_height);
imagecopyresampled($img_scaled, $img, 0, 0, 0, 0, $new_width, $new_height,
$old_width, $old_height);
imagegif($img_scaled, $new_path);
// ...
?>

This works like expected except for transparency, which is gone after the
resizing. Furthermore the resulting smaller image doesnt appear to be
resampled nicely. For me its looking exactly like the result of
imagecopyresized(). I think this has something to do with the paletted
nature of GIFs but Im far from an expert in image manipulation. What I want is a nice looking (i.e. resampled) downsized GIF with transparency if the
original image did have it. TIA for any help!

Regards
Matthias


imagecopyresampled() doesn't work well with palette image. Create
$img_scaled using imagecreatetruecolor() instead, then call
imagetruecolortopalette() before you save.

As for transparency, I'm not sure if it can be done with GD.
imagecopyresampled() doesn't preserve alpha channel info if I remember
correctly. And there is no imagecopymergeresampled().

Might want to try imagemagik instead.
Jul 17 '05 #4
On 2004-12-31, Chung Leong <ch***********@hotmail.com> wrote:
"Matthias Czapla" <la*@dexato.mine.nu> wrote in message
news:sl****************@maus.intern...
Hi!

I want to resize uploaded GIF images. Currently I do it like this:

<?php
// ...
$img = imagecreatefromgif($path);
$img_scaled = imagecreate($new_width, $new_height);
imagecopyresampled($img_scaled, $img, 0, 0, 0, 0, $new_width, $new_height,
$old_width, $old_height);
imagegif($img_scaled, $new_path);
// ...
?>

This works like expected except for transparency, which is gone after the
resizing. Furthermore the resulting smaller image doesnt appear to be
resampled nicely. For me its looking exactly like the result of
imagecopyresized(). I think this has something to do with the paletted
nature of GIFs but Im far from an expert in image manipulation. What I want
is a nice looking (i.e. resampled) downsized GIF with transparency if the
original image did have it. TIA for any help!

Regards
Matthias


imagecopyresampled() doesn't work well with palette image. Create
$img_scaled using imagecreatetruecolor() instead, then call
imagetruecolortopalette() before you save.


This produces indeed a nice resampled smaller version allthough with
wrecked transparency.
As for transparency, I'm not sure if it can be done with GD.
imagecopyresampled() doesn't preserve alpha channel info if I remember
correctly. And there is no imagecopymergeresampled().

Might want to try imagemagik instead.


Not a solution since the requirements for the application should be
restricted to just a LAMP system where the user may not have the possibility
to install additional software. But thank you very much for your help.

Regards
Matthias
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ruby Tuesday | last post by:
I have a section(185pixelsx 185pixels) in my web page to display an image that is stored in a directory. Using php, how do you resize so if: the image dimension is smaller(width and height is...
2
by: Clyde Ellul | last post by:
Hi there. I need to write a simple program that reads a GIF image from an input stream, resizes it, then writes it back to an output stream in the same format (GIF). (JPEG input/output is good...
1
by: Neil Woodvine | last post by:
***Scenario ... I have a DataList with a hyperlink WebControl in the Item Template. I want to display a 64x64 image in the Hyperlink and set the NavigateURL to the full size image. ***Source...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
0
by: GrandpaB | last post by:
I am creating a small simulation in VB and wish to incorporate several animated GIFS. I can load the GIFS from the hard drive into a picture box and they animate, but I have two questions. 1.)...
3
by: engwar | last post by:
I'm writing some file upload code using C# for users on my website. If the image is too large I want to resize it smaller to a thumbnail size. It's working fine for jpegs but the thumbnails of the...
3
by: toto | last post by:
Hi, Does anybody know how to save an image in GIF format preserving transparent background ?? Here's what I tested : import Image, ImageDraw im = Image.open('/path/to/model.gif') # An...
9
by: cdriccio | last post by:
Hello, I'm trying to make a gif image out of a total of 5 (currently) images. One background image and 4 small icons. This image also includes text of a specific font. Here are my issues. ...
5
by: helraizer1 | last post by:
Me again :D You're going to be sick of me soon. =P I found the GifEncoder class on phpclasses, which creates an animated gif image from frames and echos it to the browser to show; can also save...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.