note 103146 deleted from function.imagecolorallocatealpha by cmb
| From: | cmb@php.net | Date: | Sun, 22 Dec 2019 14:24:40 +0000 |
| Subject: | note 103146 deleted from function.imagecolorallocatealpha by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213607@lists.php.net to get a copy of this message | ||
Note Submitter: hosh [underscore] 90 [at] live [dot] com
----
I have had a case where I got the alpha from a raw RGBA pallete. This was stored as 8bit where 0 is
completely transparant, and 255 is complete opaque.
This is impossible to use with imagecolorallocatealpha() as it requires a 7bit int where 0 is
completely opaque and 127 is completely transparant (otherway around)
You can solve this by subtracting 255, removing the negative sign (either by converting to string
and substr($alpha, 1) or some other way) and then bitshifting the answer to the right by one 1 bit.
Example:
<?php
$alpha = 0; // equivalent to alpha 127 needed for imagecolorallocatealpha()
$alpha = $alpha - 255; // subtract 255, this will give a negative number
$alpha = substr($alpha, 1); // remove negative/minus sign
$alpha = (int)$alpha; // convert back to integer
$alpha = $alpha >> 1; // bitshift to the right once.
echo $alpha; // output: 127
// This can be done in one line of course:
$alpha = 255; // equivalent to 0 for imagecolorallocatealpha()
$alpha = ((int)(substr($alpha - 255, 1))) >> 1;
echo $alpha; // outputs 0
?>