WebP Enabling for PHP GD Library

blury terminal

To have the Drupal module for WebP working with PHP GD enabled. I pulled my hair out trying to find support online for how to enable this easily on our Jelastic platform. Turns out that there’s no easy way. You need to rebuild GD library in PHP with the right support for your environment. PHP will bundle the GD library with environment specific paths, or at least that’s my assumption after what I saw inspecting the binary .so file, I could be wrong.

This GD building for PHP is tested on a CentOs 7 server.

Install dependencies:

  1. ### AS ROOT ###
  2. yum install libwebp-devel libwebp autoconf automake gcc libjpeg-devel libpng-devel libXpm-devel freetype-devel

Run PHP build stuff, you need to of course tweak the following to suit your php version:

  1. ### AS NON ROOT ###
  2.  
  3. wget https://www.php.net/distributions/php-7.2.28.tar.bz2
  4. tar xf php-7.2.28.tar.bz2
  5. cd php-7.2.28
  6. cd ext/gd/
  7. phpize
  8.  
  9. ## Run the actual configure
  10.  
  11. ./configure --prefix=/usr --enable-shared --with-webp-dir=/usr --with-jpeg-dir=/usr --with-png-dir=/usr --with-zlib-dir=/usr --with-freetype-dir=/usr --with-xpm-dir=/usr
  12. make
  13.  
  14. ## Copy new gd.so file in place, backup old one
  15. cp /usr/lib64/php/modules/gd.so ~/gd.so.bak
  16. cp /var/www/php-7.2.28/ext/gd/modules/gd.so /usr/lib64/php/modules/gd.so
  17.  
  18. # Right after, otherwise server goes down.
  19. sudo service httpd restart
  20.  
  21. # Check if WebP support is enabled:
  22. php -r "print_r( gd_info() );"

Clean up, delete dependencies:

yum remove gcc libpng-devel libjpeg-devel libXpm-devel g++ xorg-x11-proto-devel.noarch glibc-devel

Hope this helps.

Br, Jorge