PHP-Imagemagick图像显示
我有创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用 Imagick,您可以使用 base64 编码:
但是,这种方法有点慢,因此我建议先生成并保存图像
$img->writeImage($path)
。With Imagick, you could use base64 encoding:
However, this method is kind a slow and therefore I recommend generating and saving the image earlier
$img->writeImage($path)
.您可以尝试通过以下方式显示图像:
you can try to display the image by this way:
使用 base64 嵌入图像是解决问题的完全错误的方法,尤其是。使用无状态的东西,比如 php web 脚本。
您应该使用 http 参数来拥有一个可以执行两个任务的 php 文件 - 默认情况下将发送 html ,并且该参数将指示 php 文件打印图像。以下是“标准”方法 -
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 -
您可以将原始图像嵌入到页面中,请参阅下面的博客文章以获取页面语法的示例。
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.
正如我所看到的,有太多答案不够准确,所以这是我的:
这将打印您现在正在做的图像(在提出这个问题时)。作为@Vasil Dakov 回答的替代方法,您应该像这样修改我给您的代码片段:
另一种选择是创建脚本来生成图像,将其保存在某个文件夹中(假设 img/ 是文件夹)并仅返回文件的路径+文件名+扩展名:
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:
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:
documentation for Imagick::writeImageFile
就我而言,我找到了这样的解决方案:
祝你好运。
In my case I found out a solution like this:
good luck.
唯一的解决方案是将图像转换为 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.