PHP-Imagemagick图像显示

发布于 2024-11-08 23:11:28 字数 522 浏览 0 评论 0原文

我有创建 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;
?>

效果很好。但如果我想在网页中显示图像,我必须使用 标签。有什么方法可以删除 header("Content-Type: image/jpeg"); 使用 从语法和回显图像..?或者有人告诉我如何使用语法在网页内显示图像。

我在 Windows Vista PC 上运行 apache 和 php5。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

生生漫 2024-11-15 23:11:28

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

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

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

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

别靠近我心 2024-11-15 23:11:28

您可以尝试通过以下方式显示图像:

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

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

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)."' />";
葬シ愛 2024-11-15 23:11:28

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

您应该使用 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;
}
?>

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;
}
?>
暮凉 2024-11-15 23:11:28

您可以将原始图像嵌入到页面中,请参阅下面的博客文章以获取页面语法的示例。

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

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

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.

挽清梦 2024-11-15 23:11:28

正如我所看到的,有太多答案不够准确,所以这是我的:

这将打印您现在正在做的图像(在提出这个问题时)。作为@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" />

另一种选择是创建脚本来生成图像,将其保存在某个文件夹中(假设 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" />

Imagick 文档::writeImageFile

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

落花浅忆 2024-11-15 23:11:28

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

            $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

祝你好运。

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.

忆沫 2024-11-15 23:11:28

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

但 IE 6 和 7 不支持此功能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文