yasmuru/ys-tinify-laravel
Composer 安装命令:
composer require yasmuru/ys-tinify-laravel
包简介
Tinify API support for Laravel
README 文档
README
Modern Laravel package for TinyPNG/TinyJPG image compression API. Compress and optimize your images with ease.
Requirements
- PHP 8.2, 8.3, or 8.4
- Laravel 11.x, 12.x, or 13.x
Installation
Install the package via Composer:
composer require yasmuru/ys-tinify-laravel
Configuration
The package will automatically register itself (Laravel auto-discovery).
Publish the configuration file (optional):
php artisan vendor:publish --tag=tinify-config
Add your TinyPNG API key to your .env file:
TINIFY_APIKEY=your-api-key-here
You can get a free API key at tinypng.com/developers.
Optional: AWS S3 Configuration
If you want to directly upload compressed images to AWS S3, add these environment variables:
S3_KEY=your-aws-access-key S3_SECRET=your-aws-secret-key S3_REGION=your-s3-region
The bucket name is passed as an argument to each upload call, not configured globally.
Usage
Basic Image Compression
use yasmuru\LaravelTinify\Facades\Tinify; // Compress from file $result = Tinify::fromFile('/path/to/image.jpg'); // Compress from buffer/string $result = Tinify::fromBuffer($imageData); // Compress from URL $result = Tinify::fromUrl('https://example.com/image.jpg'); // Save compressed image to file $result->toFile('/path/to/compressed-image.jpg'); // Get compressed image as buffer $compressedData = $result->toBuffer();
Direct Upload to AWS S3
Upload compressed images directly to S3 without saving locally:
use yasmuru\LaravelTinify\Facades\Tinify; // Compress file and upload to S3 $s3Result = Tinify::fileToS3( '/path/to/image.jpg', 'my-bucket', '/images/compressed-image.jpg' ); // Compress buffer and upload to S3 $s3Result = Tinify::bufferToS3( $imageData, 'my-bucket', '/images/compressed-image.jpg' ); // Compress URL and upload to S3 $s3Result = Tinify::urlToS3( 'https://example.com/image.jpg', 'my-bucket', '/images/compressed-image.jpg' ); // Get S3 image details $imageUrl = $s3Result->location(); $imageWidth = $s3Result->width(); $imageHeight = $s3Result->height();
Advanced Features
use yasmuru\LaravelTinify\Facades\Tinify; // Check compression count (free tier has 500 compressions/month). // compressionCount() is an alias for getCompressionCount(). $count = Tinify::getCompressionCount(); // Set custom API key at runtime Tinify::setKey('your-custom-api-key'); // Set app identifier Tinify::setAppIdentifier('My Application'); // Validate the API key. Returns true only if the key is accepted by Tinify; // returns false for an invalid key or any network/server failure. $isValid = Tinify::validate();
Resizing Images
You can combine compression with resizing using the Tinify SDK methods:
$result = Tinify::fromFile('/path/to/image.jpg') ->resize([ 'method' => 'fit', 'width' => 150, 'height' => 100 ]); $result->toFile('/path/to/resized-image.jpg');
Important Notes
- S3 Permissions: Images uploaded directly to S3 are publicly readable by default. Configure your S3 bucket permissions according to your privacy requirements.
- API Limits: The free TinyPNG API tier allows 500 compressions per month. Monitor your usage with
getCompressionCount(). - Supported Formats: PNG, JPEG, and WebP formats are supported.
Testing
composer install composer check # runs PHPStan (level 8) + PHPUnit # or individually: composer analyse composer test
CI runs the suite against PHP 8.2/8.3/8.4 × Laravel 11/12/13.
License
The MIT License (MIT). Please see License File for more information.
Credits
统计信息
- 总下载量: 36.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 41
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-11-15