471,624 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,624 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 3172
-----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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ruby Tuesday | last post: by
2 posts views Thread by Clyde Ellul | last post: by
reply views Thread by GrandpaB | last post: by
3 posts views Thread by engwar | last post: by
3 posts views Thread by toto | last post: by
1 post views Thread by ZEDKYRIE | last post: by

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.