11

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

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

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

4

7 回答 7

13

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

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

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

于 2012-11-22T14:18:28.757 回答
12

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

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

echo "<img src='data:image/jpg;base64,".base64_encode($contents)."' />";
于 2011-05-22T13:32:37.960 回答
5

使用 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;
}
?>
于 2012-11-29T11:38:39.817 回答
3

您可以在页面中嵌入原始图像,请参阅下面的博客条目以获取页面语法示例。

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

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

于 2011-05-22T13:33:02.080 回答
3

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

这将像您现在一样打印图像(在提出这个问题时)。作为@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 的文档

于 2013-05-17T10:18:13.730 回答
1

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

            $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

祝你好运。

于 2020-04-20T22:50:33.580 回答
0

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

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

于 2011-05-22T13:35:50.770 回答