PHP-Imagemagick image display

2020-01-31 03:49发布

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..

6条回答
啃猪蹄的小仙女
2楼-- · 2020-01-31 03:58

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.

查看更多
ら.Afraid
3楼-- · 2020-01-31 04:02

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.

查看更多
手持菜刀,她持情操
4楼-- · 2020-01-31 04:07

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

查看更多
够拽才男人
5楼-- · 2020-01-31 04:08

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;
}
?>
查看更多
Fickle 薄情
6楼-- · 2020-01-31 04:13

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

查看更多
别忘想泡老子
7楼-- · 2020-01-31 04:22

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)."' />";
查看更多
登录 后发表回答