Laravel Cropping and uploading an image with Croppie plugin using jQuery Ajax
In this post we will show you Laravel Cropping and uploading an image with Croppie plugin using jQuery Ajax, hear for Laravel Cropping and uploading an image with Croppie plugin using jQuery Ajax we will give you demo and example for implement.
Sometimes we need to implement the functionality to get thumbnail of any images then we can use jQuery croppie plugin to crop the images and upload the thumbnail on the server.
In this given example, we 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 we can use while cropping any images.
we can remove outer parts of any images with the help of image cropping features using jQuery Javascript plugin.
Sometimes we will notice on the social media sites where after uploading the images like profile pictures they give we option to crop the images.
Using jQuery Croppie plugin, we 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 App\Http\Controllers; use Illuminate\Http\Request; class CropImageController extends Controller { public function index() { return view('croppie'); } public function uploadCropImage(Request $request) { $images = $request->image; list($type, $images) = explode(';', $images); list(, $images) = explode(',', $image); $images = base64_decode($images); $image_name= time().'.png'; $path = public_path('upload/'.$image_name); file_put_contents($path, $images); 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
<html lang="en"> <head> <title>Laravel Cropping and uploading an image with Croppie plugin using jQuery Ajax </title> <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> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.2/croppie.min.css"> <meta name="csrf-token" content="{{ csrf_token() }}"> </head> <body> <div class="container"> <div class="panel panel-info"> <div class="panel-heading">Laravel 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> <div class="col-md-4" style="padding:6%;"> <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> <div class="col-md-4"> <div id="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: 250, height: 250, type: 'circle' //square }, boundary: { width: 350, height: 350 } }); $('#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[0]); }); $('.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>
Hope this code and post will helped you for implement Laravel 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 onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org