note 79116 added to function.imagecolorat
| From: | alanhogandotcomslashcontact at osu1 dot php dot net | Date: | Mon, 12 Nov 2007 10:44:25 +0000 |
| Subject: | note 79116 added to function.imagecolorat | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132199@lists.php.net to get a copy of this message | ||
As creamdog noted before, the alpha channel IS available from this function! (The manual should
probably be updated to include this!)
$rgba = imagecolorat($im,$x,$y);
$alpha = ($rgba & 0x7F000000) >> 24;
$alpha will then contain the TRANSPARENCY (NOT OPACITY) level. So 127, the max, would be completely
transparent, and 0 would be completely opaque.
Using this information, it is possible to write a dithering png-to-gif function like the completely
working simple one below:
<?php
$im = imagecreatefrompng($pngRel);
$transparentColor = imagecolorallocate($im, 0xfe, 0x3, 0xf4 );
$height = imagesy($im);
$width = imagesx($im);
for($x = 0; $x < $width; $x++){
for($y = 0; $y < $height; $y++) {
$alpha = (imagecolorat($im,$x,$y) & 0x7F000000) >> 24;
//DITHER!
if ($alpha > 3 && (
$alpha >=127-3 ||
(rand(0,127))>=(127-$alpha)
)){
imagesetpixel($im,$x,$y,$transparentColor);
}
}
}
imagecolortransparent($im, $transparentColor);
imagegif($im, $gifRel);//save
header("Content-type: image/gif");
readfile($gifRel); //pass thru to browser
?>
----
Server IP: 69.147.83.197
Probable Submitter: 63.229.78.26
----
Manual Page -- http://www.php.net/manual/en/function.imagecolorat.php
Edit -- https://master.php.net/note/edit/79116
Del: integrated -- https://master.php.net/note/delete/79116/integrated
Del: useless -- https://master.php.net/note/delete/79116/useless
Del: bad code -- https://master.php.net/note/delete/79116/bad+code
Del: spam -- https://master.php.net/note/delete/79116/spam
Del: non-english -- https://master.php.net/note/delete/79116/non-english
Del: in docs -- https://master.php.net/note/delete/79116/in+docs
Del: other reasons-- https://master.php.net/note/delete/79116
Reject -- https://master.php.net/note/reject/79116
Search -- https://master.php.net/manage/user-notes.php