bigbrush/yii2-tinypng

为 Yii 2 提供的 TinyPng

安装次数: 90,529

依赖项: 0

建议者: 0

安全性: 0

星标: 6

关注者: 1

分支: 2

开放问题: 0

类型:yii2-extension

dev-master 2017-03-23 10:13 UTC

This package is auto-updated.

Last update: 2024-09-25 07:06:13 UTC


README

TinyPng 提供了 TinyPng 的 Yii 2 集成。

使用它,您可以压缩 PNG 和 JPG 图片而不会损失图片质量。您还可以在压缩图片的同时调整图片大小。

安装

运行以下命令

composer require "bigbrush/yii2-tinypng:dev-master"

或者将其添加到您的 composer 文件中

"bigbrush/yii2-tinypng": "dev-master"

使用方法

图片压缩/优化

$tiny = new TinyPng(['apiKey' => 'YOUR API KEY']);

// compress image - overwrite file
$tiny->compress('path/to/file/to/compress');
// compress image - create a new image
$tiny->compress('path/to/file/to/compress', 'path/to/file/after/compression');
// get current monthly compression count 
$count = $tiny->usage();

图片调整大小

调整图片大小需要一个配置数组。

[
    'method' => 'fit',
    'width' => 150,
    'height' => 100,
]

可用方法包括:- scale - fit - cover

有关每个方法的详细信息,请参阅 TinyPng 文档

// resize image - overwrite file
$tiny->resize('path/to/file/to/resize', null, ['method' => 'fit', 'width' => 150, 'height' => 100]);
// resize image - create a new image
$tiny->resize('path/to/file/to/resize', 'path/to/file/after/resizing', ['method' => 'fit', 'width' => 150, 'height' => 100]);