I was having an issue with only certain random/jpeg files with the functions imagecreatefromjpeg imagecreatetruecolor and other related ones.
This issue was annoying basically it seems like a libgd issue/bug with newer versions of PHP and it was difficult to trace-out. One very useful thing that helped me was using "php-cgi" and passing the query string as an argument eg:
php-cgi images.php source=IMG.jpg
(that way you get all the error messages which lead me to below):
PHP Warning: imagecreatefromjpeg(): gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file
The solution is to tweak the PHP GD settings to ignore this warning
/etc/php.ini
[gd]
; Tell the jpeg decode to ignore warnings and try to create
; a gd image. The warning will then be displayed as notices
; disabled by default
; http://www.php.net/manual/en/image.configuration.php#ini.image.jpeg-ignore-warning
gd.jpeg_ignore_warning = 1
php, imagecreatefromjpeg, gd, jpeg, libjpeg, recoverable, premature, filei, functions, imagecreatetruecolor, libgd, newer, versions, quot, cgi, query, eg, images, img, jpg, tweak, settings, etc, ini, decode, warnings, displayed, notices, disabled, default, http, www, manual, en, configuration, jpeg_ignore_warning,