PHP-Imagemagick image display

With Imagick, you could use base64 encoding:

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

However, this method is kind a slow and therefore I recommend generating and saving the image earlier $img->writeImage($path).


Embedding an image using base64 is a COMPLETELY wrong way to go about the problem esp. with something stateless like a php web script.

You should instead use http parameters to have a single php file which can perform two tasks - the default will send html , and the parameter will instruct the php file to print the image. Below is the "standard" way to do it -

<?php
if (!array_key_exists('display',$_GET))
{
    print('<html><head></head><body><img src="'.$_SERVER['PHP_SELF'].'?display=image"></body></html>');
} else
{
    // The display key exists which means we want to display an image
    $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;
}
?>

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)."' />";