Imagick 的 readImageBlob 函数抛出类似 'No Decode Delegate For This Image Format' 的错误

readImageBlob function from Imagick is throwing an error like 'No Decode Delegate For This Image Format'

我正在做一个PHP项目,使用PHP版本5.4.7,它可以在线自定义图像并显示站点管理员的自定义。图片格式为png。 Imagick,php本机函数用于读取图像并将其保存为pdf。

下面显示的是我使用的代码片段。

$datadec = base64_decode($data);        
        $dpi = 300;
        $printable_width = ($dims['printable_width'] * $dpi)/25.4;
        $printable_height = ($dims['printable_height'] * $dpi)/25.4;

        $im = new Imagick();
        $im->setResolution(300, 300);
        $im->readImageBlob($datadec);
        $im->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);      
        $settings = include '../data/settings.php';
        $format = $settings['print_format'];

但是它会抛出类似

的错误

Type: ImagickException Code: 420 Message: NoDecodeDelegateForThisImageFormat `' @ error/blob.c/BlobToImage/358

而 Slim 错误跟踪就像

#0 C:\xampp\htdocs\XXXX\admin\controllers\OrdersController.php(151): Imagick->readimageblob('?PNG????????IHD...')
#1 C:\xampp\htdocs\XXXX\admin\controllers\OrdersController.php(305): OrdersController->convertHiRes('data:image/png;...', Array, '../storage/orde...')
#2 [internal function]: OrdersController->postPrint()

我也对 Imagick 安装有疑问。但是大部分论坛都是根据Linux环境来回答的。我是初学者,对这些命令不是很了解。

更新

实际上我使用 JavaScript:

从 canvas 元素中获取图像
var file = canvas.toDataURLWithMultiplier('png', multiplier, 1);

并将其发布到我之前提到的 php 文件中...我这样做是为了设置 $data...,

list($type, $data) = explode(';', $image);
list(, $data) = explode(',', $data); 

我在 Windows 环境中使用 XAMPP。

重要的是模块匹配。你有 php 5.4.7

这两个是我测试过的,很合身。

  • 在此处查找 5.4 Thread Safe (TS) x86 下载

https://pecl.php.net/package/imagick/3.1.2/windows

你只需要php_imagick.dll 找到 php_imagick.dll 并将其放入您的扩展文件夹
通常是 php/ext/.

打开C:\Windows\php.ini

如果存在则取消注释,如果不存在则添加此行

extension=php_imagick.dll

这是我唯一找到旧 6.6.5 的地方

  • 安装exe文件,建议在安装结束时做测试。将命令复制并粘贴到 cmd window.

  • 测试路径中的 ImageMagick-6.6.5-Q8

  • 重要文件随安装


  • Dependency Walker 测试 php_imagick.dll 所有 dll 都已就位。 都找到了,没有红色!


  • 测试 Php_info

  • 现在是重新启动计算机的好时机。

在 Netbeans 中测试

打开 netbeans 并创建一个新的 Project

您也可以 运行 index.php 使用 cmd

"G:\php\php.exe" "U:\Programme\NetBeans80\dtx\PngHeader\index.php"

index.php

<?php
$filename = "canvas.png";
$handle = fopen($filename, "r+");
$contents = fread($handle, filesize($filename));
fclose($handle);

$im = new Imagick();
$im->queryFormats();
$im->readImageBlob($contents);
/* Thumbnail the image */
$im->thumbnailImage(200, null);
/* Create a border for the image */
$im->borderImage(new ImagickPixel("white"), 5, 5);
/* Clone the image and flip it */
$reflection = $im->clone();
$reflection->flipImage();
/* Create gradient. It will be overlayed on the reflection */
$gradient = new Imagick();
/* Gradient needs to be large enough for the image and the borders */
$gradient->newPseudoImage($reflection->getImageWidth() + 10, $reflection->getImageHeight() + 10, "gradient:transparent-black");
/* Composite the gradient on the reflection */
$reflection->compositeImage($gradient, imagick::COMPOSITE_OVER, 0, 0);

/* Add some opacity. Requires ImageMagick 6.2.9 or later */
$reflection->setImageOpacity( 0.3 );

/* Create an empty canvas */
$canvas = new Imagick();

/* Canvas needs to be large enough to hold the both images */
$width = $im->getImageWidth() + 40;
$height = ($im->getImageHeight() * 2) + 30;
$canvas->newImage($width, $height, new ImagickPixel("black"));
$canvas->setImageFormat("png");

/* Composite the original image and the reflection on the canvas */
$canvas->compositeImage($im, imagick::COMPOSITE_OVER, 20, 10);
$canvas->compositeImage($reflection, imagick::COMPOSITE_OVER, 20, $im->getImageHeight() + 10);

$filename = "canvasNew.png";
$handle = fopen($filename, "wb");
if ($handle){
  fwrite( $handle, $canvas);
  fclose($handle);
}
?>

canvas.png

结果canvasNew.png

例子还有here