Update: I just found there was a Nginx/Apache module PageSpeed that support WebP, you may try it in my site here, and here you may notice that some pictures were loaded from a .webp
source. The module for Nginx called ngx_pagespeed
, could load pictures conditionally in .webp
or normal .png
/.jpg
/.gif
formates. But seems this module has a high CPU and Memory occupation, you got to trade-off between speed and performance.
WebP is a Google developed modern image format that provides superior lossless and lossy compression for images on the web. Compired to shrunk images that processed by tools like TinyPNG, WebP images cloud be more tiny (how tiny it is? See here). But currently, the biggest problem is few browsers support WebP.
So if you are going to work with WebP images, you'd better consider compatibility with (mainly) none-chromium based browsers.
My idea is detecting the browser supported content types by accept
in HTTP Resuest Headers:
So seems this brower support WebP
Now you may detect whether user support WebP with PHP, and response with different content types:
if(strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')) {
// Something to do with WebP supported
header('Content-Type: image/webp');
} else {
// When WebP is not supported
header('Content-Type: image/PNG');
}
Another problem, how to convert normal png/jpg/jpeg/gif
images to WebP? Seems WebP is not supported well in PHP, so I convert images to WebP type with a Python script:
(pillow lib is necessary: pip3 install pillow
)
#coding=utf-8
from PIL import Image
import os
from os import listdir
from os.path import isfile, join
# Do not forget the '/'
mypath = 'png/' # Your png/jpg/jpeg/gif folder path
webp_path = 'webp/' # WebP output path
def convert_to_webp(filename):
im = Image.open(mypath + filename).convert("RGB")
im.save(webp_path + filename + ".webp", "WEBP")
# Get a list of all file names
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for f in onlyfiles:
convert_to_webp(f)
If you hope to do it automatically, you may consider call Python script with PHP like this:
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>
You may see my WebP API demo here (Chrmoe is necessary): https://api.shino.cc/webp
「樱花庄的白猫」原创文章:《WebP Support with PHP and Python》,转载请保留出处!https://2heng.xin/2018/03/21/webp-support-with-php-and-python/
Q.E.D.
Comments | 18 条评论
博主 Chukaland
我似乎发错了?囧
博主 Chukaland
博主 Spirit
顶!
博主 铃铛
第一次看到樱花庄背景的博主,这个博客好可爱
博主 Hydral
博主 岳尧
老哥我把你的封面抠不下来23333能不能发一份超清的,谢谢啦
博主 Mashiro
@岳尧
博主 岳尧
@Mashiro 拜谢2333
博主 岳尧
@Mashiro 十分感谢加拜谢2333
博主 古河家的面包
老哥,这网站真tm漂亮!!!!还有,你是it的白猫小编吗
博主 Leslie Freda
My name is Leslie Freda and I’m the director of corporate benefits for a large wholesale travel provider. After reveiwing your website I thought you would benefit from our free employee travel benefits program. It’s free for everyone in your organization and can save you up to 70% off hotel bookings. Click on the link above, and do a few searches too see what you guys can save! I think you’ll love it! Sincerely, Leslie Freda
博主 蝉時雨
My English is very poor, so…..
博主 Mashiro
@蝉時雨 Mine is pool, too…
博主 拾柒
@Mashiro And me too…
博主 Huangxin
Why Mashiro Dalao Started use English to write his awesome blog?
博主 Mashiro
@Huangxin Why Master Panic started comment in English?
博主 Phower
啊哈,前两天正准备搞个TinyPNG的API你就弄了个这个,太棒了,找时间来试试
话说,现在突然发现我的许多想法跟你惊人的相似啊
博主 Mashiro
@Phower TinyPNG有插件加持的