bingher / zipimg
压缩图片
v1.0.0
2020-11-03 02:10 UTC
Requires
- php: >7.1
- bingher/transmit: dev-main
This package is auto-updated.
Last update: 2024-09-10 20:58:37 UTC
README
安装
composer require bingher/zipimg
使用
$input = 'path-of-image';
$zipimg = new \bingher\zipimg\Image();
$zipimg->zip($input);
# 助手函数
zipimg($input);
注意事项
-
php.ini
需要启用shell_exec
函数 -
Windows环境:已经内置了压缩用的exe程序(32位),理论上可以直接使用
-
Linux环境:需要编译安装
pngguant
、guetzli
、mozjpeg
# centos # libpng >= 1.6 yum install libpng-devel git clone --recursive https://github.com/kornelski/pngquant.git cd pngquant ./configure --prefix=/usr/local/pngquant make && make install ln -s /usr/local/pngquant/bin/pngquant /usr/local/bin/pngquant
#centos yum install libpng-devel yum install gcc gcc-c++ mkdir guetzli && cd guetzli wget https://github.com/google/guetzli/archive/v1.0.1.tar.gz tar -zxvf v1.0.1.tar.gz cd guetzli-1.0.1 make && make install ln -s 安装目录/bin/Release/guetzli /usr/local/bin/guetzli
# centos 7 yum -y install build-essential nasm # centos 8 安装nasm # 到https://www.nasm.us/pub/nasm/releasebuilds下载相关rpm包 wget https://www.nasm.us/pub/nasm/releasebuilds/2.15/linux/nasm-2.15-0.fc31.x86_64.rpm yum localinstall nasm-2.15-0.fc31.x86_64.rpm cd ~ wget -O "mozjpeg-3.3.1.tar.gz" https://github.com/mozilla/mozjpeg/archive/v3.3.1.tar.gz tar -zxvf mozjpeg-v3.3.1.tar.gz cd mozjpeg-3.3.1 autoreconf -fiv ./configure --prefix=/usr/local/mozjpeg make && make install ln -s /usr/local/mozjpeg/bin/cjpeg /usr/local/bin/cjpeg
Python服务
1. PHP客户端远程调用Python服务端进行图片压缩
由于生产环境中PHP一般会禁用执行命令行等危险函数,可以考虑开启Python服务端,然后PHP进行调用实现图片压缩
- 进入
vendor/bingher/zipimg/python
目录 - 安装Python库
pip install -r requirement.txt
- 启动服务
python zipimg_server.py
- 远程调用
<?php
require_once '../vendor/autoload.php';
use bingher\transmit\Client;
$client = new Client('127.0.0.1', 8000);
$params = [
'in_img' => __DIR__ . '/input/1.jpg',
'out_img' => __DIR__ . '/output/1.test.jpg',
];
$result = $client->zip($params);
var_dump($result);
# 助手函数
$input = __DIR__ . '/input/1.jpg';
$result = remote_zipimg($input);
var_dump($result);
2. Python自动监控压缩图片
python ./auto_zipimg.py -w ../test/output -a ../lib/ -p