Laravel PHP – Cropping and uploading an image with Croppie plugin using jQuery Ajax

Laravel PHP – Cropping and uploading an image with Croppie plugin using jQuery Ajax

In this post we will give you information about Laravel PHP – Cropping and uploading an image with Croppie plugin using jQuery Ajax. Hear we will give you detail about Laravel PHP – Cropping and uploading an image with Croppie plugin using jQuery AjaxAnd how to use it also give you demo for it if it is necessary.

Laravel PHP – Cropping and uploading an image with Croppie plugin using jQuery Ajax

In this Laravel PHP Tutorial, I am going to tell you how to crop image using jQuery croppie plugin and upload via ajax request in Laravel 5.6

Sometimes you need to implement the functionality to get thumbnail of any images then you can use jQuery croppie plugin to crop the images and upload the thumbnail on the server.

In this example, you will learn how to crop any images into square or circle and set the zoom features of a croppie instance.

There are so many options available to croppie plugin that you can use while cropping any images.

You can remove outer parts of any images with the help of image cropping features using jQuery Javascript plugin.

Sometimes you will notice on the social media sites where after uploading the images like profile pictures they give you option to crop the images.

Using jQuery Croppie plugin, you can easily add the cropping or re-sizing functionality to your web application.


Step1: Add Routes

In this step, I will add routes to display the form for croppie view and handle the request to upload cropped images on the server.


routes/web.php

Route::get('crop-image-before-upload-using-croppie', 'CropImageController@index');
Route::post('crop-image-before-upload-using-croppie', ['as'=>'croppie.upload-image','uses'=>'CropImageController@uploadCropImage']);


Step2: Create CropImage Controller

In this step, I will create a controller CropImageController.php in following path app/Http/Controllers/.


app/Http/Controllers/CropImageController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

classCropImageControllerextends Controller
{
   
    publicfunctionindex()
    {
      return view('croppie');
    }
   
    publicfunctionuploadCropImage(Request $request)
    {
        $image=$request->image;

        list($type, $image) =explode(';', $image);
        list(, $image)      =explode(',', $image);
        $image=base64_decode($image);
        $image_name=time().'.png';
        $path= public_path('upload/'.$image_name);

        file_put_contents($path, $image);
        return response()->json(['status'=>true]);
    }
}

Don’t forget to create upload directory inside the public directory.


Step3: Create View Blad File (croppie.blade.php)

In this step, I will create a new blade file croppie.blade.php and import the jQuery libraries for Croppie plugin.


resources/views/croppie.blade.php

<htmllang="en">
<head>
  <title>Laravel PHP - Cropping and uploading an image with Croppie plugin using jQuery Ajax </title>
  <linkrel="stylesheet"href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.min.css">
  <linkrel="stylesheet"href="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.2/croppie.min.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.2/croppie.js"></script>
  <metaname="csrf-token"content="{{ csrf_token() }}">
</head>

<body>
<divclass="container">
  <divclass="panel panel-info">
    <divclass="panel-heading">Laravel PHP - Cropping and uploading an image with Croppie plugin using jQuery Ajax</div>
    <divclass="panel-body">

      <divclass="row">
        <divclass="col-md-4 text-center">
        <divid="upload-demo"></div>
        </div>
        <divclass="col-md-4"style="padding:5%;">
        <strong>Select image to crop:</strong>
        <inputtype="file"id="image">

        <buttonclass="btn btn-primary btn-block upload-image"style="margin-top:2%">Upload Image</button>
        </div>

        <divclass="col-md-4">
        <divid="preview-crop-image"style="background:#9d9d9d;width:300px;padding:50px 50px;height:300px;"></div>
        </div>
      </div>

    </div>
  </div>
</div>


<script type="text/javascript">

$.ajaxSetup({
headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});


var resize = $('#upload-demo').croppie({
    enableExif:true,
    enableOrientation:true,    
    viewport: { // Default { width: 100, height: 100, type: 'square' } 
        width:200,
        height:200,
        type:'circle'//square
    },
    boundary: {
        width:300,
        height:300
    }
});


$('#image').on('change', function () { 
  var reader =new FileReader();
    reader.onload =function (e) {
      resize.croppie('bind',{
        url: e.target.result
      }).then(function(){
        console.log('jQuery bind complete');
      });
    }
    reader.readAsDataURL(this.files[]);
});


$('.upload-image').on('click', function (ev) {
  resize.croppie('result', {
    type:'canvas',
    size:'viewport'
  }).then(function (img) {
    $.ajax({
      url:"{{route('croppie.upload-image')}}",
      type:"POST",
      data: {"image":img},
      success:function (data) {
        html ='<img src="'+ img +'" />';
        $("#preview-crop-image").html(html);
      }
    });
  });
});


</script>


</body>
</html>


  • PHP jQuery crop and resize image before upload using croppie plugin

  • PHP image upload and resize it before uploading tutorial

Show Demo

Hope this code and post will helped you for implement Laravel PHP – Cropping and uploading an image with Croppie plugin using jQuery Ajax. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  13  =  17

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US