略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Imagick::getImageBlob

2024-12-21

Imagick::getImageBlob

(PECL imagick 2, PECL imagick 3)

Imagick::getImageBlobReturns the image sequence as a blob

说明

public Imagick::getImageBlob(): string

Implements direct to memory image formats. It returns the image sequence as a string. The format of the image determines the format of the returned blob (GIF, JPEG, PNG, etc.). To return a different image format, use Imagick::setImageFormat().

参数

此函数没有参数。

返回值

Returns a string containing the image.

错误/异常

错误时抛出 ImagickException。

add a noteadd a note

User Contributed Notes 3 notes

up
7
Vee W
6 years ago
For who want to show "animated" gif. This getImageBlob() will showing only one frame of image.
Use getImagesBlob() instead.

Example:

<?php
$Imagick
= new \Imagick($real_source_image_path_animated_gif);
header("Content-Type: image/gif");
echo
$Imagick->getImagesBlob();
?>
up
2
Trevor
10 years ago
It seems that this method can return a zero-length string if faced with large-ish data.  No exceptions are thrown.

<?php
  $image
= new Imagick();
  if (!
$image->newImage(1000,1,'white')) throw new Exception();
  if (!
$image->scaleImage(0,200)) throw new Exception();
  print
"Image size: {$image->getImageWidth()},{$image->getImageHeight()}\n";
  if (!
$image->setImageFormat("jpeg")) throw new Exception();
 
$a = $image->getImageBlob();
  print
"Rendered to ".strlen($a)." bytes\n";
?>

Restrict your output image size, or ensure that the blob you get back isn't empty.  (Note that IM seems to be doing the work, it delays for some time.  But there's no indication of any error anywhere.)
up
1
jim at jimohalloran dot com
10 months ago
Further to Trevor's note above about getImageBlob potentially returning an empty string for large images. 

This seems image format dependent.  I have a large (12046x8363) image which is returned ok.  But if I call setImageFormat('pdf') and then call getImageBlob() again.  Example:

<?php
// Image Manipulation here.
$pngData = $imagick->getImageBlob();  // Returns a large PNG.
$imagick->setImageFormat('pdf');
$pdfData = $imagick->getImageBlob(); // Returns empty string immediately
?>

官方地址:https://www.php.net/manual/en/imagick.getimageblob.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3