cinghie / yii2-tinify
为 TinyPng 的 Yii2 集成
1.0.2
2023-04-10 10:25 UTC
Requires
- tinify/tinify: ^1.6.1
- yiisoft/yii2: ^2.0.14
This package is auto-updated.
Last update: 2024-09-10 13:16:38 UTC
README
为 TinyPng 的 Yii2 集成: https://tinypng.com/
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
php composer.phar require cinghie/yii2-tinify "^1.0.2"
或者将此行添加到您的 composer.json
文件的 require 部分。
"cinghie/yii2-tinify": "^1.0.2"
用法
初始化组件: https://tinypng.com/developers/reference/php#authentication
$tinify = new Tinify(['apiKey' => 'YOUR API KEY']);
从路径压缩图像: https://tinypng.com/developers/reference/php#compressing-images)
// create a new image
$tinify->compress('path/to/file/to/compress','path/to/file/after/compression');
// overwrite file
$tinify->compress('path/to/file/to/compress');
从缓冲区压缩图像: https://tinypng.com/developers/reference/php#compressing-images)
$tinify->compressFromBuffer('path/to/file/to/compress');
从 URL 压缩图像: https://tinypng.com/developers/reference/php#compressing-images)
$tinify->compressFromUrl('https://tinypng.com/images/panda-happy.png','path/to/file/after/compression');
调整图像大小: https://tinypng.com/developers/reference/php#resizing-images
// create a new image
$tinify->resize('path/to/file/to/compress','path/to/file/after/compression',['method' => 'fit', 'width' => 150, 'height' => 100]);
// overwrite file
$tinify->resize('path/to/file/to/compress',null,['method' => 'fit', 'width' => 150, 'height' => 100]);
// Available methods
scale,fit,cover,thumb
将图像存储到 Amazon S3: https://tinypng.com/developers/reference/php#saving-to-amazon-s3
$tinify = new Tinify([
'apiKey' => 'YOUR API KEY',
'aws_access_key_id' => 'YOUR_AWS_ID_KEY',
'aws_secret_access_key' => 'YOUR_AWS_ACCESS_KEY',
'headers' => array('Cache-Control' => 'max-age=31536000, public'),
'path' => 'example-bucket/my-images/optimized.jpg',
'region' => 'us-west-1'
]);
$tinify->storeToAmazonS3('path/to/file/to/compress');
本月使用的压缩次数: https://tinypng.com/developers/reference/php#compression-count
$count = $tinify->usage();