How to Enable WebP support Php 7 without Recompiling?

Today, I came across a problem when I have shifted my websites to a new server having PHP 7 installed. One of my websites uses PHP imagewebp() function to generate WebP images. I was using centos 7.x server with Nginx installed and PHP version was 7.0. When I was trying to generate WebP images, I was getting error fatal error: Uncaught Error: Call to undefined function imagewebp(). I searched google and tried various tutorials in order to solve this issue, but all in vain. After trying several hours, I found the alternate solution to generate WebP images without recompiling PHP and without installing any package. Also, this method doesn’t require root access to the server. So, read this tutorial to know, how to enable WebP support in GD library for PHP 7 in CentOS Nginx.

What is WebP & Why its Important?

WebP format of the image was developed by Google. The reason behind it was to make small size images with lossless compression in order to make web pages smaller and serve them fast. WebP format is the successor to jpeg format.

According to Google: WebP is a modern image format which provides superior lossless and lossy compression for images. Using WebP, web developers can create smaller, richer images that make the web faster.

google page speed webp

Now it’s clear that Google is forcing web developers to serve WebP images in order to make web pages much faster. WebP images are 26% smaller in size compared to png, 25-34% smaller than jpeg images. In short, WebP images are approximately 3 times smaller than other formats.

Does All Browser Support WebP Format Images?

No, all browser do not support WebP format images. WebP was created google and it is supported by google official Chrome web browser and other browsers like Microsoft Edge, Firefox & Opera. However, it is not supported by some of the major browsers like Safari.
But there’s a method to serve images according to browser compatibility. For this, we need to write a bit of HTML code. Here’s the HTML code below:

<source srcset="" type="image/webp">
<img src="" alt="Demo Image" />

Using the above code, we can serve the supported image format to browser depending on the compatibility of WebP format.

How to enable WebP support in PHP 7.x or Any Version?

As I wrote above, I tried to enable WebP support using ssh (Command Line), but all in vain. The only solution I found was to recompile the PHP with –with-webp-dir=DIR to enable WebP support. And it was not possible for me as I was using MongoDB and it requires to recompile PHP again and may cause some issues with database (Lemme clear! I am using two databases (MongoDB & MariaDB) and it may cause some conflicts). It took me several hours to find the perfect solution which works with any PHP above version 5.4 and does not require any installation. Moreover, this method is provided by Google, so no need to worry about site/server security. This method supports PHP only installed on any Linux distros like CentOS, Ubuntu, Debian or Fedora etc. So follow the steps below:

  • Go to the official WebP library website.
  • Download the package depending on your OS (Windows, Mac or Linux).
  • Here we are talking about Linux, so download the latest package (For e.g. libwebp-1.1.0-rc2-linux-x86-64.tar.gz).
  • The downloaded file format is tar.gz. You can use online converters like Cloudconvert to convert this format into zip format.
  • Once converted and downloaded, extract the zip file and rename it to WebP folder.
  • Upload the folder using FTP (home/domain) or Cpanel (public_html), depending on if you are using Nginx, Cpanel or whatever.
  • Once uploaded, you are ready to convert your images to WebP format.
  • To convert any image into WebP format use the PHP command below:
echo shell_exec("webp/bin/cwebp -q 60 demo.jpg -o img/demo.webp");

If still any issue, please write in the comment box below. Hope this tutorial is helpful in resolving your issue.

Leave a Comment