Prevent malformed images from crashing server

Check the err and size variables before proceeding.

Should make it best practice to return to the error message
as well in the error object; starting that with this commit.