Invalid JPEG Marker causes Panel to crash

It took me a little while that it was a faulty JPG (invalid markers) that made the Panel crash (Kirby 2.5.7)

get_resource_type() expects parameter 1 to be resource, null given

Whoops\Exception\ErrorException 
/vendor/getkirby/toolkit/vendors/abeautifulsite/SimpleImage.php:57

get_resource_type
/vendor/getkirby/toolkit/vendors/abeautifulsite/SimpleImage.php:57

abeautifulsite\SimpleImage __destruct
[internal]0

ā€“ this is the log in Debug mode. In non-Debug mode, the panel just freezes, so yourā€™re unable to save / exit / delete the faulty files.

I am not sure if this qualifies as ā€˜bugā€™ (itā€™s the JPGs that are faulty after all), thatā€™s why I am posting it here. But it would to be great ā€“ especially for clients ā€“ to get a warning, (e.g. ā€œinvalid fileā€), instead of the whole panel just crashing.

Does the jpg work fine in graphics programs? Does it work ok with things like imageoptim?

Im not sure how easy it would be to setup, but does it work with Simple Image outside of Kirby? (this is what kirby uses to process the images.)

Also, is your server up to date, because SimpleImage depends on the common GD library.

What exactly was wrong with the image? I take it that the file was uploaded after all, but that trying to create a thumb from it caused the Panel to crash.

Thanks for your feedback. I just tested, and the files seem to work fine with imageoptim ā€“ however, I canā€™t open them with Photoshop (Error message). Iā€™ve also tried running a mogrify command, and get the following errors:

mogrify: profile 'icc': 'CMYK': invalid ICC profile color space 'ardhamatsyendrasana.png' @ warning/png.c/MagickPNGWarningHandler/1709.

and

mogrify: Unknown Adobe color transform code 2 'janushirshasana-2.jpg' @ warning/jpeg.c/JPEGWarningHandler/365.

I probably wonā€™t go through the hassle of setting up Simle Image locally, as itā€™s really just about 3 faulty images in my case; I just thought Iā€™d report it to the forum, as the bug causes a nasty crash of the panel.

I created an issue on GitHub. Donā€™t know if this can be fixed in the Panel though or if this is something that needs to be fixed in the SimpleImage class.

What happens if you try to resize such an image in a frontend template?

Thanks for creating the issue on GitHub.

When I try to output these images with a frontend template, Iā€™m basically getting the exact same error as in the panel:

Whoops\Exception\ErrorException thrown with message "get_resource_type() expects parameter 1 to be resource, null given"

Stacktrace:
#2 Whoops\Exception\ErrorException in /Users/ld/Documents/git/testsite.com/kirby/vendor/getkirby/toolkit/vendors/abeautifulsite/SimpleImage.php:57
#1 get_resource_type in /Users/ld/Documents/git/testsite.com/kirby/vendor/getkirby/toolkit/vendors/abeautifulsite/SimpleImage.php:57
#0 abeautifulsite\SimpleImage:__destruct in [internal]:0

ā€“ this is in Debug Mode; if I disable the Debug mode, all images are displayed, but the faulty ones are not resized, as opposed to other ā€œnon-faultyā€ JPGs.

That is the result I expected. Somehow Kirby/the Panel seems to accept the image (thatā€™s why it is uploaded without error) but the SimpleImage class doesnā€™t.

As a work around, If you can get the image to open in a graphics program, save it as RGB and try again with Kirby. I see you tried Photoshop but give Gimp or Affinity Photo a try. ImageOptim seemed to like it, so maybe try the result of imageoptim in Kirby rather then the source file.

Ive had issues my self with CMYK images in general (not with Kirby, but in general the common libraries used by most CMS system can get a bit funny with CMYK images).

Feel free to send me your source file, iā€™m happy to have a mess with it.

The problem is that this does not help the editor because there is very little he/she can do if the Panel crashes (especially when without access to the file system to remove the file).

Well sure, but in the absence of the issue being fixed, I cant think of another way around it.

@anteante Where did the image come from? Is this a stock photo? Can you get the image in RGB in the first place? Most stock image services offer multiple formats and sizes.

You might be able to use a panel hook to convert the image to RGB on upload by talking to GD directly, rather then through SimpleImage.

I agree with @texnixe ā€“ I myself can find ways to fix a corrupted image, but thatā€™s not an option for the client / editor, who thinks he ā€œbrokeā€ the CMS. Itā€™s not a big deal for those three images, I can convert them for the client; but it would be great to have a future proof fallback (e.g. an explcit error code ā€œInvalid fileā€ or ā€œunable to process fileā€) if that happens again.

Of course, I agree with @texnixe too - clients need to see whats going on rather then it just freezing up.

I think your best bet is as I suggestedā€¦ try a panel hook that converts the image to RGB on upload. This i think is the path of least resistance for clients.

I donā€™t think this will work because the hook is called after the event, by then, the Panel probably will already have crashed by trying to create the thumb.

Hmm thats annoying.

@anteante I was just googling around and found this plugin. It might fix your image on upload in the same way that ImageOptim was happy to do so. Ive got a hunch that it talks straight to GD, without simple image but iā€™m not sure. Worth a shot i guess. I think a minor tweak to it will have it save as RGB.

Well, itā€™s worth a try after all. Maybe the event actually does hit before the thumb is created in the sidebar or file view.

Hey @jimbobrjames thanks a lot for the hint towards this plugin ā€¦ this might be a good solution, will try this out later!

@anteante No problem. Let us know how it goes.

@anteante Could you please send me the faulty file for testing? I canā€™t reproduce this with a completely invalid file (in my case a PDF that I renamed to .jpg), so it looks like it needs to be a JPG that is basically valid but not completely valid.