Friday, April 3, 2020

Laravel Cropping and uploading an image

No comments

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.
1
2
3
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<html lang="en">
<head>
  <title>Laravel Cropping and uploading an image with Croppie plugin using jQuery Ajax </title>
  <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 pakainfo. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs pakainfo.com

No comments :

Post a Comment