Crop and Upload Image in Laravel

As we have seen in social media, we always upload images, but before uploading the Image, we crop the image for the best feel. Crop and Upload Image in Laravel.

We can crop the image using php and its framework Laravel.

Install laravel using the below command

composer create-project –prefer-dist laravel/laravel ImageCrop

I am using Laravel 8 so you can use 7,6, or 5. It is unto you. And Make a folder upload inside the public folder for Image Upload Images.

First create routes in routes.php for the project.

Route::get('/', 'CropImage@loadImage');

Route::post('/cropImg', 'CropImage@cropImg')->name('cropImg');

Now I am creating one controller for Image Cropping and My Controller name will be CropImage.php. I have disabled csrf.


namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class CropImage extends BaseController
    public function loadImage()
       return view('cropImage');
    public function cropImg()
          $data = $_POST['image'];
            $image_array_1 = explode(";", $data);
            $image_array_2 = explode(",", $image_array_1[1]);
            $data = base64_decode($image_array_2[1]);
            $image_name = 'upload/' . time() . '.png';
            file_put_contents($image_name, $data);
            echo $image_name;

Now I am creating view file here



<!DOCTYPE html>
		<title>Crop Image Before Upload using CropperJS with PHP</title>
		<link rel="stylesheet" href="" />
		<script src=""></script>
        <script src=""></script>        
		<link rel="stylesheet" href="" />
		<link href="" rel="stylesheet"/>
		<script src=""></script>
		<script src=""></script>
        <link rel="stylesheet" href="style.css" />
		.image_area {
		  position: relative;

		img {
		  	display: block;
		  	max-width: 100%;

		.preview {
  			overflow: hidden;
  			width: 160px; 
  			height: 160px;
  			margin: 10px;
  			border: 1px solid red;

  			max-width: 1000px !important;

		.overlay {
		  position: absolute;
		  bottom: 10px;
		  left: 0;
		  right: 0;
		  background-color: rgba(255, 255, 255, 0.5);
		  overflow: hidden;
		  height: 0;
		  transition: .5s ease;
		  width: 100%;

		.image_area:hover .overlay {
		  height: 50%;
		  cursor: pointer;

		.text {
		  color: #333;
		  font-size: 20px;
		  position: absolute;
		  top: 50%;
		  left: 50%;
		  -webkit-transform: translate(-50%, -50%);
		  -ms-transform: translate(-50%, -50%);
		  transform: translate(-50%, -50%);
		  text-align: center;
		<div class="container" align="center">
			<br />
			<h3 align="center">Crop Image in PHP</h3>
			<br />
			<div class="row">
				<div class="col-md-4">&nbsp;</div>
				<div class="col-md-4">
					<div class="image_area">
						<form method="post">
							<label for="upload_image">
								<img src="2.png" id="uploaded_image" class="img-responsive img-circle" />
								<div class="overlay">
									<div class="text">Change your DP</div>
								<input type="file" name="image" class="image" id="upload_image" style="display:none" />
    		<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">

              	<div class="modal-dialog modal-lg" role="document">
			    	<div class="modal-content">
			      		<div class="modal-header">
			        		<h5 class="modal-title">Crop Image Before Upload</h5>
			        		<button type="button" class="close" data-dismiss="modal" aria-label="Close">
			          			<span aria-hidden="true">×</span>
			      		<div class="modal-body">
			        		<div class="img-container">
			            		<div class="row">
			                		<div class="col-md-8">
			                    		<img src="" id="sample_image" />
			                		<div class="col-md-4">
			                    		<div class="preview"></div>
			      		<div class="modal-footer">
			      			<button type="button" id="crop" class="btn btn-primary">Crop</button>
			        		<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>



	var $modal = $('#modal');

	var image = document.getElementById('sample_image');

	var cropper;

		var files =;

		var done = function(url){
			image.src = url;

		if(files && files.length > 0)
			reader = new FileReader();
			reader.onload = function(event)

	$modal.on('', function() {
		cropper = new Cropper(image, {
			aspectRatio: 1,
			viewMode: 3,
	}).on('', function(){
   		cropper = null;

		canvas = cropper.getCroppedCanvas({

			url = URL.createObjectURL(blob);
			var reader = new FileReader();
			reader.onloadend = function(){
				var base64data = reader.result;
						$('#uploaded_image').attr('src', data);

Now Run the project on http://localhost:8000/

Subscribe to My Programming YouTube Channel Before Downloading the code :

Download the Source Code : Image Cropping in Laravel

Read our More articles

Signup using REST API in Laravel

Email Validation in ReactJS

Send Email using PHP

Routing in Node.js

OTP countdown timer in react

Leave a Reply

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