Binary image data not converting into image - php
$data = b"""
\x00\x00\x01\x00\x01\x00\x10\x10\x00\x00\x01\x00\x18\x00h\x03\x00\x00\x16\x00\x00\x00(\x00\x00\x00\x10\x00\x00\x00 \x00\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x00\x00\x00\x00d\x00\x00\x00d\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00X;ÿ%\x1Cï\t$÷\x07\x11ÿ$\x16ÿ\x11\x1Eü\x13\x19ôçó÷ÈÈø\x07\fû#\x1Fÿ\e\x15ÿ\x0E\x17û,$ú\x13\x17èAHÿODÿ%\x1Dÿ\t\x11ÿ\x18\x0Eú)\x04ô\x1C\n
ñ-\x1AóïÝÿµ±ý\f\tï\x1F\x07ï \x07ñ\x19\x11ø%\tÿ(\x1AÿTOÿFLó!\x1Cô21ý•›þ©÷—–þ““÷üôÿêåÿŽÿ£‘ü¦žõƒ“ý'\x1Dó\e\x1DóISôELõ\e\x0FñO?þìüÿüÿ÷ñüÿêüÿúþÿûþÿóúÿÿúÿøÿõÒíÿ,)ô\e\e÷ORú;Mø\x13\fýN4ÿðõÿÿûýÿüÿÿÿéÿþ÷üÿïÿÿîÿþÿþþþÛãÿ*\eÿ$\x15ÿWLü0Aü\r\rùL;üòùÿÿýþÏ°™’f\x1F•`.a+•g1ØÌ°ÿÿùÞáÿ$!ò\x15\x16ôACÿxÿYRùuëõü÷ÿþÿÎ¥„~6\x00C\x01#\x03‹3\x04àϨÿÿøëåübkëXañ}ÿÿÿòø÷ÿÿûÿÿÿéöïþͤ‹‹?\x00˜S\x03”M\x039\x11ÍÓ°øöõÿþûøùÿÿþýÿÿò‹}ÿLYûmoýøôÿÿþüǬl~A\x00‡D\x00E\x00z7\x06æØ£öÿéìîÿhe÷Saúˆ~ÿBFø\x00\fèD>÷ýüÿÿÿøÔ¨›”[\x1Ee*£c-•Z3ëÍÂÿÿôëãÿ,\x12ø\x06\fóFOöIIÿ\x12\x0FøM9þöþóÿÿôÿöÿÿþôÿøÿÿøÿÿüùý÷ÿûÿùìêÿ:\x14ÿ\x1F\x16ÿKRý?Gþ\x18\x1CñJBúîúÿöÿòúÿþÿÿþÿúýÿÿñýÿþñúýøÿùçóÿ7,ç\x1C ñCMþJAÿ&\x17ô6(ü”ŽÿŸ™ÿŸý üþûöçåñŸÿ˜•øŸ˜ÿŒÿ,&é&\x1AðYNÿQIÿ+\x1Cù\x16\x14ÿ\v\x11ø\t\tï\x06\fù\x0E\x0EöÑ÷ü©Ãÿ\x02\x00ô\x08\rò\f\x06í\n
\x0Fô\x18\x18ÿ&\x15ùXOÿ;Aþ"\x1Eé\x1C\x1Eø\x19 ó'\x1Aò\e\x13ô3#ýææÿµ°ÿ \x12ì+\x1Eö5\eÿ\x19\eõ\e ÿ$\x1DðFLÿýüÿÿþúøøÿøÿþÿþýîþÿíõÿÿÿóÿÿùêýÿúÿÿÿõÿöþ÷øÿþÿÿøûùÿ\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
"""
The above data is image binary data.
I am converting the above binary data into actual image. I have written the code as follows:
Note: I am using image intervention package - Laravel and I have written the code for displaying favicons of banks(Financial Institutions). This api is provided by Yodlee Interactive.
$img = Image::make($data)->save('images/favicons/icon1.jpg');
return $img->response();
OR
$img = Image::make($data)->save('images/favicons/icon1.ico');
return $img->response();
When I am executing the code, I am getting following error,
NotReadableException in Decoder.php line 91:
Unable to init from given binary data.
The response you receive is not byteArray, it is string so you probably have to first convert into byte array and then into image.
This should give you some pointers to start.
<?php
//Here write your code to get $byte_array
$data = base64_decode($byte_array);
$im = imagecreatefromstring($data);
header('Content-Type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
?>
Or you can probably refer to these links.
Converting a byte array into an image using PHP and HTML
http://www.zoubi.me/blog/php-get-image-byte-array
Please use the following sample code for getting the captcha image.
$bytes = array(Paste byte-array from Yodlee-API response here Ex: 0,2,3,4,1,1,1);
$byteArray = implode(array_map("chr", $bytes));
$imgData =base64_encode($byteArray);
$img = "<img src= 'data:image/jpeg;base64,$imgData' />";
print($img);
Hope this helps.
Regards,
Krithik N
Related
How to convert byte array to image and save it in folder using php ?
I'm using below code Here $data is a byte array. $decoded_data = base64_decode($data); $im = imagecreatefromstring($decoded_data); if ($im !== false) { header('Content-Type: image/png'); $image_nam = "png_".time().".png"; imagepng($im, $image_nam); imagedestroy($im); } But I don't get the exact output
try this simple code for save image into directory file_put_contents('/myfolder/imageName.jpg', base64_decode($img)); you can also try this $base_to_php = explode(',', $base64Img); $data = base64_decode($base_to_php[1]); $filepath = "/path/imageName.png"; // or image.jpg file_put_contents($filepath,$data);
I would suggest you to refer link - This could be possible an issue with metadata present in base64 string. As per source the problem could be due to data:image/png;base64, is included in binary string. The metadata present in the binary string results in invalid image data when the decoded back. Can you try it by removing metadata in the function before decoding the string. You may use function and write output to a new file. $position_s= strpos($base64_string , "data:image/png;base64,"); substr($base64_string, 0, $position_s) . substr($base64_string, $position_s+ strlen("data:image/png;base64,"); Note: If you are preferring explode function, it would be best to set limits: explode(',',$base64_string,2)
How to processing an image downloaded from AWS S3 with Laravel 5?
I want download an image from AWS S3 and process it with php. I am using "imagecreatefromjpeg" and "getimagesize" to process my image but it seem that Storage::disk('s3')->get(imageUrlonS3); retrieve the image in binary and is giving me errors. This is my code: function createSlices($imagePath) { //create transform driver object $im = imagecreatefromjpeg($imagePath); $sizeArray = getimagesize($imagePath); //Set the Image dimensions $imageWidth = $sizeArray[0]; $imageHeight = $sizeArray[1]; //See how many zoom levels are required for the width and height $widthLog = ceil(log($imageWidth/256,2)); $heightLog = ceil(log($imageHeight/256,2)); //more code here to slice the image . . . . } // ex: https://s3-us-west-2.amazonaws.com/bucketname/image.jpg $content = Storage::disk('s3')->get(imageUrlonS3); createSlices($content); What am I missing here ? Thanks
I think you are right in your question what the problem is - the get method returns the source of the image of itself, not the location of the image. When you pass that to createSlices, you're passing the binary data, not its file path. Inside of createSlices you call imagecreatefromjpeg, which expects a file path, not the image itself. If this indeed the case, you should be able to use createimagefromstring instead of createimagefromjpeg and getimagesizefromstring instead of getimagesize. The functions createimagefromstring and getimagesizefromstring each expects the binary string of the image, which I believe is what you have. Here's the relevant documentation: createimagefromstring - http://php.net/manual/en/function.imagecreatefromstring.php getimagesizefromstring - http://php.net/manual/en/function.getimagesizefromstring.php Resulting code might look something like this: function createSlices($imageData) { $im = imagecreatefromstring($imageData); $sizeArray = getimagesizefromstring($imageData); //Everything else can probably be the same . . . . } $contents = Storage::disk('s3')->get($imageUrlOnS3); createSlices($contents); Please note I haven't tested this, but I believe from what I can see in your question and what I read in the documentation that this might just do it.
Image from database into PDF using FPDF
I have an image that is sent from an iPad app to an SQL database. I can retrieve this image and display in a web page using the following php: $img = base64_encode($row['photoData']); echo "<img src=\"data:image/jpg;charset=utf8;base64, $img\"/>"; This displays fine. What I want to do now is put this image into a PDF document using FPDF however I am struggling to do this. This: $img = base64_encode($row['photoData']); $pdf->Image($img); give this error: FPDF error: Image file has no extension and no type was specified: So I tried this (although I realise I will then have to look at how to get the size of the image sorted): $pdf->Image($img, 20, 20, 20, 20 'JPG'); which give me: FPDF error: Missing or incorrect image file: What is the correct way to do this? Or would it be easier to temporarily save the image to the server and then place the saved image into the PDFdoc?
As mentioned in the comments above this is possible by using a stream ("data url") to hand over the image data to the fpdf library without writing physical files to disk: <?php // load the 'fpdf' extension require('fpdf.php'); // just for demonstration purpose, the OP gets the content from a database instead $h_img = fopen('img.jpg', "rb"); $img = fread($h_img, filesize('img.jpg')); fclose($h_img); // prepare a base64 encoded "data url" $pic = 'data://text/plain;base64,' . base64_encode($img); // extract dimensions from image $info = getimagesize($pic); // create a simple pdf document to prove this is very well possible: $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',16); $pdf->Cell(40,10,'Hello Image!'); $pdf->Image($pic, 10, 30, $info[0], $info[1], 'jpg'); $pdf->Output(); If this is a good advice is another question, this is merely meant to prove that this is possible...
According to the Docs FPDF::Image accepts a filename as the first argument, not a binary blob. If you want to use FPDF specifically, save the image to a temporary file first, and then pass that to FPDF::Image. To do that, something like this should work: $tmpFile = tempnam(sys_get_temp_dir(), 'fpdfimg'); if (file_put_contents($tmpFile, $row['photoData'])) { $fpdf->Image($tmpFile); // save/display image unlink($tmpFile); } Alternatively, if you want to just serve the image as a PDF (with no other content) you could use Imagick: $im = new \Imagick(); $im->readImageBlob($row['photoData']); $im->setImageFormat('pdf'); header('Content-Type: application/pdf'); echo $im;
Since FPDF cannot use base64 data to produce images on the PDF, I would recommend saving the file to the disk permanently as opposed to writing a temp file for every PDF operation. This will save you a lot of I/O overhead. Assuming your table has unique photo_id or photo_name to accompany photoData then you can use something like this to create your images and use them in FPDF. I will also assume you have a last_update and photo_extension column. <?php $path = '/path/to/fpdf/images/'; $filename = $row['photo_id'].'.'.$row['photo_extension']; $filepath = $path.$filename; // If a physical file is not available then create it // If the DB data is fresher than the file then make a new file if(!is_file($filepath) || strtotime($row['last_update']) > filemtime($filepath)) { $result = file_put_contents($filepath, $row['photoData']); if($result === FALSE) { die(__FILE__.'<br>Error - Line #'.__LINE__.': Could not create '.$filepath); } } $pdf->Image($filepath); If you plan on updating the photoData which is stored in your DB then you will have to make sure to also have a timestamp column and compare that timestamp against the filemtime($filepath) of the image on your disk.
Another solution for this ;) Make a new php by copying and pasting this (piece of fpdf's code edited): require('fpdf.php'); class DATAIMAGE extends FPDF { protected function _parsedata($file) { // Extract info from a JPEG file $a = getimagesizefromstring($file); if(!$a) $this->Error('Missing or incorrect image file: '.$file); if($a[2]!=2) $this->Error('Not a JPEG file: '.$file); if(!isset($a['channels']) || $a['channels']==3) $colspace = 'DeviceRGB'; elseif($a['channels']==4) $colspace = 'DeviceCMYK'; else $colspace = 'DeviceGray'; $bpc = isset($a['bits']) ? $a['bits'] : 8; return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$file); } } Then call this php instead of fpdf.php in your main php. You'll now be able to display an image simply by adding 'data' to the end of the function: $pdf->Image($mysqlrow["blob"],0,0,40,0,'data');
How to display a JSON/base64 encoded image in FPDF?
I'm storing signatures (using signaturepad in my database Coldfusion/MySQL 5.0.88 and would like to output the signature I'm taking onto a pdf which I'm generating with fpdf. However I can*t get it to work... Signatures are stored like this: [{"lx":19,"ly":58,"mx":19,"my":57},{"lx":23,"ly":54,"mx":19,"my":58},{"lx":26,"ly":53,"mx":23,"my":54},{"lx":32,"ly":51,"mx":26,"my":53},{"lx":38,"ly":47,"mx":32,"my":51},{"lx":44,"ly":44,"mx":38,"my":47},{"lx":51,"ly":41,"mx":44,"my":44},{"lx":58,"ly":37,"mx":51,"my":41},{"lx":64,"ly":35,"mx":58,"my":37},{"lx":67,"ly":31,"mx":64,"my":35},{"lx":70,"ly":30,"mx":67,"my":31},{"lx":72,"ly":28,"mx":70,"my":30},{"lx":71,"ly":28,"mx":72,"my":28},{"lx":69,"ly":28,"mx":71,"my":28},{"lx":66,"ly":28,"mx":69,"my":28},{"lx":62,"ly":29,"mx":66,"my":28},{"lx":59,"ly":31,"mx":62,"my":29},{"lx":55,"ly":32,"mx":59,"my":31},{"lx":52,"ly":33,"mx":55,"my":32},{"lx":48,"ly":35,"mx":52,"my":33},{"lx":44,"ly":37,"mx":48,"my":35},{"lx":41,"ly":38,"mx":44,"my":37},{"lx":39,"ly":40,"mx":41,"my":38},{"lx":36,"ly":40,"mx":39,"my":40},{"lx":33,"ly":42,"mx":36,"my":40},{"lx":32,"ly":43,"mx":33,"my":42},{"lx":31,"ly":44,"mx":32,"my":43},{"lx":31,"ly":46,"mx":31,"my":44},{"lx":32,"ly":48,"mx":31,"my":46},{"lx":136,"ly":23,"mx":32,"my":48},{"lx":132,"ly":24,"mx":136,"my":23},{"lx":104,"ly":38,"mx":132,"my":24},{"lx":103,"ly":40,"mx":104,"my":38},{"lx":102,"ly":41,"mx":103,"my":40},{"lx":102,"ly":42,"mx":102,"my":41},{"lx":103,"ly":42,"mx":102,"my":42},{"lx":108,"ly":42,"mx":103,"my":42},{"lx":115,"ly":42,"mx":108,"my":42},{"lx":123,"ly":39,"mx":115,"my":42},{"lx":133,"ly":36,"mx":123,"my":39},{"lx":141,"ly":34,"mx":133,"my":36},{"lx":148,"ly":32,"mx":141,"my":34},{"lx":155,"ly":30,"mx":148,"my":32},{"lx":159,"ly":29,"mx":155,"my":30},{"lx":161,"ly":28,"mx":159,"my":29},{"lx":159,"ly":27,"mx":161,"my":28},{"lx":155,"ly":27,"mx":159,"my":27},{"lx":151,"ly":27,"mx":155,"my":27},{"lx":145,"ly":27,"mx":151,"my":27},{"lx":140,"ly":29,"mx":145,"my":27},{"lx":135,"ly":31,"mx":140,"my":29},{"lx":132,"ly":33,"mx":135,"my":31},{"lx":127,"ly":36,"mx":132,"my":33},{"lx":125,"ly":38,"mx":127,"my":36},{"lx":125,"ly":40,"mx":125,"my":38},{"lx":125,"ly":41,"mx":125,"my":40},{"lx":125,"ly":42,"mx":125,"my":41},{"lx":127,"ly":43,"mx":125,"my":42},{"lx":131,"ly":44,"mx":127,"my":43},{"lx":139,"ly":45,"mx":131,"my":44},{"lx":147,"ly":45,"mx":139,"my":45},{"lx":157,"ly":43,"mx":147,"my":45},{"lx":164,"ly":41,"mx":157,"my":43},{"lx":173,"ly":39,"mx":164,"my":41},{"lx":181,"ly":36,"mx":173,"my":39},{"lx":186,"ly":34,"mx":181,"my":36},{"lx":191,"ly":33,"mx":186,"my":34},{"lx":193,"ly":30,"mx":191,"my":33},{"lx":194,"ly":29,"mx":193,"my":30},{"lx":194,"ly":28,"mx":194,"my":29},{"lx":193,"ly":27,"mx":194,"my":28},{"lx":191,"ly":26,"mx":193,"my":27},{"lx":188,"ly":25,"mx":191,"my":26},{"lx":183,"ly":25,"mx":188,"my":25},{"lx":180,"ly":25,"mx":183,"my":25},{"lx":177,"ly":25,"mx":180,"my":25},{"lx":174,"ly":27,"mx":177,"my":25},{"lx":171,"ly":30,"mx":174,"my":27},{"lx":169,"ly":32,"mx":171,"my":30},{"lx":168,"ly":34,"mx":169,"my":32},{"lx":167,"ly":36,"mx":168,"my":34},{"lx":167,"ly":38,"mx":167,"my":36},{"lx":168,"ly":40,"mx":167,"my":38},{"lx":169,"ly":41,"mx":168,"my":40},{"lx":171,"ly":41,"mx":169,"my":41},{"lx":174,"ly":41,"mx":171,"my":41},{"lx":176,"ly":41,"mx":174,"my":41},{"lx":176,"ly":40,"mx":176,"my":41},{"lx":177,"ly":39,"mx":176,"my":40},{"lx":177,"ly":37,"mx":177,"my":39}] There is a custom function included in siganturepad (signature to image) to convert the above back into an image. Like so: <?php require_once '../../signature-to-image.php'; $img = sigJsonToImage(file_get_contents('sig-output.json')); // Output to browser header('Content-Type: image/png'); imagepng($img); imagedestroy($img); > But if I try this on my fpdf page, the whole page breaks ( I guess because I want to embed the image in a cell not output only the image to the browser) and I get this: Resource id #10‰PNG IHDRÆ7Ø7¢¶±IDATxœí›iP[×Ç/’a6±£'ŒÀk0›ë݆6)NÒƉݸµM2î´nf:“d2gq“Æušq–Ió! ŽSâÄNJgRÛñ ’#1KOFf±õ´ ÀfÖÖ/}y‘Ä`çþ>x¬û®Gºÿ{Î=ç…¹.Pm¡ €Ük#IA(J B1PRŠ’‚P”„b ¤ %¡()Å#IA(J B1PRŠ’‚P”„b ¤ %¡()Å#IA(J B1PRŠ’‚Pc¡ €üÀðèT³Æ Pa×L'^z02œ¾Ð”Ô£·Œ+ÔX‹S¨0íÍb¼«wpåòô4,h ¤vK†ê¿Õe¨þÆà1Á óÓ¥bŽTÌÉËN™ÃL#u ªƒÇ•¾§Y‘±27½ZÂg%{^ ƒ? œNÚoQ¨°A†ê‡Fnâ–Dìyà¾=÷ß¹ >¿¤ýËáÆ[Óþ¿¥€—ºwKá/Ëy4Z1%*lvG{¹YmÀãÚØ”mÆiþ‹i|êN}c÷7}ß´öûiÃoÖ×dº-¹'“VÛSïž?)ïTKxÏ?.‰aFø˜ßÞcúJ¡;¥¼>:y°±ˆ{è÷kÙ‰1øÕ»XR£“wZԘŔZ÷Z£ÛUuíîäø¨y6iÒjSjJA†êÛºMV›ƒ¸ÄCX"Äîp6u}ìüS‡Î\× ªo잰άKß¼³wãk>lÞvàk9Š±¢#_ß³vKEŽŸ·¶9ŽžS½Qßj™xò+^Ýý3|ü®””idâí/ÛjOw:œ^fkɳ[KçÁ˜‘qk‹ÚЬ6ÈP}WŸÅîpâãaa#ÌM–ˆ2[*æho¿ö™RŽbÀo1án©®AõÝõA|„‡... If I'm doing this: if( strlen($unterschrift) > 0){ $img = sigJsonToImage( $unterschrift ); }; and try to output: if ( $imgProceed == "true" ){ $pdf->imagepng($img); } else { $pdf->Cell(50,4,'',0,1); } all I'm getting is: `Call to undefined method PDF::imagepng() `/ `strpos() expects parameter 1 to be string, resource` Can someone explain to me, what I'm doing wrong or what I need to do to include the image in a cell in fpdf? I'm clueless. Thanks!
The issue is here $pdf->imagepng($img); ^-------------- This should be an image path (String) Solution $file = 'signature.png' ; imagepng($img, $file); ^----------- Save Image to File Instead Then if ($imgProceed == "true") { $pdf->imagepng($file); } else { $pdf->Cell(50, 4, '', 0, 1); }
Originally, I was going to convert to PNG and the more I thought about it, it was more overhead then necessary. After all, Signature Pad give us everything we need to re-draw the signature in FPDF. I did the following: // decode the signature into an array $sig = json_decode($signatureInJSON); // for each line segment foreach ($sig as $line) { $lx = $line->lx; $ly = $line->ly; $mx = $line->mx; $my = $line->my; // draw the line $pdf->Line($lx, $ly, $mx, $my); } Obviously, I also added some functions to scale the signature and offset it to where I wanted in the PDF. Hope that helps someone else.
PDF to JPG Imagic page selection
Loads of answers on how to do it for a command line convert /path/to/file/file.pdf[3] output.jpg great... but what if I am using in memory processing, I am generating PDF with PDFlib and then output its buffer to a function that I want to generate jpg preview of selected page. How? My code : [...] $buf = $pdf->get_buffer(); //$buff is just a PDF stored in a string now. $im = new Imagick(); $im->readimageblob($buf); $im->setImageFormat("jpg"); $im->setimagecompressionquality(60); $len = strlen($im); header("Content-type: image/jpeg"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=test.jpg"); echo $im; This creates a jpeg but always returns last page of the PDF. I want to be able to choose which one will be converted. Is it doable without saving temporary files and using command line (exec('convert /path/to/file/file.pdf[3] output.jpg')) syntax? Let me add that I tried $im->readimageblob($buf[2]); and it did not work :)
For the ones who is still searching for solution of reading specific page number from blob, please check this question Creating array populated by images from a PDF using PHP and ImageMagick $img_array = array(); $im = new imagick(); $im->setResolution(150,150); $im->readImageBlob($pdf_in); $num_pages = $im->getNumberImages(); for($i = 0;$i < $num_pages; $i++) { $im->setIteratorIndex($i); $im->setImageFormat('jpeg'); $img_array[$i] = $im->getImageBlob(); } $im->destroy();
I'm loading the PDF binary into memory from Amazon S3 and then selecting the specific page I want using setIteratorIndex() followed by getImage() function get_image_from_pdf($pdf_bytes, $page_num){ $im = new \Imagick(); $im->setResolution(150, 150); $im->readImageBlob($pdf_bytes); $im->setIteratorIndex($page_num); $im = $im->getImage(); $im->setImageFormat('png'); return $im->getImageBlob(); }
version 3.0.1 being on the last image or first image in imagic object $image_obj = new Imagick("test.pdf"); then you on last image in $image_obj if you use fp_pdf = fopen("test.pdf", 'rb'); $image_obj = new Imagick(); $image_obj -> readImageFile($fp_pdf); then you on the first image in $image_obj In second case to switch to last image you can do fp_pdf = fopen("test.pdf", 'rb'); $image_obj = new Imagick(); $image_obj -> readImageFile($fp_pdf,2); // 2 can be any positive number? then you on the last image in $image_obj echo $image_obj->getNumberImages() // Returns the number of images in the object then if ($image_obj->hadPreviousImage) $image_obj->previousImage() //Switch to the previous image in the object } or if ($image_obj->hasNextImage()) { $image_obj->nextImage()) //Switch to the next image in the object } e.g. if you have 6 images total and you need 4th then do from the end $image_obj->previousImage() $image_obj->previousImage() $image_obj->setImageFormat("png"); header("Content-Type: image/png"); echo $image_obj; EDIT: Another find is that you can foreach($image_obj as $slides) { echo "<br>".$Obj_img->getImageWidth(); //or wehatever you need to do. } EDIT 2: Very simple solution would be to use this function $image_obj->setIteratorIndex(4) count starts with zero.
It's not good news unfortunately, but I can definitively say that, as of time of writing, the ImageMagick (and PHP libraries) don't support the page notation that you're trying to use. (For people from the future finding this: I'm checking php-imagick-3.0.1 and imagemagick-6.6.0.4). I'm trying to do the exact same thing as you, and I've just spent the last few hours trawling through the source, trying to figure out what it does and how it gets the pages, and it looks like it simply won't use it when reading from a stream (ie. the readBlob() call). As such, I'm just going to be putting it in a temporary file and reading it from there instead. Not as elegant, but it'll work.