forgetphp/image-compression

一个图像压缩库

v1.0.0 2021-01-04 06:33 UTC

This package is auto-updated.

Last update: 2024-09-04 14:14:04 UTC


README

PHP JPEG Encoder 项目

该项目基于 libwebpmozjpeg 实现。PHP的 gd 库或 imagick 的压缩比和性能都不如前者。如果需要转换成异步模式,则需要使用 Swoole 提供的 Hook 功能将命令转为 CLI 模式。

运行环境

  • Linux, OS X
  • PHP 7.2+
  • Swoole 4.4+

安装

compser require forgetphp/image-compression

使用

<?php
use Image\Compression\Type;
use Image\Contract\FinishInterface;
use Image\Contract\ReportEncodingProgressInterface;
use Image\Event\Finish;
use Image\Event\ReportEncodingProgress;
use Image\ImageFactory;
use Image\Listener\FinishListener;
use Image\Listener\ReportEncodingProgressListener;

//监听压缩进度事件(如果需要)
class ReportListener extends ReportEncodingProgressListener
{

    /**
     * @param ReportEncodingProgress| object $event
     */
    public function process( object $event )
    {
        //压缩进度
        //$event->progress; 
        //当前文件名
        //$event->currentFile;
       
    }
}

//监听压缩完成事件(如果需要)
class CompressionFinishListener extends FinishListener
{
    /**
     * @param Finish| object $event
     */
    public function process( object $event )
    {
        $event->currentFile;//当前文件名
        $event->compressionRatio;//压缩比
        $event->distImageSize;//压缩后文件大小
        $event->distPath;//压缩后文件路径
        $event->originImageSize;//原图大小
    }
}

$config = [
    'type'       => Type::CJPEG , //压缩类型
    'output_dir' => './runtime', //文件输出路径
    //'bin' => '',//对应bin的路径。默认不用填写。如果在本地无法正常运行。请编译对应平台的可执行文件后配置该项

    'listener'   => [
        //ReportEncodingProgressInterface::class => ReportListener::class , //压缩进度事件
        //FinishInterface::class                 => CompressionFinishListener::class ,//压缩完成事件
    ] ,
];

$image = ImageFactory::create($config);
方法

setQuality(int) 设置压缩质量。0-95 默认75

$image->setQuality(75);

enableAsync() 是否开启异步。依赖 Swoole 提供的 Hook 功能

$image->enableAsync();

input(string $image,string $outfile = '') 输入一张图片。第二个参数为输出图片名称,不需要带后缀,不传则使用原名。也可以输入网络图片地址。

$image->input('demo.jpg');

inputs(array $images) 输入图片数组。参数说明参考 input()

$image->inputs([
   'https://wenda.swoole.com/storage/avatar/avatar-1.png',
   'WechatIMG409.jpeg'
]);

//or

$image->inputs([
   [ 'WechatIMG409.jpeg' , 'outfilename'],
   ['https://wenda.swoole.com/storage/avatar/avatar-1.png','输出文件名。不需要带后缀。可不传同 input()' ],
]);

crop( int $x , int $y , int $width , int $height ) 图片裁剪。 x 轴位置 ,y 轴位置 , width 图片宽度 ,height 图片高度

$image->->crop(100,100,100,100);

resize( int $width , int $height ) 图片缩放( mozjpeg 不支持), width 图片宽度 ,height 图片高度

$image->->resize(10,10);

output() 输出图片

$result = $image->->output();

例子

$result = ImageFactory::create($config)
    ->input( 'demo.jpg' )
    ->output( );

效果对比

此对比只是针对同一图片而言。不同的图片压缩比会有差异。只能做一个相对的对比。

兼容

webp兼容一览表

许可证

MIT