473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 3290
-----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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.