PHP-Imagemagick 图片显示

13

我有一段 PHP 代码,可以创建 PDF 缩略图,代码如下:

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

7个回答

16

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

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

然而,这种方法比较缓慢,因此我建议尽早生成并保存图像$img->writeImage($path)


13

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

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

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

9
我认为应该这样写:echo "<img src='data:image/jpg;base64,".base64_encode($thumbnail)."' />";,该代码将生成一个包含 $thumbnail 变量值的 base64 编码图片链接。 - Alfred
我同意@ashein的看法,最好的解决方案是生成缩略图并使用它们。 - Vasil Dakov

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

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 方法的文档


3
您可以在页面中嵌入原始图像,有关页面语法示例,请参见下面的博客条目。 http://www.sveinbjorn.org/news/2005-11-28-02-39-23 但我认为将缩略图保存在文件系统中并作为普通文件提供会更有效。否则,每次访问页面时都会生成缩略图。可能有人上传了此PDF文件,因此最好在上传时生成缩略图。

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

祝你好运。


0

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接