英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论