"This was stored as 8bit where 0 is completely transparant, and 255 is complete opaque."
This would be a lot more efficient:
<?php
$alpha7 = ((~((int)$alpha8)) & 0xff) >> 1;
?>
where alpha8==255, alpha7==0
where alpha8=127, alpha7== 64
where alpha8==0, alpha7==127
imagecolorallocatealpha
(PHP 4 >= 4.3.2, PHP 5)
imagecolorallocatealpha — Asigna un color para una imagen
Descripción
$image
, int $red
, int $green
, int $blue
, int $alpha
)
imagecolorallocatealpha() se comporta de forma idéntica a
imagecolorallocate() con la adición del parámetro de
transparencia alpha.
Parámetros
-
image -
Un recurso image, es devuelto por una de las funciones de creación de imágenes, como imagecreatetruecolor().
-
red -
Valor del componente rojo.
-
green -
Valor del componente verde.
-
blue -
Valor del componente azul.
-
alpha -
Un valor entre 0 y 127. 0 indica completamente opaco mientras que 127 indica completamente transparente.
Valores devueltos
Un identificador de color o FALSE si la asignación falló.
Esta función puede
devolver el valor booleano FALSE, pero también puede devolver un valor no booleano que se
evalúa como FALSE. Por favor lea la sección sobre Booleanos para más información. Use
el operador === para comprobar el valor devuelto por esta
función.
Historial de cambios
| Versión | Descripción |
|---|---|
| Antes de 5.1.3 | Devuelve -1 si la asignación falló. |
Ejemplos
Ejemplo #1 Ejemplo usando imagecolorallocatealpha()
<?php
$tamaño = 300;
$imagen=imagecreatetruecolor($tamaño, $tamaño);
// algo para obtener un fondo blanco con borde negro
$fondo = imagecolorallocate($imagen, 255, 255, 255);
$borde = imagecolorallocate($imagen, 0, 0, 0);
imagefilledrectangle($imagen, 0, 0, $tamaño - 1, $tamaño - 1, $fondo);
imagerectangle($imagen, 0, 0, $tamaño - 1, $tamaño - 1, $borde);
$amarillo_x = 100;
$amarillo_y = 75;
$rojo_x = 120;
$rojo_y = 165;
$azul_x = 187;
$azul_y = 125;
$radio = 150;
// asignar colores con valores alfa
$amarillo = imagecolorallocatealpha($imagen, 255, 255, 0, 75);
$rojo = imagecolorallocatealpha($imagen, 255, 0, 0, 75);
$azul = imagecolorallocatealpha($imagen, 0, 0, 255, 75);
// dibujar 3 círculos solapados
imagefilledellipse($imagen, $amarillo_x, $amarillo_y, $radio, $radio, $amarillo);
imagefilledellipse($imagen, $rojo_x, $rojo_y, $radio, $radio, $rojo);
imagefilledellipse($imagen, $azul_x, $azul_y, $radio, $radio, $azul);
// ¡no olvide imprimir la cabecera correcta!
header('Content-Type: image/png');
// y finalmente, imprimir el resultado
imagepng($imagen);
imagedestroy($imagen);
?>
El resultado del ejemplo sería algo similar a:
Notas
Nota: Esta función require GD 2.0.1 o superior (se recomienda 2.0.28 o superior).
Ver también
- imagecolorallocate() - Asigna un color para una imagen
- imagecolordeallocate() - Desasignar un color de una imagen
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
?>
If you need to calculate the integer representation of a color with an alpha channel, without initialising an image and using the imagecolorallocatealpha function. Then this function might be of some help:
<?php
function alphaColor($hexColor,$alpha)
{
return bindec(decbin($alpha).decbin(hexdec($hexColor));
}
echo alphaColor("FFFFFF",127);
?>
If you only wish to extract the alpha value for a color, you can simply extract it like so:
<?php
$color = imagecolorat($im, 50, 50);
$alpha = $color >> 24;
?>
It actually shifts off the first 24 bits (where 8x3 are used for each color), and returns the remaining 7 allocated bits (commonly used for alpha)
Check out this Source, it's a little funny feature showing you, for what imagecolorallocatealpha() is used:
<?php
$im=imagecreatetruecolor(300,300);
$white=imagecolorallocate($im,255,255,255);
imagefilledrectangle($im,0,0,imagesx($im),imagesy($im),$white);
for($i=0;$i<256;$i=$i+10)
{
$col=imagecolorallocatealpha($im,$i,$i,$i,ceil(rand(0,127)));
imagefilledellipse($im,$i,$i,$i,$i,$col);
}
header("content-type: image/png");
imagepng($im);
?>
