note 103146 added to function.imagecolorallocatealpha

From: Date: Tue, 29 Mar 2011 01:57:40 +0000
Subject: note 103146 added to function.imagecolorallocatealpha
Groups: php.notes 
Request: Send a blank email to php-notes+get-178285@lists.php.net to get a copy of this message
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 ?> ---- Server IP: 92.48.74.199 Probable Submitter: 92.24.143.170 ---- Manual Page -- http://www.php.net/manual/en/function.imagecolorallocatealpha.php Edit -- https://master.php.net/note/edit/103146 Del: integrated -- https://master.php.net/note/delete/103146/integrated Del: useless -- https://master.php.net/note/delete/103146/useless Del: bad code -- https://master.php.net/note/delete/103146/bad+code Del: spam -- https://master.php.net/note/delete/103146/spam Del: non-english -- https://master.php.net/note/delete/103146/non-english Del: in docs -- https://master.php.net/note/delete/103146/in+docs Del: other reasons-- https://master.php.net/note/delete/103146 Reject -- https://master.php.net/note/reject/103146 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#178285) next »