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

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.

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/.


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CropImageController extends Controller
    public function index()
      return view('croppie');
    public function uploadCropImage(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.

<html lang="en">
  <title>Laravel PHP - Cropping and uploading an image with Croppie plugin using jQuery Ajax </title>
  <link rel="stylesheet" href="">
  <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></script>
  <meta name="csrf-token" content="{{ csrf_token() }}">

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

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

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

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


<script type="text/javascript">

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) {
        console.log('jQuery bind complete');

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



Phone: (+91) 8800417876
Noida, 201301
sakarya escort sakarya escort bayan sakarya escort serdivan escort sakarya escort sakarya escort serdivan escort
karasu escort akyazı escort serdivan escort akyazı escort ferizli escort sapanca escort serdivan escort söğütlü escort sakarya escort izmit escort arifiye escort erenler escort hendek escort kocaali escort pamukova escort taraklı escort geyve escort