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

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

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:

  1. $imagick = new Imagick();
  2. // Initialize GoogleCloudStorage access
  3. $google_client_config = [
  4. 'projectId' => MY_PROJECT_ID,
  5. 'keyFilePath' => MY_SERVICE_KEY_PATH
  6. ];
  7. // get access to the bucket for object access, and enable the wrapper
  8. $storage_client = new StorageClient( $google_client_config );
  9. // allow reading with fopen, etc from 'gs://' scheme
  10. $storage_client->registerStreamWrapper();
  11. $bucket = $storage_client->bucket( MY_BUCKET_NAME );
  12. $bucket_path = gs://PATH_TO_MY_BUCKET
  13. $mls_id ='MT-NWMAR';
  14. $params = [
  15. 'prefix' => $mls_id,
  16. 'fields' => 'items/id,items/name,items/updated,nextPageToken',
  17. ];
  18. $objects = $bucket->objects($params);
  19. foreach ($objects as $object ) {
  20. if ( substr( $object->name(), -4 ) != '.jpg') continue;
  21. $full_file_path = "$bucket_path/" . $object->name();
  22. $imagick->clear(); // clear IM internals
  23. try {
  24. $imagick->readImage( $full_file_path );
  25. }
  26. catch ( Exception $ex ) {
  27. print "Exception for $full_file_path: " . $ex->getMessage() . "\n";
  28. }
  29. }

This throws exceptions for every file in the bucket:

  1. 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:

  1. gsutil cp gs://MY_BUCKET_NAME/MT-NWMAR/30006995/org/032.jpg .
  2. identify 032.jpg
  3. 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:

  1. $imagick = new Imagick();
  2. // Initialize GoogleCloudStorage access
  3. $google_client_config = [
  4. 'projectId' => MY_PROJECT_ID,
  5. 'keyFilePath' => MY_SERVICE_KEY_PATH
  6. ];
  7. // get access to the bucket for object access, and enable the wrapper
  8. $storage_client = new StorageClient( $google_client_config );
  9. // allow reading with fopen, etc from 'gs://' scheme
  10. $storage_client->registerStreamWrapper();
  11. $bucket = $storage_client->bucket( MY_BUCKET_NAME );
  12. $bucket_path = gs://PATH_TO_MY_BUCKET
  13. $mls_id ='MT-NWMAR';
  14. $params = [
  15. 'prefix' => $mls_id,
  16. 'fields' => 'items/id,items/name,items/updated,nextPageToken',
  17. ];
  18. $objects = $bucket->objects($params);
  19. foreach ($objects as $object ) {
  20. if ( substr( $object->name(), -4 ) != '.jpg') continue;
  21. $full_file_path = "$bucket_path/" . $object->name();
  22. $imagick->clear(); // clear IM internals
  23. try {
  24. $imagick->readImage( $full_file_path );
  25. }
  26. catch ( Exception $ex ) {
  27. print "Exception for $full_file_path: " . $ex->getMessage() . "\n";
  28. }
  29. }

This throws exceptions for every file in the bucket:

  1. 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:

  1. gsutil cp gs://MY_BUCKET_NAME/MT-NWMAR/30006995/org/032.jpg .
  2. identify 032.jpg
  3. 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:

  1. php v8.0.18
  2. 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:

确定