473,785 Members | 2,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Resizing GIFs preserving transparency

Hi!

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

<?php
// ...
$img = imagecreatefrom gif($path);
$img_scaled = imagecreate($ne w_width, $new_height);
imagecopyresamp led($img_scaled , $img, 0, 0, 0, 0, $new_width, $new_height,
$old_width, $old_height);
imagegif($img_s caled, $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
imagecopyresize d(). 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 3314
-----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 = imagecreatefrom gif($path);
| $img_scaled = imagecreate($ne w_width, $new_height);
| imagecopyresamp led($img_scaled , $img, 0, 0, 0, 0, $new_width, $new_height,
| $old_width, $old_height);
| imagegif($img_s caled, $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
| imagecopyresize d(). 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:

imagepalettecop y -- Copy the palette from one image to another

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

iD8DBQFB0uKVm2S xQ7JEbpoRAlT6AJ 4sdSGbE9uypqiY1 PojKaRFQgPT4gCe Lu6I
HkajvRuK2iuGzRM oEni0H9o=
=taJH
-----END PGP SIGNATURE-----
Jul 17 '05 #2
On 2004-12-29, Justin Koivisto <ju****@koivi.c om> wrote:
Matthias Czapla wrote:

| I want to resize uploaded GIF images. Currently I do it like this:
|
| <?php
| // ...
| $img = imagecreatefrom gif($path);
| $img_scaled = imagecreate($ne w_width, $new_height);
| imagecopyresamp led($img_scaled , $img, 0, 0, 0, 0, $new_width, $new_height,
| $old_width, $old_height);
| imagegif($img_s caled, $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
| imagecopyresize d(). 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:

imagepalettecop y -- Copy the palette from one image to another


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

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

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

<?php
// ...
$img = imagecreatefrom gif($path);
$img_scaled = imagecreate($ne w_width, $new_height);
imagecopyresamp led($img_scaled , $img, 0, 0, 0, 0, $new_width, $new_height,
$old_width, $old_height);
imagegif($img_s caled, $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
imagecopyresize d(). 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


imagecopyresamp led() doesn't work well with palette image. Create
$img_scaled using imagecreatetrue color() instead, then call
imagetruecolort opalette() before you save.

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

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

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

<?php
// ...
$img = imagecreatefrom gif($path);
$img_scaled = imagecreate($ne w_width, $new_height);
imagecopyresamp led($img_scaled , $img, 0, 0, 0, 0, $new_width, $new_height,
$old_width, $old_height);
imagegif($img_s caled, $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
imagecopyresize d(). 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


imagecopyresamp led() doesn't work well with palette image. Create
$img_scaled using imagecreatetrue color() instead, then call
imagetruecolort opalette() 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.
imagecopyresamp led() doesn't preserve alpha channel info if I remember
correctly. And there is no imagecopymerger esampled().

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
2868
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 less than 185pixels), display it in the center of the section if the width OR height is larger than 185pixel, resize them with aspect ratio maintained so both width and height is <= 185pixels. Then display it in the center of the section.
2
11259
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 too, but GIF is preferrable). JAI can do this sort of but can't write to GIF format. Also, it looks too complicated for what I need; I'm a bit in a hurry... Is there a simple way of doing what I want? Is there a free,
1
2953
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 Data Item for Databinding is a class with 2 props ... URL Description ***MyMainPage.aspx Page_Load Code ...
16
5142
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. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
0
1854
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.) Some of the GIFS were designed to play once, but in my VB application they loop continuously. What must I do to make them play only once? 2.) When I build the solution for distribution, how do I
3
1813
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 gifs look pretty bad. I assume it's something to do with the color palette. I don't know a whole lot about graphics programming. Can anyone assist? Here's the code I'm currently using.
3
4278
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 image with transparent backgroung draw = ImageDraw.Draw(im)
9
3784
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. First... the background image is in color and looks correct. The icons that I merge onto the background image are in black and white. I'm not sure why this is happening. This happens on both my development environment and on my server.
5
3426
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 it as an image with .gif extension. The only problem is that I need to add the animated gif onto another image (with imagecopymerge), however the gif image that is returned by this code in an object. Here is the code: GifEncoder.Class.php
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10341
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.