composer require golchha21/resmushit
php artisan vendor:publish --provider="Golchha21\ReSmushIt\Providers\ServiceProvider" --tag=config
config/ReSmushIt.php with your API key (add 'api_key' => 'your_api_key').use Golchha21\ReSmushIt\Facades\ReSmushIt;
$result = ReSmushIt::optimize(public_path('images/example.jpg'));
Golchha21\ReSmushIt\Facades\ReSmushIt for quick usage.Golchha21\ReSmushIt\ReSmushIt for custom logic.config/ReSmushit.php for adjusting settings.Single Image Optimization:
$result = ReSmushIt::optimize($filePath);
// Returns array with 'success', 'original', 'optimized', 'errors'
Batch Processing:
$files = [public_path('images/*.jpg')];
$results = ReSmushIt::optimizeBatch($files);
Queue-Based Processing (for large volumes):
use Golchha21\ReSmushIt\Jobs\OptimizeImage;
OptimizeImage::dispatch($filePath)->onQueue('optimize');
Event Listeners (e.g., optimize on upload):
// In EventServiceProvider
protected $listen = [
'eloquent.attached' => ['Golchha21\ReSmushIt\Listeners\OptimizeUploadedImage'],
];
Storage Integration:
Use Storage::disk('public')->put() to save optimized images directly to cloud storage.
$optimizedPath = storage_path('app/optimized.jpg');
Storage::disk('s3')->put('optimized.jpg', file_get_contents($optimizedPath));
Media Library Packages (e.g., Spatie Media Library):
Extend the Analyzable trait or use events to trigger optimization post-upload.
API Endpoints: Create a controller to expose optimization as an API:
public function optimize(Request $request) {
$file = $request->file('image');
$result = ReSmushIt::optimize($file->path());
return response()->json($result);
}
Artisan Command: Build a custom command for bulk optimization:
php artisan optimize:batch /path/to/images
API Key Missing:
Invalid API key or empty responses.'api_key' is set in config/ReSmushit.php.File Size Limits:
File too large (default: 5MB).'max_filesize' in config or pre-process large files.Mime Type Restrictions:
.webp).'mime' array in config.Rate Limiting:
Too many requests (reSmush.it has limits).EXIF Data Loss:
'exif' => false).'exif' => true if metadata preservation is critical.Network Issues:
ReSmushIt::setTimeout(30) or retry logic.'debug' => true to config to log API responses.ReSmushIt::getLastResponse() to inspect the raw API output.$result['success'] and $result['errors'] before processing.Custom API Endpoint: Override the default endpoint in config:
'endpoint' => 'https://custom-resmush.it/api',
Pre/Post-Processing:
Extend the Golchha21\ReSmushIt\ReSmushIt class to add logic before/after optimization:
public function optimize($filePath) {
// Pre-processing (e.g., resize)
$result = parent::optimize($filePath);
// Post-processing (e.g., rename)
return $result;
}
Fallback for Offline Mode: Implement a local fallback (e.g., Intervention Image) when reSmush.it is unavailable:
if (!ReSmushIt::isOnline()) {
return $this->fallbackOptimize($filePath);
}
Webhook Integration: Use reSmush.it’s webhook feature (if supported) to trigger post-optimization actions:
// In config
'webhook_url' => route('resmushit.webhook'),
$batch = array_chunk($files, 10);
foreach ($batch as $files) {
ReSmushIt::optimizeBatch($files);
}
$cacheKey = 'optimized_'.md5($filePath);
if (Cache::has($cacheKey)) {
return Cache::get($cacheKey);
}
$result = ReSmushIt::optimize($filePath);
Cache::put($cacheKey, $result, now()->addHours(1));
return $result;
How can I help you explore Laravel packages today?