我正在开发一个 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 线程安全 (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 窗口中。
测试路径中的
ImageMagick-6.6.5-Q8
。
- 重要文件随安装
- 使用
Dependency Walker
测试php_imagick.dll
所有dll
都已就位。 都找到了,没有红色!
- 使用 Php_info 进行测试
- 现在是重新启动计算机的好时机。
在 Netbeans 中测试
打开netbeans并创建一个新的Project
你也可以用cmd运行index.php
"G:\php\php.exe" "U:\Programme\NetBeans80\dtx\PngHeader\index.php"
索引.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
关于php - Imagick 的 readImageBlob 函数抛出类似 'No Decode Delegate For This Image Format' 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32440529/