[LINUX] I want to convert an image to WebP with lollipop

WebP was officially supported on April 27, 2020: tada: [^ webp-official] So now you don't have to do the hassle of:

[^ webp-official]: PHP7.3 now supports "WebP" format image files that are effective in speeding up the site! --Lollipop


I managed to convert JPEG and PNG images to WebP format on a lollipop rental server. It is premised on the standard plan or higher (SSH connection is possible).

Lollipop PHP does not support ʻimagewebp` function

WebP conversion? ʻImagewebp` It's easy to use the function: thumbs up: If you think ...

Fatal error: Uncaught Error: Call to undefined function imagewebp()



 Did not work. If you check with `phpinfo ()`
 ![lolipop-phpinfo-gd.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/617255/ef6cf73a-d685-a14d-af51-d727606969eb.png)
 It turns out that lollipops don't support WebP. [^ webp-support]
 [^ webp-support]: When I contacted support, I received a reply that there are no plans to support WebP at this time (March 26, 2019). ~~ I would like to expect it in the future. ~~

## Build an environment that can be converted to WebP on your own
 `cwebp`Images can be converted to WebP format by using command line tools ([A new image format for the Web | WebP | Google Developers](https://developers.google.com/speed/webp)). In other words`cwebp`The solution is to make it available on the lollipop server.

### policy
 Copy the `cwebp` command line tools and required libraries built in a lollipop-like environment to the lollipop server. Since `make` and` gcc` cannot be used on the lollipop rental server ([Available command list --Lollipop](https://lolipop.jp/manual/user/ssh/#p-command)), the rental server It is impossible to build on.

### Build build environment
 Build an environment similar to a lollipop rental server with VirtualBox. The point is to match the version ** [glibc](https://www.gnu.org/software/libc/) **. [^ glibc]
 [^ glibc]: Try downloading [Precompiled WebP utilities](https://developers.google.com/speed/webp/download) and running it on a lollipop server, `` cwebp: /lib64/libc.so. 6: version `GLIBC_2.14'not found (required by cwebp) `` and you can see that it doesn't work. [^ glibc2]
 [^ glibc2]: You might think that changing the version of glibc on the rental server ... but it's not possible because many programs depend on glibc. If you force it, all commands will be invalidated and you will get stuck (Reference: [[Solved] relocation error: /lib64/libc.so.6: symbol _dl_starting_up Solution --Qiita](/ sachiotomita / items / ef29520c55fc168191d8)).

#### Check glibc version of lollipop server
 On the rental server

```shell
/lib64/libc.so*

When you execute

GNU C Library stable release version 2.12, by Roland McGrath et al.

The version is displayed as (it was 2.12 in my environment).

Installation of CentOS 6

Build a CentOS 6 (64bit) environment with glibc version 2.12 with VirtualBox. Since the rental server is CentOS [^ server-spec], the OS also matches it.

The method of building the environment with VirtualBox is not described in detail here. To explain it quite roughly

  1. Install VirtualBox
  2. Download CentOS 6.10 (64bit) from the x86_64 link in Download --CentOS ( It is recommended to use aria2 because it can be downloaded at high speed [^ aria2-qiita]. )
  3. Install CentOS with the downloaded ISO

It is a flow. I enabled and used SSH without a GUI.

[^ server-spec]: Server specifications-Lollipop [^ aria2-qiita]: Reference: [Use explosive downloader aria2, which is several times faster than curl and wget --Qiita](/ TokyoMickey / items / cb51805a19dcee416151)

Build cwebp

Build with CentOS of VirtualBox. MostlyCompiling the Utilities | WebP | Google DevelopersIt is written in.

Installation of required packages

Without this, the input image cannot be read even if it is built.

sudo yum install libjpeg-devel libpng-devel

Download and unpack the source

Download libwebp-1.0.3.tar.gz from downloads list and extract it below

tar xvzf libwebp-1.0.3.tar.gz

Build

Considering the trouble of uploading to the rental server later, install it in your home directory (~ / bin / libwebp-1.0.3).

cd libwebp-1.0.3
./configure --prefix=$HOME/bin/libwebp-1.0.3
make
make install

Allow it to run on a lollipop server

Copy required files

Copy the following files to the home directory of the rental server.

Local (VirtualBox) Rental Server
~/bin/libwebp-1.0.3/bin/cwebp ~/usr/bin/cwebp
~/bin/libwebp-1.0.3/lib/libwebp.so.7.1.0 ~/usr/lib/libwebp.so.7.1.0
~/bin/libwebp-1.0.3/lib/libwebpdemux.so.2.0.6 ~/usr/lib/libwebpdemux.so.2.0.6

Creating a symbolic link

Create a symbolic link for your library.

cd ~/usr/lib
ln -s libwebp.so.7.1.0 libwebp.so.7
ln -s libwebpdemux.so.2.0.6 libwebpdemux.so.2

Setting environment variables

Pass the path so that you can find the cwebp and the libraries it uses.

cd ~
vi .bash_profile

Enter the insert mode with the ʻInsert` key and add the following contents.

~/.bash_profile


export PATH=~/usr/bin:$PATH
export LD_LIBRARY_PATH=~/usr/lib:$LD_LIBRARY_PATH

After editing, press the ʻEsckey to enter command mode, press: wqto save and exit. By reloading.bash_profile` with the following command, the path will be in the state. From now on, if you log in with SSH, your pass will be passed automatically.

source ~/.bash_profile

If you try running cwebp and the display looks like the one below, it's OK.

Usage:

   cwebp [options] -q quality input.png -o output.webp

where quality is between 0 (poor) to 100 (very good).
Typical value is around 80.

Try -longhelp for an exhaustive list of advanced options.

If you get an error that the library cannot be found, you can copy the library to the rental server by referring to ~ / bin / libwebp-1.0.3 / lib / in the local environment.

Use from PHP

Please note that you need to set environment variables as well as ~ / .bash_profile when using from PHP. Specify the environment variable with the absolute path from the root (path like / home / users / ..., you can check with the pwd command) instead of~ / bin / ....

Recommended Posts

I want to convert an image to WebP with lollipop
I want to be an OREMO with setParam!
Convert a string to an image
I want to do ○○ with Pandas
Convert PDF to image with ImageMagick
I want to debug with Python
I want to convert an ISO-8601 character string to Japan time
I want to detect objects with OpenCV
I want to blog with Jupyter Notebook
I want to pip install with PythonAnywhere
I want to analyze logs with Python
I want to play with aws with python
I want to use an external library with IBM Cloud Functions
I want to make an automation program!
For the time being, I want to convert files with ffmpeg !!
Try to generate an image with aliasing
Sample to convert image to Wavelet with Python
I tried to make an image similarity function with Python + OpenCV
I want to display an image on Jupyter Notebook using OpenCV (mac)
I want to create an Ubuntu chrome User Profile with Colab only
I want to use MATLAB feval with python
Convert PDF to image (JPEG / PNG) with Python
I want to analyze songs with Spotify API 2
I tried to get an image by scraping
I want to mock datetime.datetime.now () even with pytest!
I want to display multiple images with matplotlib.
I want to knock 100 data sciences with Colaboratory
I want to make a game with Python
How to crop an image with Python + OpenCV
I want to analyze songs with Spotify API 1
I want to use Temporary Directory with Python2
I tried to detect an object with M2Det!
I don't want to use -inf with np.log
#Unresolved I want to compile gobject-introspection with Python3
I want to use ip vrf with SONiC
I want to solve APG4b with Python (Chapter 2)
I want to start over with Django's Migrate
I want to write to a file with Python
Post an article with an image to WordPress with Python
Convert 202003 to 2020-03 with pandas
I want to send Gmail with Python, but I can't because of an error
I want to write an element to a file with numpy and check it.
I tried to extract a line art from an image with Deep Learning
I want to detect unauthorized login to facebook with Jubatus (1)
100 image processing knocks !! (021-030) I want to take a break ...
I want to transition with a button in flask
I want to handle optimization with python and cplex
I want to climb a mountain with reinforcement learning
I want to develop an Android application on Android (debugging)
Convert the image in .zip to PDF with Python
I tried to implement an artificial perceptron with python
I want to inherit to the back with python dataclass
I want to work with a robot in python.
I want to split a character string with hiragana
I want to AWS Lambda with Python on Mac!
I want to manually create a legend with matplotlib
[TensorFlow] I want to process windows with Ragged Tensor
[ML Ops] I want to do multi-project with Python
I tried to make an OCR application with PySimpleGUI
I want to run a quantum computer with Python
I tried to find an alternating series with tensorflow