“Imagemagick – 从谷歌云存储打开时出现“文件中图像数据不足”错误”

huangapple go评论96阅读模式
英文:

Imagemagick - "insufficient image data in file" error open from google cloud storage

问题

I'm writing a PHP script to do some analysis of files we have stored in Google Cloud storage, and am running into problems - every file throws an "insufficient image data in file" exception:

$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";
    }
}

This throws exceptions for every file in the bucket:

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

I can use gsutil to copy the file to my local machine, and identify works fine there:

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

Not sure why - I suspect maybe the wrapper isn't pulling the whole file in?

Running:

  • PHP v8.0.18
  • ImageMagick: 6.9.10-23 Q16 x86_64
英文:

I'm writing a PHP script to do some analysis of files we have stored in
Google Cloud storage, and am running into problems - every file throws
an "insufficient image data in file" exception:

$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";
    }
}

This throws exceptions for every file in the bucket:

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

I can use gsutil to copy the file to my local machine, and identify works fine there:

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

Not sure why - I suspect maybe the wrapper isn't pulling the whole file in?

running:

php v8.0.18

ImageMagick: 6.9.10-23 Q16 x86_64

答案1

得分: 0

$imagick->readImage( $full_file_path ); 期望本地文件名。不支持方案 gs://

使用存储 SDK 下载对象,然后加载到 Image Magick 中。

英文:

$imagick->readImage( $full_file_path ); expects a local filename. The scheme gs:// is not supported.

Use the storage SDK to download the object and then load it into Image Magick.

huangapple
  • 本文由 发表于 2023年6月6日 07:13:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定