PHP-Imagemagick image display

我的未来我决定 提交于 2019-11-27 21:34:49

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

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 embed the raw image in you page, see the blog entry below for an example in page syntax.

http://www.sveinbjorn.org/news/2005-11-28-02-39-23

But i think it would be more productive to save the thumbnail on the filesystem and serve it as normal file. Otherwise you will be generating the thumbnail each time the page is accessed. Someone possibly uploaded this PDF file, so you may as well generate the thumbnail on upload time.

As I can see there are too many answers which are not accurate enough, so here goes mine:

This will print the image as you are doing it now(by the time of asking this question). As alternative to answer by @Vasil Dakov you should modify the snippet i gave you like this:

<?php
// ... Image generation goes here
header("Content-Type: image/jpeg"); 
ob_start();
print $im->getImageBlob();
$the_outputted_image = ob_get_flush();
?>
// Assuming that you use MVC approach and you are storing $the_outputted_image in a object and passing it to the view(ie. index.html or the HTML below the code).
//... Html code of index.html
<img src="data:image/jpg;base64 <?php print $the_outputted_image; ?>" alt="image" title="IMagick Generated Image" />

As another alternative is creating a script to generate the image, save it in some folder ( assuming img/ is the folder) and return only the path+filename+ extension to the file:

<?php
// ... Image generation goes here
header("Content-Type: image/jpeg"); 
$filename = 'img/' . md5(microtime()) . '.jpg'// Microtime is just as an example, you should use your own method.
$fp = fopen($filename, "x"); //Creating and opening the file for write-only  
$im->writeImageFile($fp); //Writing the image to the file pointer (I would recommend writing it using, fwrite(), because it is binary-safe writing method)
fclose($fp);
?>

// Html
<img src="<?php print $filename; ?>" alt="image" title="IMagick Generated Image" />

documentation for Imagick::writeImageFile

The only solution would be to convert your image to base64 and include it as an embedded base64 image (data:image/png;base64, ). Further reference.

But this isn't supported in IE 6 and 7.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!