imagecreatefromjpeg() fails or is undefined in PHP
Fatal error: Uncaught Error: Call to undefined function imagecreatefromjpeg()Short answer
imagecreatefromjpeg() belongs to the GD extension. If PHP says the function is undefined, GD is not loaded by the php.ini your web server uses, or GD was built without JPEG support. If the function exists but returns false, the file could not be read as a JPEG. Check gd_info() first.
Fix it fast
1Find out which cause you have
Ask the same PHP that serves your page whether GD is loaded and whether it reports JPEG support. Put this in a temporary file on the site, open it in the browser, then delete it. Running it only in the terminal can show a different PHP than the web server uses.
<?php var_dump(extension_loaded('gd')); var_dump(function_exists('imagecreatefromjpeg')); if (function_exists('gd_info')) { var_dump(gd_info()['JPEG Support']); }2Match what you see, then jump to its fix
- 1
extension_loaded('gd') prints falseThe GD extension is not loaded in the php.ini the web server readsFix - 2
extension_loaded('gd') is true, yet gd_info() reports JPEG Support as falseGD is loaded but was built without JPEG supportFix - 3
No fatal error, but the result of imagecreatefromjpeg() is falseThe function exists but returns falseFix
- 1
What does the error look like?
Fatal error: Uncaught Error: Call to undefined function imagecreatefromjpeg()
Cause 1 of 3
The GD extension is not loaded in the php.ini the web server reads
1Check
The php.ini that the web server loads is the one that counts, and it can differ from the command-line one. phpinfo() shows its path under Loaded Configuration File.
<?php phpinfo();2Fix
Enable GD in that php.ini. On Windows PHP 7.2 or newer, uncomment the line by removing the semicolon and use the extension name. On a build from source, configure PHP with GD enabled. Then restart the web server.
; php.ini on Windows
extension=gd
# building PHP from source
./configure --enable-gd3Verify
Reload the test page. extension_loaded('gd') should print true, and phpinfo() should now have a gd section.
var_dump(extension_loaded('gd'));Confirmed by php.net
Cause 2 of 3
GD is loaded but was built without JPEG support
1Check
gd_info() returns the capabilities of the installed GD library, including a JPEG Support flag.
<?php
print_r(gd_info());2Fix
Rebuild or replace PHP with JPEG support. GD needs libpng and libjpeg to compile, and since PHP 7.4 the configure option for JPEG is --with-jpeg.
./configure --enable-gd --with-jpeg3Verify
gd_info() now shows JPEG Support as true, and function_exists('imagecreatefromjpeg') is true.
var_dump(gd_info()['JPEG Support']);Confirmed by php.net
Cause 3 of 3
The function exists but returns false
1Check
The function returns an image object on success and false on errors, for example when the path is wrong or the file is not a JPEG that GD can decode.
<?php
$path = __DIR__ . '/photo.jpg';
var_dump(is_readable($path));
var_dump(getimagesize($path));2Fix
Test the return value before you use it, and fix the path or the file.
<?php
$im = imagecreatefromjpeg($path);
if ($im === false) {
exit('Could not read the image');
}3Verify
The check passes and later image functions get a valid image object.
var_dump($im);Confirmed by php.net
How do I stop it coming back?
- Check function_exists('imagecreatefromjpeg') in your deploy or health check, not only on your laptop.
- Compare phpinfo() on the server and the version you develop with, because extension lists differ.
- Always test the return value of an image-loading function before you pass it on.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix