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

使用Imagemagick读取GCS文件触发‘insufficient image data in file’错误

问题:PHP脚本读取GCS中图片时触发“insufficient image data in file”异常

我编写PHP脚本分析存储在Google Cloud Storage(GCS)里的图片文件,但所有文件都会抛出“insufficient image data in file”异常。

我的PHP代码如下:

$imagick = new Imagick();

// Initialize GoogleCloudStorage access
$google_client_config = [
    'projectId'   => MY_PROJECT_ID,
    'keyFilePath' => MY_SERVICE_KEY_PATH
];

// get access to the bucket for object access, and enable the wrapper
$storage_client = new StorageClient( $google_client_config );

// allow reading with fopen, etc from 'gs://' scheme
$storage_client->registerStreamWrapper();

$bucket = $storage_client->bucket( MY_BUCKET_NAME );
$bucket_path = gs://PATH_TO_MY_BUCKET

$mls_id ='MT-NWMAR';
$params = [
    'prefix' => $mls_id,
    'fields' => 'items/id,items/name,items/updated,nextPageToken',
];

$objects = $bucket->objects($params);
foreach ($objects as $object ) {
    if ( substr( $object->name(), -4 ) != '.jpg') continue;

    $full_file_path = "$bucket_path/" . $object->name();

    $imagick->clear();  // clear IM internals
    try {
        $imagick->readImage( $full_file_path );
    }
    catch ( Exception $ex ) {
        print "Exception for $full_file_path: " . $ex->getMessage() . "\n";
    }
}

所有文件触发的异常信息一致:

Exception for gs://MY_BUCKET_NAME/MT-NWMAR/30006995/org/032.jpg: insufficient image data in file `/tmp/magick-25748xWZCfvQg3MVj' @ error/jpeg.c/ReadJPEGImage/1117

我通过gsutil将文件复制到本地后,用identify命令可以正常识别图片:

gsutil cp gs://MY_BUCKET_NAME/MT-NWMAR/30006995/org/032.jpg .
identify 032.jpg 
032.jpg JPEG 1920x1080 1920x1080+0+0 8-bit sRGB 694478B 0.000u 0:00.000

我怀疑问题可能出在流包装器没有拉取完整文件,当前运行环境:

php v8.0.18

ImageMagick: 6.9.10-23 Q16 x86_64

内容的提问来源于stack exchange,提问作者Andy Wallace

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 15:50:12