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.

Read Also:  Image Upload in Laravel 7 Example Tutorial

Phase 1: Install Laravel 7/6

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

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:  Learn Laravel (Best Laravel Tutorials for Beginners)

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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  select query in php mysql with example
  2. Read Also:  jQuery get value of select onChange Example
  3. Read Also:  Laravel 5.8 Image Resize Tutorials
  4. Read Also:  Send PHP variable to bootstrap modal popup via Ajax
  5. Read Also:  ERROR in node_modules/rxjs/internal/types.d.ts(81,44): error TS1005: ';' expected.
  6. Read Also:  Top 5 Best php PDF Generation libraries
  7. Read Also:  Laravel 6 Remove public from URL
  8. Read Also:  how to upload image in ckeditor in Laravel?
  9. Read Also:  ASP.NET Core MVC Tutorials with Example
  10. Read Also:  Laravel Many to many relationships with itself