Step 1: Install Intervention Image Package to Project
composer require intervention/image
'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 });
<?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'); });
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)
Leave a reply