Convert and Display Array of Bytes to Grayscale JPG

MagickWand for PHP is an object-oriented PHP interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning MagickWand for PHP.
Locked
coolcatco888

Convert and Display Array of Bytes to Grayscale JPG

Post by coolcatco888 »

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.

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 );

coolcatco888

Re: Convert and Display Array of Bytes to Grayscale JPG

Post by coolcatco888 »

Okay, so this code does not even work and it is off of the API page. http://www.magickwand.org/MagickSetImagePixels.html

I have to admit, this has a potential to be a great tool but the documentation and support really BLOWS the big one! Only 1 example of the code usage in the entire site. Plus, I see a lot of 0 replies on this forum.

How do I use the MagickSetImageFormat() function? Please just give an example of code that works that creates a tiny image from a pixel array.

Code: Select all

	$magick_wand = NewMagickWand();
	MagickSetWandSize($magick_wand, 200, 200);
	MagickSetImagePixels( $magick_wand, 34, 77, 4, 8, 'BRG', MW_CharPixel, array( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //pixels (34,77) (35,77) (36,77) (37,77) 
	32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, //pixels (34,78) (35,78) (36,78) (37,78) 
	64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, //pixels (34,79) (35,79) (36,79) (37,79) 
	128,128,128, 128,128,128, 128,128,128, 128,128,128, //pixels (34,80) (35,80) (36,80) (37,80) 
	160,160,160, 160,160,160, 160,160,160, 160,160,160, //pixels (34,81) (35,81) (36,81) (37,81) 
	192,192,192, 192,192,192, 192,192,192, 192,192,192, //pixels (34,82) (35,82) (36,82) (37,82) 
	224,224,224, 224,224,224, 224,224,224, 224,224,224, //pixels (34,83) (35,83) (36,83) (37,83) 
	255,255,255, 255,255,255, 255,255,255, 255,255,255 //pixels (34,84) (35,84) (36,84) (37,84) 
	) );
    MagickSetImageFormat($magick_wand, 'jpg');
    header('Content-Type: image/jpeg');
    MagickEchoImageBlob( $magick_wand );

coolcatco888

Re: Convert and Display Array of Bytes to Grayscale JPG

Post by coolcatco888 »

Ok, so here's the solution, if you are wanting to generate an image from binary data, you must load an image first. Infact, before you can create any images you must load some sort of blank image.

This would be handy for oh, lets say you are making a graphing program using PHP.

Sorry for bashing MagickWand earlier, well, the documentation still leaves much to be desired but hopefully this helps.

Code: Select all

//Use ImageMagick to format and display the image in jpg format
    $magick_wand = NewMagickWand();
    MagickReadImage($magick_wand, 'blank_hint.jpg');//This needs to be loaded in order to set pixels
    MagickSetImagePixels($magick_wand, 0, 0, $row->cols, $row->rows, 'I',//Indicates the single byte pixel value is a grayscale value
            MW_CharPixel,//Specifies that there is 1 byte per pixel value
            $pixel_array);
    MagickSetImageFormat($magick_wand, 'jpg');
    header('Content-Type: image/jpeg');
    MagickEchoImageBlob( $magick_wand );

Locked