skip to Main Content

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


  1. Chosen as BEST ANSWER

    Just for anyone who runs into the same issue. Based on Mark Setchell's answer I created the following shell script:

    #!/bin/bash
    SAVEIFS=$IFS
    IFS=$(echo -en "nb")
    
    if [[ $# -lt 1 ]] || [[ $# -gt 2 ]];
    then
      echo "Usage $0 <directory> [<backupdir>]"
      exit
    fi
    
    for FILENAME in $(find $1 -regex ".*.(jpg|gif|png|jpeg)");
    do
      MIME=$(file --mime-type "$FILENAME" | awk '{print $NF}')
      if [ "$MIME" == "application/octet-stream" ]
      then
        echo "$FILENAME has mime type $MIME:"
        if [[ ! -z $2 ]];
        then
          echo "  - Creating backup"
          cp "$FILENAME" "$2"
        fi
        echo "  - Running convert"
        convert "$FILENAME" "$FILENAME"
        if [[ $? -eq 0 ]];
        then
          echo "  - Done!"
        else
          echo "  - Error!"
          echo "$FILENAME" >> errors.txt
        fi
      fi
    done
    IFS=$SAVEIFS
    

    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.


  2. So, basically you are saying that

    file --mime-type <filename>
    

    doesn’t work? If so, try asking ImageMagick to work it out for you:

    identify -ping -verbose someImage.jpg | awk '/Mime type:/{print $NF}'
    

    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:

    convert troubleFile.jpg rebuilt.jpg
    

    and see if file --mime-type rebuilt.jpg works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search