Convert and Display Array of Bytes to Grayscale JPG
Posted: 2010-10-07T15:26:11-07:00
Ok, I want to create a grayscale jpg image from an array of bytes (I can specify the width and height). I retrieve a blob from the database which is just a series of bytes where each byte specifies a grayscale value 0...255. How do I do this using ImageMagick for PHP?
Here is what I have, $row->data is a blob from the database.
Here is what I have, $row->data is a blob from the database.
Code: Select all
$char_array = str_split($row->data);
$pixel_array = array();
foreach ($char_array as $char) {
$pixel_array[] = ord($char);
}
$magick_wand = NewMagickWand();
MagickSetWandSize($magick_wand, $row->cols, $row->rows);
//Fails on the line directly below this one
MagickSetImagePixels($magick_wand, 0, 0, $row->cols, $row->rows, 'III', MW_CharPixel, $pixel_array);// Fails here
MagickSetImageFormat($magick_wand, 'jpg');
header('Content-Type: image/jpeg');
MagickEchoImageBlob( $magick_wand );