You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP Imagick报无解码委托错误,树莓派Ubuntu环境求助

PHP图片代理程序部署树莓派Ubuntu后Imagick处理AVIF图片报错

我在Stack Overflow上找过类似已解决问题,但还是没法修复自己的bug,所以来求助。我开发了一个PHP图片代理程序,本地Mac环境运行完全正常,但部署到运行Ubuntu 20.04.06 LTS的树莓派3后,出现了本地从未遇到的错误,推测是依赖缺失或配置问题。已经尝试执行以下命令安装依赖:

$ sudo apt install -y imagemagick php8.2-imagick
$ apt-get install libgraphicsmagick1-dev libmagickcore-dev libmagickcore-6.q16-2 libmagickcore-6.q16-2-extra
$ sudo apt install libmagickcore-6*

代码逻辑与错误情况

  • 当$compress为0时,图片不经过Imagick处理,能正常返回给客户端;
  • 当$compress≥1时,会抛出错误:

An error occurred: no decode delegate for this image format `' @ error/blob.c/BlobToImage/361

我已经大量搜索并尝试各种解决方案,但问题依然存在。我的核心代码如下:

<?php
try {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "https://example.com/image.avif");
    curl_setopt($ch, CURLOPT_USERAGENT, "My Cool User Agent Here");
    curl_setopt($ch, CURLOPT_REFERER, "https://example.com/image.avif");

    // CURL SETTINGS HERE....

    $imageData = curl_exec($ch);
    $imageInfo = getimagesizefromstring($imageData);
    $mimeType = $imageInfo['mime'];

    curl_close($ch);

    if ($imageData !== false) {
        if ( $compress > 0 ) {
            // header need to be commented-out for read error.
            // header('Content-Type: ' . $mimeType);
    
            $imagick = new Imagick();
            $imagick->readImageBlob($imageData);
            
            $originalWidth = $imagick->getImageWidth();
            $originalHeight = $imagick->getImageHeight();
            
            if ($originalWidth > 400 || $originalHeight > 400) {
                if ($originalWidth > $originalHeight) {
                    $newWidth = 400;
                    $newHeight = (int)($originalHeight * (400 / $originalWidth));
                } else {
                    $newHeight = 400;
                    $newWidth = (int)($originalWidth * (400 / $originalHeight));
                }

                $imagick->resizeImage($newWidth, $newHeight, Imagick::FILTER_LANCZOS, 1);
            }
            
            echo $imagick->getImageBlob();
        } else {
            header('Content-Type: ' . $mimeType);
            echo $imageData;
        }
        fclose($content);
    }
} 
?>

测试时代理的是AVIF格式的图片,我的目标是创建一个支持可选压缩功能的图片代理API,以节省用户带宽(已移除无关代码简化问题)。


内容的提问来源于stack exchange,提问作者もぐもぐぷりん

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 20:56:03