Posts Learn Components Snippets Categories Tags Tools About
/

How to change Image Format in Laravel and Save to Disk

Learn how to change image format from jpg to png and change format from png to webp in Laravel the easy way

Created on Sep 10, 2021

7034 views

In this short snippet, you will learn how to change image format from one to the other in Laravel using the Intervention Image package. The steps are very simple so let's get started.

Step 1: Install Intervention Image Package to Project

Firstly you have to install Intervention Image to your project using the composer command below.
composer require intervention/image
If you do want to add the alias you can define this in $aliases array.
'Image' => Intervention\Image\Facades\Image::class

Step 2: Retrieve the Image


Next, you will have to retrieve the image from either "user upload" or from the "storage" itself. Below are ways of how you can get using both methods. Do note that it's defined within a route so that we can later call to process the image.
<?php // getting image from the user upload

Route::get('change-image-format', function (Request $request) {
    $image = $request->file('image'); // let's assume the image is in "png" format
});
If you are getting from the storage then you can write your code like below.
<?php // getting image from the storage

Route::get('change-image-format', function (Request $request) {
    $user_profile = User::first()->profile_picture; // image path e.g user-profile-picture.png

    $image = \Storage::disk('s3')->get($user_profile); // getting the user profile image form "s3"
});

Step 3: Convert The Image Using Intervention/Image


Once you have retrieved the image using either one of the ways, now you can load the image to the Intervention Image class and perform the conversion. Do note that you can convert to any format "png" to "jpg", "webp" and any other formats.
<?php // getting image from the storage

Route::get('change-image-format', function (Request $request) {
    // retrieve the image form the database
    $user_profile = User::first()->profile_picture;

    // retrieve the image from "s3"
    $image = \Storage::disk('s3')->get($user_profile);

    // load the image to Intervention image and "stream" it to other format
    $interventionImage = \Image::make($image)->stream("webp", 100); // in this case webp is the format and the quality is set to 100%
    \Storage::disk('s3')->put('some-imge-name.webp', $interventionImage, 'public');
});
Now when you visit "/change-image-format" endpoint in your browser the image format will be converted from "png" to "webp" or any other image format that you have specified.

Other Formats


Below are some of the other image formats that you can make use of. Any other image format like "webp" is also supported.
  • jpg — return JPEG encoded image data
  • png — return Portable Network Graphics (PNG) encoded image data
  • gif — return Graphics Interchange Format (GIF) encoded image data
  • tif — return Tagged Image File Format (TIFF) encoded image data
  • bmp — return Bitmap (BMP) encoded image data
  • data-url — encode current image data in data URI scheme (RFC 2397)
If you do find this tutorial to be helpful do make sure to share it with your friends and cheers, happy coding!

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

)