MichaelThuren / tinify-laravel-client
支持Laravel的Tinify API(TinyPNG)
9.0.0
2022-03-02 10:06 UTC
Requires
- php: ^8.0
- illuminate/support: ^9.0
- tinify/tinify: ^1.5
This package is auto-updated.
Last update: 2024-09-29 05:29:49 UTC
README
支持的Laravel版本 9+
安装
$ composer require michaelthuren/tinify-laravel-client
将以下内容添加到您的config/app.php文件中,
在"providers"下
michaelthuren\tinify-laravel-client\TinifyLaravelServiceProvider::class,
在"aliases"下
'Tinify' => michaelthuren\tinify-laravel-client\Facades\Tinify::class
并设置环境变量TINIFY_APIKEY
,使用您的TinyPNG API密钥。
如果您想直接将图片上传到aws s3
,您需要设置以下环境变量,使用您的aws s3凭证。
S3_KEY= S3_SECRET= S3_REGION= S3_BUCKET=
示例
$result = Tinify::fromFile('\path\to\file'); $result = Tinify::fromBuffer($source_data); $result = Tinify::fromUrl($image_url); /** To save as File **/ $result->toFile('\path\to\save'); /** To get image as data **/ $data = $result->toBuffer();
$s3_result = Tinify::fileToS3('\path\to\file', $s3_bucket_name, '/path/to/save/in/bucket'); $s3_result = Tinify::bufferToS3($source_data, $s3_bucket_name, '/path/to/save/in/bucket'); $s3_result = Tinify::urlToS3($image_url, $s3_bucket_name, '/path/to/save/in/bucket'); /** To get the url of saved image **/ $s3_image_url = $s3_result->location(); $s3_image_width = $s3_result->width(); $s3_image_hight = $s3_result->height();
注意:
所有直接保存到s3的图片都是公开可读的。您可以在aws控制台中为s3存储桶文件夹设置权限,以确保图片的隐私性。