PHP-Imagemagick image display

Alfred picture Alfred · May 22, 2011 · Viewed 33.6k times · Source

I have php code which create pdf thumbnail as follows;

<?php
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("png");
$im->resizeImage(200,200,1,0);
header("Content-Type: image/jpeg");
$thumbnail = $im->getImageBlob();
echo $thumbnail;
?>

Which is working well. But if I want to display the image in a web page, I have to use <img src=""> tag. Is there any way to remove header("Content-Type: image/jpeg"); from the syntax and echo image using <img src="">..? Or anybody tell me how to use the syntax to display the image inside a web page.

I am running apache with php5 in my Windows Vista PC..

Answer

Vasil Dakov picture Vasil Dakov · May 22, 2011

you can try to display the image by this way:

// start buffering
ob_start();
$thumbnail = $im->getImageBlob();
$contents =  ob_get_contents();
ob_end_clean();

echo "<img src='data:image/jpg;base64,".base64_encode($contents)."' />";