note 82223 added to function.imagecolortransparent
| From: | yousuf at philipz dot com | Date: | Tue, 01 Apr 2008 19:45:06 +0000 |
| Subject: | note 82223 added to function.imagecolortransparent | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137827@lists.php.net to get a copy of this message | ||
Well 8-bit gif and png files can have multiple transparent colors, so detecting the transparent
color by manually looking into the file or using imagecolortransparent and using the detected rgb
color as the background color when imagecopy'ing (or other image copying functions) on a true
color image will not work. The only means of getting the transparent pixels to appear is to copy the
pixel colors one by one to a new true color image. Here's some code i wrote to do this.
$original_image = @imagecreatefromgif( $original_image_file );
$original_image_width = imagesx( $original_image );
$original_image_height = imagesy( $original_image );
if ( !imageistruecolor($original_image) ) {
# detects if its non-true color image
if ( imagecolortransparent($original_image) >= 0 ) {
# detects if any of the color palette has been set to transparent
$truecolor = imagecreatetruecolor( $original_image_width, $original_image_height );
for ($x = 0; $x < $original_image_width; $x++) {
for ($y = 0; $y < $original_image_height; $y++) {
$color_index = ImageColorAt($original_image, $x, $y);
if ( !$color_palette[$color_index] ) {
$rgb = imagecolorsforindex($original_image, $color_index);
$color_to_set = imagecolorallocate($truecolor, $rgb['red'],
$rgb['green'], $rgb['blue']);
$color_palette[$color_index] = $color_to_set;
} else {
$color_to_set = $color_palette[$color_index];
}
imagesetpixel($truecolor, $x, $y, $color_to_set);
}
}
imagedestroy($original_image);
$original_image = $truecolor;
}
}
----
Server IP: 69.147.83.197
Probable Submitter: 86.96.227.70 (proxied: 92.96.255.208)
----
Manual Page -- http://www.php.net/manual/en/function.imagecolortransparent.php
Edit -- https://master.php.net/note/edit/82223
Del: integrated -- https://master.php.net/note/delete/82223/integrated
Del: useless -- https://master.php.net/note/delete/82223/useless
Del: bad code -- https://master.php.net/note/delete/82223/bad+code
Del: spam -- https://master.php.net/note/delete/82223/spam
Del: non-english -- https://master.php.net/note/delete/82223/non-english
Del: in docs -- https://master.php.net/note/delete/82223/in+docs
Del: other reasons-- https://master.php.net/note/delete/82223
Reject -- https://master.php.net/note/reject/82223
Search -- https://master.php.net/manage/user-notes.php