bigbrush / yii2-tinypng
为 Yii 2 提供的 TinyPng
dev-master
2017-03-23 10:13 UTC
Requires
- tinify/tinify: ^1.2
- yiisoft/yii2: *
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]);