PHP Docker error: 'Call to undefined function imagecreatefromjpeg()'
12, December 2021

Few days ago I was trying to adopt docker containers into a project developement flow. Since that project uses GD extension for manipulate images in PHP, I enabled GD in PHP container. But when I ran Psalm, a static analyzer, it reported with a error that says:

Call to undefined function imagecreatefromjpeg()


I was kinda baffled, surely I installed gd extension and enabled it in my Dockerfile, then why php can't load this function?


After a quick google search, I found the issue. I didn't configure GD extension with libjpeg support.


Oh, rookie mistake, I thought. I then installed libjpeg-dev and configured GD with it before installing the extension:

apt install libjpeg-dev \
  && docker-php-ext-configure gd --with-jpeg-dir \
  && docker-php-ext-install gd


But programming God was not happy with me yet. GD wasn't still building with libjpeg.


Frustrated I am, did another google seach, and this time found that GD now has different flag to use to build with libjpeg. This change has been done on PHP 7.4


From PHP 7.4, we have to use --with-jpeg instead of --with-jpeg-dir


So my dockerfile now contains:

apt install libjpeg-dev \
  && docker-php-ext-configure gd --with-jpeg \
  && docker-php-ext-install gd


Now my container builds just fine, imagecreatefromjpeg() works too.


Also, --with-png-dir is not needed any more, libpng is required now.


See more: https://www.php.net/manual/en/migration74.other-changes.php


Write comment about this article: