[Laravel] Compress and Upload Images to S3

Compressing images is easy way to decrease the page loading time and increase performance. Tinify has the better option to compress the images in lossless way to not to lose the quality of the image. Here we gonna use Tinify REST api in laravel to compress the images and directly upload the images to S3 using YsTinifyLaravel package.

You can install the package using Composer.

After successfully installed the YsTinifyLaravel package, add the following in config\app.php file.

Under providers,

Under aliases,

After set a env variable TINIFY_APIKEY with your tinypng api key in .env file. You can get your tinify api key here. You can compress upto 500 images per month freely, after that you can pay as per the number of images you compressing at less price.

If you want to directly upload the image to aws s3, you need set the env variables of following with your aws s3 credentials.

Examples

Upload to s3

When you want to upload the image directly to s3, you can use the following.

NOTE: All the images directly save to s3 is publicably readable. And you can set permissions for s3 bucket folder in your aws console to make sure the privacy of images.

When you want to upload original image also in s3 before compressing, you can upload the image directly to s3 and get the url of the same and compress the image using the url and re upload to s3 in a different folder. So you can keep both original and compressed images in s3.

Hope you like it. Thanks!

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!