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