๐ข๐ฝ๐๐ถ๐บ๐ถ๐๐ถ๐ป๐ด ๐๐บ๐ฎ๐ด๐ฒ๐ ๐ข๐ป ๐ฆ๐ฏ ๐ช๐ถ๐๐ต ๐ฆ๐ต๐ผ๐ฟ๐๐ฃ๐ถ๐ ๐ฒ๐น If your Laravel app lets users upload images to S3, you may be storing large files. This can increase page weight, CDN traffic, and storage costs over time.
Here's how to optimize images:
- Upload images to S3
- Dispatch a queued job to optimize the image with ShortPixel
- Overwrite the S3 object with the smaller version
You can use a service to wrap ShortPixel's API. This service will handle the optimization and return the optimized image.
To set this up:
- Install the AWS S3 Flysystem adapter
- Configure your S3 disk in config/filesystems.php
- Create a ShortPixel service to handle optimization
- Dispatch a queued job to optimize the image after upload
Some key points to consider:
- Use lossy compression for strong compression with good visual quality
- Use glossy compression for preserving fine details
- Store WebP versions of images for better compression
- Handle private S3 buckets with temporary signed URLs
- Make your job idempotent to avoid spending credits on the same file repeatedly
Source: https://dev.to/biancarus/optimizing-images-stored-on-s3-with-shortpixel-a-laravel-walkthrough-3058