php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33914 Resizing palette based images with transparency bit loose transparency
Submitted: 2005-07-29 02:37 UTC Modified: 2005-07-29 23:12 UTC
From: me at thomaskeller dot biz Assigned: pajoye (profile)
Status: Not a bug Package: GD related
PHP Version: 4.3.11 OS: Linux 2.4 shared hosting
Private report: No CVE-ID: None
 [2005-07-29 02:37 UTC] me at thomaskeller dot biz
Description:
------------
Following use case:
Uploaded images should be shrinked to a certain size according to our guidelines.

Works with jpeg/ png (not palette based) images without a problem, problems arise when used palette based (8bit) images. Resizing via imagecopyresampled/ imagecopyresize works only for images WITHOUT a transparency color set properly. If a transparency color is set in the image, the value of this color is just ignored and a random background color is applied to the "transparent" area. It makes no difference if the image is outputted as gif or 8bit png.

If imagecopyresampled/imagecopyresized is called with the same width/height for the source and the target image, the transparency keeps intact.



Reproduce code:
---------------
I've setuped a few things on a test server:

PHPInfo:
 http://thomaskeller.biz/work/php/PHP_GIF/info.php
Testfile:
 http://thomaskeller.biz/work/php/PHP_GIF/image.php
Testfile (Source):
 http://thomaskeller.biz/work/php/PHP_GIF/image.phps

image.php loads http://thomaskeller.biz/work/php/PHP_GIF/rose.gif by default, but you can load an own gif file via URL like

image.php?filename=http://your.server.com/file.gif

The loaded image is per default resized to its double width/ height.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-29 17:19 UTC] pajoye@php.net
Provide a sample (and little) script and the images causing your problem may help.

For what you said, the "random" color may in fact be the background. You have to set the transparent color yourself to the dest image.

Short version, it's not a bug as far as I can tell now.

I keep this bug to bogus, if you have a reproducable script with one image, turn it back to open.
 [2005-07-29 17:36 UTC] me at thomaskeller dot biz
I have already provided a sample script, which is available on
http://thomaskeller.biz/work/php/PHP_GIF/image.php, the source for this file is  http://thomaskeller.biz/work/php/PHP_GIF/image.phps [0].

The test image which fails to be scaled is http://thomaskeller.biz/work/php/PHP_GIF/rose.gif

As you can see here [0] the problematic code seems to be (shortened):

<?php
$idx = imagecolortransparent($im_src);
if ($idx > -1) // transparent color found?
{
    list($r, $g, $b) = imagecolorsforindex($im_src,$idx);
    $transColor = imagecolorexact($im_target, $r, $g, $b);
    imagecolortransparent($im_target, $transColor);
}
?>

where I try to receive the index of the transparent color
of the original image, then find the rgb value of this color and finally create a new color on the target image using the rgb value and set it as transparent color.
 [2005-07-29 18:19 UTC] pajoye@php.net
Again it's not a bug.

You have to set the transparent color correctly in the destination image.

--Pierre
 [2005-07-29 23:12 UTC] me at thomaskeller dot biz
> Again it's not a bug.   
> You have to set the transparent color correctly    
> in the destination image.   
  
So what means "correctly" here? I've tried several  
dozens of combination to get the desired result  
but failed with each. I copied palettes over, tried to  
copy into a 32bit target image while later shrinking  
it back via imagetruecolortopalette, and more.  
I'll promise I'll shut up if you just give me a few   
lines of CORRECT code. The docs don't help me on my  
problem.  
  
I've also noticed that a list($r, $g, $b) =  
imagecolorsforindex(...) doesn't work as suspected,  
because all three variables are unset. This is my  
current code:  
  
<?php  
$im_source = imagecreatefromgif($somefile);  
$im_target = imagecreate($width, $height);  
$idx = imagecolortransparent($im_source);  
$rgba = imagecolorsforindex($im_source, $idx);  
imagecopyresampled($im_target,$im_source, 0, 0, 0, 0,  
  $new_width, $new_height, $orig_width, $orig_height);  
$transColor = imagecolorallocate($im_target,  
  $rgba['red'], $rgba['green'], $rgba['blue']);   
imagecolortransparent($im_target, $transColor);  
header("Content-type: image/gif");  
imagegif($im_target);  
exit;  
?>  
  
imagecolortransparent on $im_target afterwards returns  
the index for the transparent color (white), but still the  
background of the image has another color (rgb 128,64,88).  
If I set this color to be transparent, nothing happens  
(its still there).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 18:01:34 2024 UTC