minipng/laravel-minipng
Composer 安装命令:
composer require minipng/laravel-minipng
包简介
Laravel package for MiniPNG API integration - Image and PDF processing
关键字:
README 文档
README
Laravel package for integrating with MiniPNG API - Image and PDF processing services.
Version: 1.0.0
Release Date: June 29, 2025
Features
- 🖼️ Image Compression - Compress images while maintaining quality
- 🔄 Image Format Conversion - Convert between PNG, JPG, JPEG, WebP, GIF
- 📄 PDF Compression - Reduce PDF file sizes
- 🖼️ PDF to Images - Convert PDF pages to images
- 👤 User Profile - Get API usage information
- 🛡️ Error Handling - Comprehensive exception handling
- ⚙️ Configuration - Easy configuration management
Requirements
- PHP 8.0 or higher
- Laravel 9.0, 10.0, or 11.0
- MiniPNG API key
Getting Started
1. Get Your API Token
To use this package, you need a MiniPNG API token:
- Create an account or login at https://minipng.com/en/login
- Get your API token from https://minipng.com/user/panel/api-token
- Enjoy 500 free credits for one month upon registration
2. Installation
Install the package via Composer:
composer require minipng/laravel-minipng
3. Publish Configuration
Publish the configuration file:
php artisan vendor:publish --tag=minipng-config
4. Configure API Key
Add your MiniPNG API key to your .env file:
MINIPNG_API_KEY=your_api_key_here MINIPNG_BASE_URL=https://minipng.com
Configuration
The configuration file config/minipng.php contains the following options:
return [ 'api_key' => env('MINIPNG_API_KEY', ''), 'base_url' => env('MINIPNG_BASE_URL', 'https://minipng.com'), 'api_version' => env('MINIPNG_API_VERSION', 'v1'), 'timeout' => env('MINIPNG_TIMEOUT', 30), 'retry_attempts' => env('MINIPNG_RETRY_ATTEMPTS', 3), 'default_image_quality' => env('MINIPNG_DEFAULT_IMAGE_QUALITY', 85), 'default_pdf_images_quality' => env('MINIPNG_DEFAULT_PDF_IMAGES_QUALITY', 'medium'), 'default_pdf_images_format' => env('MINIPNG_DEFAULT_PDF_IMAGES_FORMAT', 'png'), ];
Usage
Using the Facade
use MiniPNG\LaravelMiniPNG\Facades\MiniPNG; // Compress an image $result = MiniPNG::compressImage('https://example.com/image.jpg'); // Convert image format $result = MiniPNG::convertImage('https://example.com/image.jpg', 'webp', 90); // Compress PDF $result = MiniPNG::compressPdf('https://example.com/document.pdf'); // Convert PDF to images $result = MiniPNG::convertPdfToImages('https://example.com/document.pdf', 'high', 'jpg'); // Get user profile $profile = MiniPNG::getProfile();
Using Dependency Injection
use MiniPNG\LaravelMiniPNG\Contracts\MiniPNGInterface; class ImageController extends Controller { public function __construct(private MiniPNGInterface $minipng) { } public function compress(Request $request) { $result = $this->minipng->compressImage($request->input('image_url')); return response()->json($result); } }
Using the Service Container
$minipng = app('minipng'); $result = $minipng->compressImage('https://example.com/image.jpg');
API Methods
Image Processing
compressImage(string $sourceUrl): array
Compress an image file to reduce its size while maintaining quality.
$result = MiniPNG::compressImage('https://example.com/image.jpg');
convertImage(string $sourceUrl, string $outputFormat, ?int $quality = null): array
Convert an image to a different format.
// Convert to WebP with 90% quality $result = MiniPNG::convertImage('https://example.com/image.jpg', 'webp', 90); // Convert to PNG with default quality $result = MiniPNG::convertImage('https://example.com/image.jpg', 'png');
Supported formats: png, jpg, jpeg, webp, gif
PDF Processing
compressPdf(string $sourceUrl): array
Compress a PDF file to reduce its size while maintaining quality.
$result = MiniPNG::compressPdf('https://example.com/document.pdf');
convertPdfToImages(string $sourceUrl, ?string $imagesQuality = null, ?string $imagesFormat = null): array
Convert a PDF document to a set of images (one per page).
// Convert to high-quality JPG images $result = MiniPNG::convertPdfToImages('https://example.com/document.pdf', 'high', 'jpg'); // Convert to medium-quality PNG images (default) $result = MiniPNG::convertPdfToImages('https://example.com/document.pdf');
Quality options: low, medium, high
Format options: png, jpg
User Information
getProfile(): array
Get user profile and API usage information.
$profile = MiniPNG::getProfile();
Response Format
Success Response (200/201)
{
"success": true,
"output_ext": "jpg",
"size_before": 1024000,
"size_after": 512000,
"compression_percentage": "50%",
"download": "https://example.com/compressed/image.jpg"
}
Error Response (400/404/500)
{
"error": "Invalid source URL provided",
"message": "The provided URL is not accessible or invalid"
}
Error Handling
The package throws MiniPNGException for API errors:
use MiniPNG\LaravelMiniPNG\Exceptions\MiniPNGException; try { $result = MiniPNG::compressImage('https://example.com/image.jpg'); } catch (MiniPNGException $e) { // Handle API errors Log::error('MiniPNG API Error: ' . $e->getMessage()); }
Examples
Controller Example
<?php namespace App\Http\Controllers; use MiniPNG\LaravelMiniPNG\Facades\MiniPNG; use MiniPNG\LaravelMiniPNG\Exceptions\MiniPNGException; use Illuminate\Http\Request; class ImageController extends Controller { public function compress(Request $request) { $request->validate([ 'image_url' => 'required|url', ]); try { $result = MiniPNG::compressImage($request->input('image_url')); return response()->json([ 'success' => true, 'data' => $result, ]); } catch (MiniPNGException $e) { return response()->json([ 'success' => false, 'message' => $e->getMessage(), ], 400); } } public function convert(Request $request) { $request->validate([ 'image_url' => 'required|url', 'format' => 'required|in:png,jpg,jpeg,webp,gif', 'quality' => 'nullable|integer|min:1|max:100', ]); try { $result = MiniPNG::convertImage( $request->input('image_url'), $request->input('format'), $request->input('quality') ); return response()->json([ 'success' => true, 'data' => $result, ]); } catch (MiniPNGException $e) { return response()->json([ 'success' => false, 'message' => $e->getMessage(), ], 400); } } }
Artisan Command Example
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use MiniPNG\LaravelMiniPNG\Facades\MiniPNG; class CompressImage extends Command { protected $signature = 'image:compress {url}'; protected $description = 'Compress an image using MiniPNG API'; public function handle() { $url = $this->argument('url'); $this->info("Compressing image: {$url}"); try { $result = MiniPNG::compressImage($url); $this->info("Compression successful!"); $this->table(['Metric', 'Value'], [ ['Original Size', $result['size_before'] . ' bytes'], ['Compressed Size', $result['size_after'] . ' bytes'], ['Compression', $result['compression_percentage']], ['Download URL', $result['download']], ]); } catch (\Exception $e) { $this->error("Compression failed: " . $e->getMessage()); } } }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email security@minipng.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Support
For support, please contact: info@minipng.com
minipng/laravel-minipng 适用场景与选型建议
minipng/laravel-minipng 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 06 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「laravel」 「image-processing」 「image-compression」 「pdf-processing」 「minipng」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 minipng/laravel-minipng 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 minipng/laravel-minipng 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 minipng/laravel-minipng 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Smarty plugin that adds some image related template syntax enchantments
Build imgproxy URLs in PHP with optional encryption and signing.
Build imgproxy URLs in Laravel with optional encryption and signing.
A PSR-7 compatible library for making CRUD API endpoints
Symfony 8 bundle for on-demand image processing, resizing, cropping, AVIF/WebP conversion, and caching. Filter pipelines with fit, fill, optimize, strip, interlace processors and avifenc, cwebp, MozJPEG, pngquant post-processors. Filesystem, stream, and S3 loaders/resolvers. HMAC-signed runtime URLs
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-29