在PHP中输出Imagick图像的原始图像

时间:2022-08-01 21:19:29

I'm using Imagick lib to do some modifications to original image. Then I'd like to output it directly to browser without saving. Is there a way to do that?

我正在使用Imagick lib对原始图像进行一些修改。然后我想直接将它输出到浏览器而不保存。有没有办法做到这一点?

I tried to use Imagick::writeImage('STDOUT') (empty output) and 'php://stdout' with error "Unable to write to file".

我试图使用Imagick :: writeImage('STDOUT')(空输出)和'php:// stdout',错误“无法写入文件”。

Any ideas? :)

有任何想法吗? :)

4 个解决方案

#1


23  

You just need to echo your imagick object:

你只需要回应你的想象对象:

$img = new Imagick($file);
header('Content-Type: image/'.$img->getImageFormat());
echo $img;

#2


2  

I believe what Schneck meant is:

我相信Schneck的意思是:

header('Content-Type: '.$mime_type);
$fp = fopen('php://stdout', 'w+');
$image->writeImageFile($fp);
fclose($fp);

This may cause problems, especially in multipage files, so you should probably use:

这可能会导致问题,尤其是在多页文件中,因此您应该使用:

ob_start();
$fp = fopen('php://output', 'w+');
$image->writeimagefile($fp); // or writeImagesFile for multipage PDF
fclose($fp);
$out = ob_get_clean();
header('Content-Length: '.strlen($out));
echo $out;

This is tested and working.

这是经过测试和运作的。

I realize this is an old question. The reason I'm posting this is that if you want to write a multipage PDF, this is the solution, as Nabab's solution only works for single images.

我意识到这是一个老问题。我发布这个的原因是,如果你想写一个多页PDF,这就是解决方案,因为Nabab的解决方案只适用于单个图像。

You can also use (PHP 5.1.0+):

您也可以使用(PHP 5.1.0+):

$fp = fopen('php://temp', 'r+');
$image->writeimagesfile($fp);
rewind($fp);
$out = stream_get_contents($fp);
header('Content-Length: '.strlen($out));
fclose($fp);
echo $out;

This way seems to be faster if your PHP version supports it.

如果您的PHP版本支持它,这种方式似乎更快。

Before you downvote, the reason I added this as an answer and not a comment is I don't have sufficient reputation to comment. Hope that is alright. Just wanted to get this out there in case anyone needed it.

在你投票之前,我之所以添加这个作为答案而不是评论的原因是我没有足够的声誉来评论。希望没关系。只是想把它拿出去,万一有人需要它。

#3


2  

If you're using HHVM it'll throw a fatal exception if you just try and echo the Imagick object so you'll need to call getimageblob();

如果您正在使用HHVM,如果您只是尝试回显Imagick对象,那么它将抛出一个致命的异常,因此您需要调用getimageblob();

$imagick = new Imagick('my-image.jpg');
header('Content-Type: image/' . $imagick->getImageFormat());
echo $imagick->getimageblob();

#4


-1  

Have you tried Imagick::writeImageFile("php://stdout") (untested)?

你试过Imagick :: writeImageFile(“php:// stdout”)(未经测试)吗?

#1


23  

You just need to echo your imagick object:

你只需要回应你的想象对象:

$img = new Imagick($file);
header('Content-Type: image/'.$img->getImageFormat());
echo $img;

#2


2  

I believe what Schneck meant is:

我相信Schneck的意思是:

header('Content-Type: '.$mime_type);
$fp = fopen('php://stdout', 'w+');
$image->writeImageFile($fp);
fclose($fp);

This may cause problems, especially in multipage files, so you should probably use:

这可能会导致问题,尤其是在多页文件中,因此您应该使用:

ob_start();
$fp = fopen('php://output', 'w+');
$image->writeimagefile($fp); // or writeImagesFile for multipage PDF
fclose($fp);
$out = ob_get_clean();
header('Content-Length: '.strlen($out));
echo $out;

This is tested and working.

这是经过测试和运作的。

I realize this is an old question. The reason I'm posting this is that if you want to write a multipage PDF, this is the solution, as Nabab's solution only works for single images.

我意识到这是一个老问题。我发布这个的原因是,如果你想写一个多页PDF,这就是解决方案,因为Nabab的解决方案只适用于单个图像。

You can also use (PHP 5.1.0+):

您也可以使用(PHP 5.1.0+):

$fp = fopen('php://temp', 'r+');
$image->writeimagesfile($fp);
rewind($fp);
$out = stream_get_contents($fp);
header('Content-Length: '.strlen($out));
fclose($fp);
echo $out;

This way seems to be faster if your PHP version supports it.

如果您的PHP版本支持它,这种方式似乎更快。

Before you downvote, the reason I added this as an answer and not a comment is I don't have sufficient reputation to comment. Hope that is alright. Just wanted to get this out there in case anyone needed it.

在你投票之前,我之所以添加这个作为答案而不是评论的原因是我没有足够的声誉来评论。希望没关系。只是想把它拿出去,万一有人需要它。

#3


2  

If you're using HHVM it'll throw a fatal exception if you just try and echo the Imagick object so you'll need to call getimageblob();

如果您正在使用HHVM,如果您只是尝试回显Imagick对象,那么它将抛出一个致命的异常,因此您需要调用getimageblob();

$imagick = new Imagick('my-image.jpg');
header('Content-Type: image/' . $imagick->getImageFormat());
echo $imagick->getimageblob();

#4


-1  

Have you tried Imagick::writeImageFile("php://stdout") (untested)?

你试过Imagick :: writeImageFile(“php:// stdout”)(未经测试)吗?