致命错误:未捕获异常';ImagickException';带有消息';没有解码此图像格式的电报


Fatal error: Uncaught exception 'ImagickException' with message 'NoDecodeDelegateForThisImageFormat

我无法使imagick与本地examplep一起工作。

错误

Fatal error: Uncaught exception 'ImagickException' with message 'NoDecodeDelegateForThisImageFormat

代码

$im = new imagick();
$im->setResolution(300, 300);
$im->readimage($base_dir . 'files/PDF/test.pdf');
$im->setIteratorIndex($i);
$im->setImageFormat('jpeg');
$newimgname = time();
$im->resizeImage(500, 500, Imagick::FILTER_LANCZOS, 1);
$im->cropImage(100, 100, 0, 0);
$thumbnail = $im->getImageBlob();

Imagick调用ImageMagick库来完成它对图像的所有处理。Image Magick库本身实际上并不处理PDF,它调用GhostScript来处理它们,并生成PNG或Jpeg,然后Image Magick读取。

NoDecodeDelegateForThisImageFormat表示,Image Magick无法调用它认为应该将解码委派给GhostScript的委派程序。

解决方案是通过yum或apt安装GhostScript,它"应该"可以工作。

如果仍然不起作用,您应该检查Image Magick的代理文件中的内容(http://www.imagemagick.org/source/delegates.xml)对于PDF条目,并确保它可以从命令提示符调用,即检查Image Magick是否也能找到它。

来自我的服务器管理员:您可能想尝试GraphicsMagick作为Image Magick的替代方案。

Graphics Magick主页

即使在安装GhostScript之后,在从AWS切换到Azure之后,我们也无法使imagick代码工作,从而导致Delegate错误。我们最终将PHP代码转换为Image Magick命令行,使用函数execInBackground运行命令(PHP.net Exec()Page)

注意:命令行无法单独使用exec。php脚本不会正常终止。

//from PHP.net 
function execInBackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        exec($cmd . " > /dev/null &");   
    } 
}
//create a thumbnail from the first page of PDF
//old php code
/*
$image_magick = new imagick(); 
$image_magick->setbackgroundcolor('white');
$image_magick->readImage($file_path . "[0]");
$image_magick = $image_magick->flattenImages();
$image_magick->setResolution(300,300);
$image_magick->thumbnailImage(102, 102, true);
$image_magick->setImageFormat('jpg');
$image_magick->writeImage($thumbnail_path);
*/
//command line syntax
$cmd = "magick convert " . chr(34) . $file_path . "[0]" . chr(34) . " -background white -flatten -resample " . chr(34) . "300x300" . chr(34) . " -thumbnail " . chr(34) . "102x102" . chr(34) . " -format jpg -write " . chr(34) . $thumbnail_path . chr(34);
execInBackground($cmd);