bla*_*d Ψ 10 php imagemagick imagick
我有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;
?>
Run Code Online (Sandbox Code Playgroud)
哪个运作良好.但是如果我想在网页中显示图像,我必须使用<img src="">标记.有没有办法header("Content-Type: image/jpeg");
从语法和回声图像中删除<img src="">...?或者任何人告诉我如何使用语法在网页中显示图像.
我在我的Windows Vista PC上用php5运行apache ..
Vas*_*kov 10
您可以尝试以这种方式显示图像:
// start buffering
ob_start();
$thumbnail = $im->getImageBlob();
$contents = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/jpg;base64,".base64_encode($contents)."' />";
Run Code Online (Sandbox Code Playgroud)
slu*_*ijs 10
使用Imagick,您可以使用base64编码:
echo '<img src="data:image/jpg;base64,'.base64_encode($img->getImageBlob()).'" alt="" />';`
Run Code Online (Sandbox Code Playgroud)
但是,这种方法很慢,因此我建议先生成并保存图像$img->writeImage($path).
小智 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;
}
?>
Run Code Online (Sandbox Code Playgroud)