ホーム > Laravel > How to compress and process images using intervention-image in Laravel
Laravel

How to compress and process images using intervention-image in Laravel

Thank you for your continued support.
This article contains advertisements that help fund our operations.

This article summarizes how to compress and process images using intervention-image in Laravel.

Environment

Laravel and PHP Versions

php v7.4.1
Laravel Framework 6.16.0

composer.json

"intervention/image": "^2.5"

Installation

Read the documentation here

Run the command

$ composer require intervention/image

to install.

Edit config/app.php

~~~~~~
'providers' =>[
~~~~~~
Intervention\Image\ImageServiceProvider::class
~~~~~~
]

~~~~~~
'aliases' => [
~~~~~~
'Image' => Intervention\Image\Facades\Image::class
~~~~~~
]

Add the above to the file.

Run the following command to add files

$ php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravelRecent"

This command will create config/image.php.

It includes instructions to specify the driver as gd, which is a graphics library for image processing included in Laravel.

Implementation

Everything from this point onward is written in the controller.

use Image;

First, declare this.

Then,

In this example,

/storage/app/public/pop/pop1.png

Assuming the file is located here.

$path = storage_path('app/public/pop/pop1.png');
$img = Image::make($path);

This defines the image data (be careful with specifying the file path, it can be confusing with directories and such).

You can display the image by returning

$img

in the response.

Using S3 image data

You can use image URLs for publicly accessible images, not just limited to S3.

$img = Image::make('Image URL here');
$img->resize(430, 430);
$img->encode('png');
$img->save('Save path here');

Overlaying images

Simply insert like this.

$img->insert($img2, 'top-left', 0, 30);

This will insert at the top-left, 0px horizontally and 30px vertically.

You can insert text or make it transparent.

Saving images

Saving to Laravel storage

$filePath = storage_path('app/temp');
$img->encode('jpg')->save($filePath);

The above example saves to a temporary folder, but you can save using Laravel's functionality.

Saving from Laravel storage to S3

$disk = Storage::disk('s3');
$uploadPath = 'upload';
$disk->put($uploadPath, file_get_contents($filePath), 'public');

This example uses file_get_contents() to save from a temporary folder to S3.

Deleting temporary files

Normal deletion

Storage::disk('local')->delete($filePath);

Special case

For download functionality

return response()->download($file, 'Filename.jpg', $headers)->deleteFileAfterSend(true);

By using the deleteFileAfterSend(true) method, it will delete the temporary file after sending. Convenient.

That's all.

The library is useful and can accomplish many tasks with minimal code.

Specifying file paths can be quite challenging, so make sure to read error messages carefully and try experimenting.

Conclusion

That's all.

We used Intervention-Image to process images.

Using Resize or webp, you can reduce the size of large images, so I recommend it.

Until next time!

Web エンジニアの転職なら【転職ドラフト】

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!