I have a lot of images that I need to process using a third party PHP tool. All these images are GIFs, JPEGs or PNGs. However, on some of the images I get an error because of an incorrect mime type. Turns out that some images (mostly JPG files) are detected as “application/octet-stream”.
Whenever I open the image an any viewer, all seems ok. After resaving such an image using Photoshop, the mime type is detected correctly as “image/jpeg”. I’m just not exited about opening and resaving thousands of images.
I’ve constructed a list of filenames for which the problem occurs. Is there any way to ‘fix’ the image such that the correct mime type is detected?
I’ve found the incorrect mime types using the following command:
file --mime-type <filename>
Here are some example images:
Running identify -ping -verbose <filename>
doesn’t return a mime type only a Format. The format is Format: JPEG (Joint Photographic Experts Group JFIF format)
for all JPG images with or without correct mime.
Some more info about versions:
$ file --version
file-5.14
magic file from /etc/magic:/usr/share/misc/magic
$ identify --version
Version: ImageMagick 6.7.7-10 2014-03-06 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP
2
Answers
Just for anyone who runs into the same issue. Based on Mark Setchell's answer I created the following shell script:
It will search a directory recursively for jpg/png/gif files and checks the mime type. If the mime type is
"application/octet-stream"
it will try to convert the image without actually performing any visible change in the image. After this operation the correct mime type will be detected.Images that could not be converted will get logged in
errors.txt
. You can also provide a backup directory in which the original files will be saved.So, basically you are saying that
doesn’t work? If so, try asking ImageMagick to work it out for you:
Or post a couple of troublesome images so that other folks can test it out.
Maybe you can use ImageMagick to rewrite the file and then
file --mime-type
will work. Try running this:and see if
file --mime-type rebuilt.jpg
works.