php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38764 GD imagecopyresampled() AND imagecopyresized() bug
Submitted: 2006-09-09 21:45 UTC Modified: 2006-09-09 23:53 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: markg852 at hotmail dot com Assigned: pajoye (profile)
Status: Not a bug Package: GD related
PHP Version: 5.1.6 OS: Windows XP, linux
Private report: No CVE-ID: None
 [2006-09-09 21:45 UTC] markg852 at hotmail dot com
Description:
------------
This bug can be found when trying to make a thumbnail from a url (example: http://promote.opera.com/logos/Opera5_t.png) the png isn`t even transperant anymore unless these 3 lines of code are also added: 

$background = imagecolorallocate($thumb, 0, 0, 0);
ImageColorTransparent($thumb, $background); // make the new temp image all transparent
imagealphablending($thumb, false); // turn off the alpha blending to keep the alpha channel

i tried everything i could thing of but the quality stays different..

What the code below does is the following:
1. It downloaded the opera logo png image and makes a thumbnail of the URL!!
2. (after hitting F5) the code will make another thumbnail from the downloaded full scale image.
3. (after hitting F5 2nd time) the code will show both thumbnails and look at the quality difference!! and without the 3 lines posted above the "ugly" image has a black background!!

Reproduce code:
---------------
<?php
// Error Reporting
error_reporting(E_ALL);

// File and new size
if (file_exists('thumb_van_url.png') AND file_exists('thumb_van_path.png'))
{
    echo '<img src="./thumb_van_url.png"></img>';
    echo "Thumbnail made from URL";
    echo '<img src="./thumb_van_path.png"></img>';
    echo "This one is made from a PATH";
    exit;
}
elseif (file_exists('./Opera5_t.png'))
{
    $filename = 'Opera5_t.png';
    $mid = 'van_path';
}
else
{
    $filename = 'http://promote.opera.com/logos/Opera5_t.png';
    $mid = 'van_url';
    imagepng(imagecreatefrompng('http://promote.opera.com/logos/Opera5_t.png'), 'Opera5_t.png');
}

$percent = 0.1;

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefrompng($filename);

$background = imagecolorallocate($thumb, 0, 0, 0);
ImageColorTransparent($thumb, $background); // make the new temp image all transparent
imagealphablending($thumb, false); // turn off the alpha blending to keep the alpha channel

// Resize
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagepng($thumb, 'thumb_' . $mid . '.png');
echo '<img src="./thumb_' . $mid . '.png"></img>';


// Content type
// header('Content-type: image/png');
// readfile('thumb_' . $mid . '.png');
?>



Expected result:
----------------
http://img393.imageshack.us/img393/2796/uitkomstvc4.png


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-09-09 21:47 UTC] markg852 at hotmail dot com
The bug is in imagecopyresampled() AND imagecopyresized()!!
 [2006-09-09 21:51 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.2-win32-latest.zip


 [2006-09-09 22:13 UTC] pajoye@php.net
There is no bug.

You copy an image to a another image. The destination image has no transparent color defined. True color images are "filled" with black by default, that's why you see black instead of the transparent color.

short version: all you need to do is:
ImageColorTransparent($thumb, 0);




 [2006-09-09 22:16 UTC] pajoye@php.net
doh, submit too quickly

all you have to do:
imagecolortransparent($thumb, 0);
imagealphablending($thumb, false);

the fist to set the bgd color, the second function to keep the alpha information while copying (no blend ops).
 [2006-09-09 22:28 UTC] markg852 at hotmail dot com
@ tony2001@php.net
i even tested the 6.0.0-dev snapshot and it still has the exact same issue.

@ pajoye@php.net
tested your suggestion and the result is still exactly the same.

and just to be sore.. i`m talking about the QUALITY!!! that`s just different.. just look at the image i supplied in the first image.

Could this be a bug in GD itself (so not php`s fault) or in php_gd.dll ?
 [2006-09-09 23:28 UTC] pajoye@php.net
First of all no need to be loud.

It was not a suggestion but a fact.

In this image "uitkomstvc4.png", the left sude shows the artifacts resulting from a wrong/no transparent color.


Just try:

$thumb = imagecreatetruecolor($newwidth, $newheight);
imagefill($thumb,0,0, 0xffffff);
.. do the copy/save with transparent color or blend mode..

and then do the copy, you will have what you have on the right side. It is all about alpha component and background color of two separate images.

Again, there is no bug, not in the gd and not in php.

 [2006-09-09 23:53 UTC] markg852 at hotmail dot com
Thanx alot that worked how i wanted it.
but it might be a idea to put something like this in the php documentation aswell.. maybe here: http://nl3.php.net/manual/en/function.imagefill.php i couldn`t find it anywhere and people on the ##php channal (irc) on freenode couldn`t offer a solution aswell.. so this might be verry unknown.

Again Thanx
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 09:01:27 2024 UTC