Home / Fix the “call to undefined function ImageCreateFromPNG” error in PHP on Debian

Fix the “call to undefined function ImageCreateFromPNG” error in PHP on Debian

If you call the ImageCreateFromPNG function (and similar image processing functions) in PHP and do not have the GD module installed you will get the error message "Fatal error: Call to undefined function ImageCreateFromPNG()" error message. This post shows how to install the PHP GD module on Debian Linux and the instructions should also work on all Debian derived distros such as the Ubuntu family.

The instructions show here are from the command line and do not use a GUI tool.

Open up a command prompt and run the following either as the root user or using the sudo utility:

aptitude install php5-gd

You’ll see something like this:

Reading package lists... Done
Building dependency tree
Reading state information... Done
Reading extended state information
Initializing package states... Done
Reading task descriptions... Done
The following NEW packages will be installed:
  libgd2-xpm{a} libt1-5{a} php5-gd
The following packages will be REMOVED:
  libgd2-noxpm{a}
0 packages upgraded, 3 newly installed, 1 to remove and 0 not upgraded.
Need to get 409kB of archives. After unpacking 524kB will be used.
Do you want to continue? [Y/n/?]

Type in "y" and then <enter> and the package will install:

Writing extended state information... Done
Get:1 http://ftp.nz.debian.org lenny/main libgd2-xpm 2.0.36~rc1~dfsg-3 [223kB]
Get:2 http://ftp.nz.debian.org lenny/main libt1-5 5.1.2-3 [153kB]
Get:3 http://ftp.nz.debian.org lenny/main php5-gd 5.2.6.dfsg.1-1+lenny2 [32.4kB]
Fetched 409kB in 1s (299kB/s)
dpkg: libgd2-noxpm: dependency problems, but removing anyway as you request:
 libgraphviz4 depends on libgd2-noxpm (>= 2.0.36~rc1~dfsg) | libgd2-xpm (>= 2.0.36~rc1~dfsg); however:
  Package libgd2-noxpm is to be removed.
  Package libgd2-xpm is not installed.
(Reading database ... 48706 files and directories currently installed.)
Removing libgd2-noxpm ...
Selecting previously deselected package libgd2-xpm.
(Reading database ... 48695 files and directories currently installed.)
Unpacking libgd2-xpm (from .../libgd2-xpm_2.0.36~rc1~dfsg-3_i386.deb) ...
Selecting previously deselected package libt1-5.
Unpacking libt1-5 (from .../libt1-5_5.1.2-3_i386.deb) ...
Selecting previously deselected package php5-gd.
Unpacking php5-gd (from .../php5-gd_5.2.6.dfsg.1-1+lenny2_i386.deb) ...
Setting up libgd2-xpm (2.0.36~rc1~dfsg-3) ...
Setting up libt1-5 (5.1.2-3) ...
Setting up php5-gd (5.2.6.dfsg.1-1+lenny2) ...
Reading package lists... Done
Building dependency tree
Reading state information... Done
Reading extended state information
Initializing package states... Done
Writing extended state information... Done
Reading task descriptions... Done

Now restart Apache doing a graceful reload or a full restart again either as the root user or using sudo. You may need to wait a few seconds using graceful for the extension to be loaded in all threads but it become available without a full restart.

apache2ctl graceful
OR
apache2ctl restart

That’s all there is to it. The GD functions should now be available in PHP.