Skip to main content

Image Crop and Upload using jQuery and PHP

Image upload and Crop is an important feature of web applications to allow users to crop the image to desired size before upload. This feature is generally implemented in user’s section to manage user profile to upload user avatar or profile photo after image crop to fit correctly to display in user profile. The functionality can be implement any where with image upload in web applications

As in our previous tutorial you have learned how to upload images with jQuery, PHP and MySQL. In this tutorial you will learn how to crop image before upload using jQuery and PHP.

We will use Croppie jQuery plugin that enables to crop the image with zoom.

We will cover this tutorial step by step with live demp to browse ithe mage and display to crop with zoom feature and upload after image crop.

crop image and upload with php and jquery

Also, read:

So let’s start the coding for image crop before upload example using jQuery and PHP. We will have following file structure for this example.

  • index.php
  • upload.php
  • upload.js

Step1: Include Bootstrap and jQuery Croppie plugin

As we are going to handle design using Bootstrap and jQuery Croppie plugin, so we will include related files in index.php. We will also include upload.js in which handle plugin functions to crop image and save on server.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="croppie/croppie.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="croppie/croppie.css">
<script type="text/javascript" src="upload.js"></script>
<link rel="stylesheet" href="style.css">

Step2: Create Image Upload and Preview HTML

In index.php, we will create HTML for image upload and image preview.

<div class="container">
	<h2>Crop Image and Upload using jQuery and PHP</h2>	
	<div class="panel panel-default">
	  <div class="panel-body">
	  	<div class="row">
	  		<div class="col-md-4 text-center">
				<div id="upload-image"></div>
	  		</div>
	  		<div class="col-md-4">
				<strong>Select Image:</strong>
				<br/>
				<input type="file" id="images">
				<br/>
				<button class="btn btn-success cropped_image">Upload Image</button>
	  		</div>			
	  		<div class="col-md-4 crop_preview">
				<div id="upload-image-i"></div>
	  		</div>
	  	</div>
	  </div>
	</div>			
</div>

Step3: Implement Image Crop and Upload

Now in upload.js, we will handle image crop functionality using croppie plugin function.

$image_crop = $('#upload-image').croppie({
	enableExif: true,
	viewport: {
		width: 200,
		height: 200,
		type: 'square'
	},
	boundary: {
		width: 300,
		height: 300
	}
});
$('#images').on('change', function () { 
	var reader = new FileReader();
	reader.onload = function (e) {
		$image_crop.croppie('bind', {
			url: e.target.result
		}).then(function(){
			console.log('jQuery bind complete');
		});			
	}
	reader.readAsDataURL(this.files[0]);
});

After image crop, we will upload cropped image to server by calling upload.php when click on upload image.

$('.cropped_image').on('click', function (ev) {
	$image_crop.croppie('result', {
		type: 'canvas',
		size: 'viewport'
	}).then(function (response) {
		$.ajax({
			url: "http://localhost/coderszine/crop-image-and-upload-using-jquery-and-php/upload.php",
			type: "POST",
			data: {"image":response},
			success: function (data) {
				html = '<img src="' + response + '" />';
				$("#upload-image-i").html(html);
			}
		});
	});
});	

Step4: Upload Image on Server After Crop

Finally in upload.php, we will implement functionality to upload cropped image on server.

<?php
$croped_image = $_POST['image'];
list($type, $croped_image) = explode(';', $croped_image);
list(, $croped_image)      = explode(',', $croped_image);
$croped_image = base64_decode($croped_image);
$image_name = time().'.png';
// upload cropped image to server 
file_put_contents('upload/'.$image_name, $croped_image);
echo 'Cropped image uploaded successfully.';
?>

You may also like:

You can view the live demo from the Demo link and can download the source from the Download link below.
Demo Download

21 thoughts on “Image Crop and Upload using jQuery and PHP

  1. we need to save original image also with cropped image both images need to save while upload image please answer to this question and image format by default saved with png how can we save as a jpg

  2. You just need to unlock download link available next to Demo link. Thanks!

  3. You need to implement to get cropped image details and insert into database.

  4. You need handle to save original file and also cropped image. Also currently it is saving PNG default image, you need to check uploaded image and save accordingly. Thanks!

  5. Hello,
    The message “Cropped image uploaded successfully” and the uploaded image don’y appear. Div is grey. What to do ?

  6. I’m needing the cropped image to be 200×400 instead of the 200×200.. I’ve changed the settings every place I see an option, viewport calls, upload .js, css, crappie.js and its still defaulting to 200×200… Any ideas?

  7. yes, viewport should work to change default. I am checking this and update you. thanks!

  8. Hi, I have your code working. Thank you for sharing! Can you help with some customization? I need to save the image as a jpg and a bmp file but the bmp file dimensions need to be smaller than the jpg. The aspect ratio is the same. Right now I am able to save both formats but can’t figure out how to change the dimensions of the bmp. Any suggestions?

  9. I think you can add check for bmp file to set specific ratio for this, thanks!

  10. You need to make changes to browse multiple files and also need to do changes in JavaScript and PHP file to handle for multiple images. thanks!

  11. but if i upload a .JPG i’d like get back a cropped .JPG not another extension
    and how can i crop and replace an existing image?

Comments are closed.