3

I have this ImageMagick error with one of the images my site is trying to convert:

{ Error: Command failed: convert: no decode delegate for this image format `/tmp/925bf249f8297827f51f0370642eb560.jpg' @ error/constitute.c/ReadImage/544.
convert: no images defined `/tmp/abdf362d-f7eb-435f-bafe-5a134be0235f.png' @ error/convert.c/ConvertImageCommand/3046.
at ChildProcess.<anonymous> (/var/task/node_modules/imagemagick/imagemagick.js:88:15)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:886:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5) timedOut: false, killed: false, code: 1, signal: null }

The weird part is that it's happening only in my AWS Lambda function, not on my machine (Mac). I am reading about versioning, reinstalling ImageMagick and stuff, but I can't do that in Lambda runtime environment. Is there any way around this?

Lee Maan
  • 699
  • 3
  • 10
  • 30
  • 1
    Have you installed Image Magik into the Lambda package that you deploy? See [this post](https://stackoverflow.com/questions/44729088/how-can-i-install-graphicsmagick-or-imagemagick-on-aws-lambda) for more details. – stdunbar Dec 14 '18 at 16:58
  • 1
    You may have to provide the full path to ImageMagick convert in AWS as it may not use the same ENVIRONMENT variables as the basic OS such as on the MAC. – fmw42 Dec 14 '18 at 17:56
  • 1
    What is the real format of the failing image? There are really two JPEG formats: JFIF ("traditional", produced by image editors) and Exif (more recent, produced by cameras). The `file` command will distinguish them (`JPEG image data, Exif standard` v.s. `JPEG image data, JFIF standard 1.01`). Or the file is a Jpeg2000, or even something unrelated (PNG, GIF, TIFF...). – xenoid Dec 14 '18 at 22:49
  • @stdunbar I don't know what you mean. ImageMagick is installed locally and it's in my package.json. I thought it's enough for Lambda to install it and use it. – Lee Maan Dec 17 '18 at 15:31
  • @fmw42 the error shown in my question means ImageMagick is working but it's lacking a decode delegate for a certain image. But I don't quite know how to add such delegate to Lambda. I only believe it all comes with ImageMagick, right? – Lee Maan Dec 17 '18 at 15:32
  • @xenoid the image is JPG but a super weird type! My Preview in Mac couldn't open it. Even Photoshop couldn't. Also `magick identify` can't read it to get metadata. Yet the browser can open it! – Lee Maan Dec 17 '18 at 15:35
  • Care to pose the image somewhere? Or do `cat image.jpg | hexdump -C | head -16` (ie, 16 first lines of the hex dump) and add this to your question? – xenoid Dec 18 '18 at 20:29
  • @xenoid Sure! This is an example of an image: https://customizer.hypernode.io:8443/storage/customer_uploads/abi68a73v1mf0h1jdof75q6ms7/1cf53ced62e4854778d28b2ee442c9ec.jpg Is it possible to use the command you wrote from within AWS Lambda instance? I'm not aware of any way to do that. – Lee Maan Dec 20 '18 at 09:16
  • The image is not a JPG... It ends in .JPG, but is actually a [WebP image](https://en.wikipedia.org/wiki/WebP). – xenoid Dec 20 '18 at 12:41
  • So still, no one answered your question. I'm running into the same problem, identifying that this is a webp doesn't fix or change anything, imagemagick detects the file type and has no delegate for webp on the version of imagemagick on lambda. Any one else have a real answer...? – Farley Sep 24 '19 at 05:53
  • @Farley I remember I ended up adding the binaries manually with the repo, ditching the library that uses ImagMagic and using the binaries with Node's child_process. Along with that I had to use other libraries to tackle to all edge cases. – Lee Maan Nov 28 '19 at 12:15

2 Answers2

2

Don't trust file extensions blindly. The image provided in not a Jpeg. You can download it to another system where you can check it using file or else. In the case at hand it is a WebP image (WebP is a new image format pushed by Google).

One possible cause of the confusion is that Web servers generate the Mime-type from the file extension, so the WebP image is returned with a mime type of image/jpeg, and this is normally trusted blindly by most software (including your browser).

xenoid
  • 8,396
  • 3
  • 23
  • 49
  • Thanks for the answer. I know about the real type of the image. Some users simply rename files to be able to upload them. The issue here is that ImageMagick does recognise the real format of the image and converts it to PNG (what I intend to do). It only doesn't do that in AWS Lambda due to lack of image delegate as the error shows. The challenge is only to install the delegates to kill this issue. – Lee Maan Dec 20 '18 at 21:49
1

Through some much wasted time, I figured out this is a flaw in the built-in ImageMagick that Amazon has on Lambda by default. It's older and doesn't have a decoder for WebP. If you use the following layer as an overlay for your imagemagick, and then use it instead of the built in one it will fix your problems.

https://github.com/serverlesspub/imagemagick-aws-lambda-2

Before I overlaid the above layer I was getting...

b"identify: no decode delegate for this image format `/tmp/downloaded_file' @ error/constitute.c/ReadImage/544.\n"

Afterwards...

b'/tmp/downloaded_file WEBP 740x493 740x493+0+0 8-bit sRGB 377646B 0.000u 0:00.000\n'

Most of the other answers and comments are focusing on something that is completely irrelevant, which is that the image file format is actually a webp, when imagemagick has built-in detectors for file formats and it ignores file's extension anyways, so this info is irrelevant to ImageMagick.

Enjoy anyone else that runs into this problem!!!

Farley
  • 1,400
  • 1
  • 8
  • 13
  • Thanks for the answer! I actually forgot how I solved that problem back then :D But will keep this in record just in case. – Lee Maan Sep 24 '19 at 16:10