A simple simple question but i cannot find the documentation on the php site.
I am working with ImageMagick and i am exporting pixels as CHAR
$pixels = $im->exportImagePixels(0, 0, $im->getImageGeometry()['width'], $im->getImageGeometry()['height'], "RGB", Imagick::PIXEL_CHAR);
I need to define new pixels and i want them to be of CHAR type. When i use var_dump on the sigle pixels PHP recognize them as integers. Now i see that php does not have a CHAR data type, so i want to define a two byte unsigned integer like this:
I have pixel defined as $pixel = 134; ranging from 0 to 255
I want to define the corresponding char like that:
0xFF & $pixel
It doesn't work.
Anyone has an idea why? or what can i do with that? I tried to use the PIXEL_FLOAT datatype but it's really heavy and it crashes my script on bigger pictures.
Thanks anyone.
Use the pack function:
function printPacked($base, $packed_value)
{
$value = unpack($base, $packed_value);
printf("%08x
", $value[1]);
}
// Unsigned char
//
$oldpixel = pack( "C", 0xFF );
printPacked("C", $oldpixel);
// Unsigned short, little-endian
//
$newpixel = pack( "v", 0xCDEF );
printPacked("v", $newpixel);
// Assign the short to the char
//
$oldpixel = $newpixel;
printPacked("C", $oldpixel);
// Mask operation on the char
//
$oldpixel = $oldpixel & pack( "C", 0xAB );
printPacked("C", $oldpixel);
The output of the program above is:
000000ff
0000cdef
000000ef
000000ab