Laravel Resize Image Before Upload Example

Today, We want to share with you Laravel Resize Image Before Upload Example.In this post we will show you laravel 7/6 image intervention example, hear for resize image in laravel using intervention image library we will give you demo and example for implement.In this post, we will learn about resize image intervention laravel 7/6 with an example image upload in laravel.

Laravel Resize Image Before Upload Example

There are the Following The simple About PHP Laravel 5 Intervention image upload and resize tutorial Full Information With upload image laravel Example and source code.

As I will cover this Post with live Working example to develop laravel 6/7 resizes image before upload, so the Smooth image upload and resizes in laravel is used for this laravel upload image example is following below.

Phase 1: Install Laravel 7/6

Laravel 6 application setup upload image laravel then we have to get fresh laravel 6 application

Read Also:  Angular 4 Tutorial for beginners - Angular 4 Introduction

composer create-project --prefer-dist laravel/laravel blog

Phase 2: Install Intervention Image Package

install intervention/image for resizes image

composer require intervention/image

config/app.php

return [
    ......
    $provides => [
        ......
        ......,
        'Intervention\Image\ImageServiceProvider'
    ],
    $aliases => [
        .....
        .....,

        'Image' => 'Intervention\Image\Facades\Image'
    ]
]

Phase 3: Create Routes

routes/web.php

Route::get('flexifyLiveImg', '[email protected]');
Route::post('liveFlexifyImg', '[email protected]')->name('liveFlexifyImg');

Phase 4: Create Controller File

create new flexifyImgController for image uploading and resizeing image

php artisan make:controller flexifyImgController

app/Http/Controllers/flexifyImgController.php

<?php
  
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
use App\Http\Requests;
use Image;
class flexifyImgController extends Controller
{
  
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function flexifyLiveImg()
    {
        return view('flexifyLiveImg');
    }
  
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function liveFlexifyImg(Request $request)
    {
        $this->validate($request, [
            'title' => 'required',
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);
  
        $image = $request->file('image');
        $input['imagename'] = time().'.'.$image->extension();
     
        $destinationPath = public_path('/thumbnail');
        $img = Image::make($image->path());
        $img->resize(100, 100, function ($constraint) {
            $constraint->aspectRatio();
        })->save($destinationPath.'/'.$input['imagename']);
   
        $destinationPath = public_path('/images');
        $image->move($destinationPath, $input['imagename']);
   
        return back()
            ->with('success','Image Upload successful')
            ->with('flexifyImgNm',$input['imagename']);
    }
   
}

Phase 5: View File and Create Upload directory

resources/views/flexifyLiveImg.blade.php

@extends('layouts.app')
   
@section('content')
<div class="container">
<h1>Resize Image Uploading Demo</h1>
@if (count($errors) > 0)
    <div class="alert alert-danger">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
   
@if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
    <button type="button" class="close" data-dismiss="alert">×</button>    
    <strong>{{ $message }}</strong>
</div>
<div class="row">
    <div class="col-md-4">
        <strong>Original Image:</strong>
        <br/>
        <img src="/images/{{ Session::get('flexifyImgNm') }}" />
    </div>
    <div class="col-md-4">
        <strong>Convert Thumbnail Image:</strong>
        <br/>
        <img src="/thumbnail/{{ Session::get('flexifyImgNm') }}" />
    </div>
</div>
@endif
   
{!! Form::open(array('route' => 'liveFlexifyImg','enctype' => 'multipart/form-data')) !!}
    <div class="row">
        <div class="col-md-4">
            <br/>
            {!! Form::text('title', null,array('class' => 'form-control dsp','placeholder'=>'Add Title')) !!}
        </div>
        <div class="col-md-12">
            <br/>
            {!! Form::file('image', array('class' => 'image')) !!}
        </div>
        <div class="col-md-12">
            <br/>
            <button type="submit" class="btn btn-success">Upload Your Image</button>
        </div>
    </div>
{!! Form::close() !!}
</div>
@endsection

Web Programming Tutorials Example with Demo

Read :

Read Also:  Simple Autocomplete Combobox in jQuery

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel image converter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.