Viewing images on the command line and the “No identify available” error

I’ve been testing a website that generates images on the fly and in the past had used the less command to view the file contents, this helped see when PHP errors had unfortunately made their way into an image file.

However, sometimes when viewing a file I got the following error returned:

No identify available
Install ImageMagick or GraphicsMagick to browse images

I’m pretty sure I worked this one out a few years ago, but had obviously forgotten. Turns out you can’t view binary files via a command like less!

The right way to view an image file is a command such as xxd. To view the top of a file (which usually points in the direction of the file format) use a command such as:

xxd /path/to/file.jpg | head

This command works just as well for text files, so will still pick up if PHP errors are inside the image file instead of the correct binary data.