|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-10-27 04:17 UTC] seth at pricepages dot org
Description:
------------
imagecopyresampled() should be copying the alpha channel, but
it doesn't seem to be doing so. This is a palette based source
image being copied to a true color image.
If you use imagecopy() instead, the image copies as expected
(mostly).
Reproduce code:
---------------
<?php
$small = imagecreatefrompng('http://leopold.sage.wisc.edu/test.png');
$width = 300;
$height = 300;
$srcW = imagesx($small);
$srcH = imagesy($small);
$img = imagecreatetruecolor($width, $height);
//Make a transparent canvas
$trans = imagecolorresolve($img,255,255,255);
imagecolortransparent($img, $trans);
imagealphablending($img, false);
imagefilledrectangle( $img,
0, 0,
$width, $height,
$trans);
//This shouldn't *need* to be on, but it does
imagealphablending($img, true);
//One of these works, the other doesn't
//imagecopy($img, $small, 0,0, 0,0, $srcW, $srcH);
imagecopyresized($img, $small, 0,0, 0,0, $width, $height, $srcW, $srcH);
header('Content-Type: image/png');
imagepng($img);
?>
Expected result:
----------------
An enlarged, pixellated, mostly transparent, image.
Actual result:
--------------
A black, opaque, image.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 23 21:00:01 2025 UTC |
There is nothing wrong in imagecopyresize (or imagecopy). The problem you have is the misunderstanding of what is the background color, the alpha channel and alpha blending. Your original image has many black colors, one is transparent (what you consider as background), and the other with various transparency levels. Try the code below, it will explain you what is your image and how it works. <?php $small = imagecreatefrompng('39273.png'); print_r(imagecolortransparent($small)); $width = 300; $height = 300; $srcW = imagesx($small); $srcH = imagesy($small); print_r(imagecolorsforindex($small, imagecolorat($small, 0,0))); // one of the ""black"" pixel print_r(imagecolorsforindex($small, imagecolorat($small, 37,1))); imagecolortransparent($small, 1); $img = imagecreatetruecolor($width, $height); $trans = imagecolorresolve($img,255,255,255); imagefill($img, 0,0, $trans); imagecolortransparent($img, $trans); imagecopyresized($img, $small, 0,0, 0,0, $width, $height, $srcW, $srcH); imagepng($img, '1.png');