By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,826 Members | 2,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,826 IT Pros & Developers. It's quick & easy.

Resizing GIFs preserving transparency

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
-----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

P: n/a
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

P: n/a
"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

P: n/a
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.