PHP-Imagemagick 图像显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6087569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:19:23  来源:igfitidea点击:

PHP-Imagemagick image display

phpimagemagickimagick

提问by blasteralfred Ψ

I have php code which create pdf thumbnail as follows;

我有如下创建 pdf 缩略图的 php 代码;

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

哪个运行良好。但是如果我想在网页中显示图像,我必须使用<img src="">标签。有什么方法可以header("Content-Type: image/jpeg"); 使用<img src="">..从语法和回显图像中删除吗?或者有人告诉我如何使用语法在网页中显示图像。

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

我在我的 Windows Vista PC 中使用 php5 运行 apache ..

采纳答案by Vasil Dakov

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

回答by sluijs

With Imagick, you could use base64 encoding:

使用 Imagick,您可以使用 base64 编码:

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

但是,这种方法有点慢,因此我建议尽早生成和保存图像$img->writeImage($path)

回答by Chetan Chauhan

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

使用 base64 嵌入图像是解决问题的完全错误的方法,尤其是。使用无状态的东西,比如 php web 脚本。

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 -

您应该使用 http 参数来拥有一个可以执行两个任务的单个 php 文件 - 默认将发送 html ,并且该参数将指示 php 文件打印图像。以下是执行此操作的“标准”方法-

<?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;
}
?>

回答by DaGhostman Dimitrov

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:

这将打印您现在正在执行的图像(在提出此问题时)。作为@Vasil Dakov 回答的替代方案,您应该像这样修改我给您的代码片段:

<?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:

另一种选择是创建一个脚本来生成图像,将其保存在某个文件夹中(假设img/是文件夹)并仅返回文件的路径+文件名+扩展名:

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

Imagick::writeImageFile 的文档

回答by ashein

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

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.

但我认为将缩略图保存在文件系统上并将其作为普通文件提供会更有效率。否则,您将在每次访问页面时生成缩略图。有人可能上传了此 PDF 文件,因此您最好在上传时生成缩略图。

回答by Alp Altunel

In my case I found out a solution like this:

就我而言,我找到了这样的解决方案:

            $im = new Imagick("http://www.yourserver.com/upload/file_name.pdf");
            $im->setResolution(300, 300);     // if higher image will be good to read
            $im->setIteratorIndex(0); // read first page
            $im->setImageFormat('jpg');
            header('Content-Type: image/jpeg');

            ob_start();
            print $im->getImageBlob(); 
            $contents =  ob_get_contents();
            ob_end_clean();

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

good luck.

祝你好运。

回答by Henrik P. Hessel

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.

唯一的解决方案是将您的图像转换为 base64 并将其作为嵌入的 base64 图像 ( data:image/png;base64,)包含在内。进一步参考

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

但这在 IE 6 和 7 中不受支持。