Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

imagecreatefromjpeg() fails or is undefined in PHP

ErrorFatal 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.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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']); }
  2. 2Match what you see, then jump to its fix

    1. 1extension_loaded('gd') prints falseThe GD extension is not loaded in the php.ini the web server readsFix
    2. 2extension_loaded('gd') is true, yet gd_info() reports JPEG Support as falseGD is loaded but was built without JPEG supportFix
    3. 3No fatal error, but the result of imagecreatefromjpeg() is falseThe function exists but returns falseFix

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-gd

3Verify

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

Did this fix it?

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-jpeg

3Verify

gd_info() now shows JPEG Support as true, and function_exists('imagecreatefromjpeg') is true.

var_dump(gd_info()['JPEG Support']);

Confirmed by php.net

Did this fix it?

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

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.