Image Crop and Upload using JQuery with PHP Ajax

Image Crop and Upload using JQuery with PHP Ajax


Welcome folks I am back with another blog post. In this post we will be Doing Image Crop and Upload using jQuery with PHP and AJAX. Image Crop is the most important part in every modern web application. In this post we will be implementing this feature in the following steps.


Make a new index.html file and copy paste the below code. In this code we have just a Image upload button.

READ  BootBoxJS - Bootstrap Modals Library For Creating Beautiful Bootstrap Modals


<!DOCTYPE html>
<title>Image Crop</title>
<link rel="stylesheet" href="croppie.css"/>
   <div class="container">
      <h3 align="center">Image Crop and Upload using Jquery with Php and Ajax</h3>
      <p style="text-align:center;">Select Profile Image</p>
     <input type="file" name="upload_image" id="upload_image" accept="image/*">
      <div id="uploadimage">
      <button class="crop_image">Crop and Upload Image</button>
      <div id="uploaded_image"></div>
<script src=""></script>
<script src="croppie.min.js"></script>
<script src="script.js"></script>


For this we are using the croppie library for cropping images so download this library from here

READ  Image Processing in Node.js Using Jimp Library - Coding Shiksha


Make a new script.js file for the javascript




    $image_crop = $('#uploadimage').croppie({
       enableExif: true,
       viewport: {
         type:'square' //circle
     $('#upload_image').on('change', function(){
       var reader = new FileReader();
       reader.onload = function (event) {
         $image_crop.croppie('bind', {
           console.log('jQuery bind complete');
       $image_crop.croppie('result', {
         type: 'canvas',
         size: 'viewport'
           type: "POST",
           data:{"image": response},


READ  What is the Difference Between Node and Nodemon

Make a new upload.php file. This php script will actually take the cropped image and upload it to the server.




 $data = $_POST["image"];

 $image_array_1 = explode(";", $data);

 $image_array_2 = explode(",", $image_array_1[1]);

 $data = base64_decode($image_array_2[1]);

 $imageName = time() . '.png';

 file_put_contents($imageName, $data);

 echo '<img src="'.$imageName.'" class="img-thumbnail" />';


Leave a Reply

Close Menu

Indian Desi Tools - Ultimate Tools Website

Demo Description

This will close in 20 seconds