Tag Archives: image type

PHP: “JPEG library reports unrecoverable error: Not a JPEG file: starts with 0x89 0x50” Error Solved Tips, Tricks and Tutorials 30 OCT 2017

My cross-platform Kinetica TouchandTell app running on an old iPad 3 was not successfully uploading images to our new RedStation hosted Ubuntu server (the Android builds were doing it just fine), and I wasn’t really sure why. The Appcelerator Titanium code seemed all okay and in fact it wasn’t until I checked the server’s PHP error logs that I finally spotted the reason why. Exhibit A:

“JPEG library reports unrecoverable error: Not a JPEG file: starts with 0x89 0x50”

Some quick Googling led me to understand that the PHP GD library was failing when trying to parse the received JPG file from the iPad, primarily because the supplied photo was not in the accepted JPEG format. (In fact, chances are that the encoding is in fact either that of a PNG file or perhaps the brand new HEIC image format that has just rolled out with iOS 11).

Although the documentation states that the old imagecreatefromjpeg function is meant to return FALSE if it fails to open the supplied file, there is (annoyingly) a listed bug tracking the fact that the function is in practice actually triggering a Fatal Error instead!

So instead of adopting a practice of trying to open an image based on its file extension, a better solution to get around this problem would be to make use of the similar imagecreatefromstring function, which returns an image resource representing the photo obtained from the given file.

In practice:

$image = false;
$image_data = file_get_contents($imagefilepath);
try {
      $image = imagecreatefromstring($image_data);
} catch (Exception $ex) {
      $image = false;
if ($image !== false){
//process $image resource

(These image types will be automatically detected if your build of PHP supports them: JPEG, PNG, GIF, BMP, WBMP, and GD2.)

Related Link: imagecreatefromstring

PHP: Checking a Photo’s Image Type before trying to Extract Useable EXIF Data CodeUnit 04 MAY 2011

With EXIF data embedded by almost all digital cameras and mobile phone cameras nowadays, it makes a lot of sense to use this wealth of information in your project when displaying images and their associated information. With PHP’s handy exif_read_data function, grabbing this information becomes a snap – though there is one small caveat: when applied to an image type that doesn’t support EXIF embedding, the function generates a PHP Warning!

Because only really JPG, TIF and RAW files support EXIF, you need to first do an image type check before attempting to extract the embedded EXIF data, which thankfully is pretty simple due to PHP’s exif_imagetype function.

What exif_imagetype does is simply read the first couple of bytes of an image, checks its signature and attempts to report back what it thinks the image is. It supports a number of different image formats, each assigned to a specific IMAGETYPE constant to make it easier to validate image types.

For our purposes, assuming we only allow JPEG photo uploads, our checking code would look like this:

//check that file type is a JPG (IMAGETYPE_JPEG) before exif_read
if (exif_imagetype($filepath) == 2) {
    $exif = exif_read_data($filepath, 0, true);
} else {
    echo 'Photo file type does not support embedded EXIF data';


Related Link: http://www.php.net/manual/en/function.exif-imagetype.php