PHP Crop Image Before Upload using Cropper JS Tutorial

Apr 28, 2021 . Admin

Hi Dev,

In this tutorial, you will learn Php cropper js example. this example will help you Php crop image before upload. This tutorial will give you simple example of crop image using cropper.js Php. you will learn Php crop image before upload cropper.js. Let's see bellow example cropper js Php example.

Cropper.js is an easy to use JavaScript/jQuery plugin for image cropping with support of live previews and custom aspect ratio.this plugin provide features move,zoom,rotate,Fully responsive and mobile-friendly.

I will use model view in example. model display to upload image and crop image preview.follow bellow step example.

Example :


    <!DOCTYPE html>
        <title>PHP Crop Image Before Upload using Cropper JS</title>
        <meta name="_token" content="{{ csrf_token() }}">
        <script src="" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
        <link rel="stylesheet" href="" crossorigin="anonymous" />
        <script src="" integrity="sha256-WqU1JavFxSAMcLP2WIOI+GB2zWmShMI82mTpLDcqFUg=" crossorigin="anonymous"></script>
        <link rel="stylesheet"  href="" integrity="sha256-jKV9n9bkk/CTP8zbtEtnKaKf+ehRovOYeKoyfthwbC8=" crossorigin="anonymous" />
        <script src="" integrity="sha256-CgvH7sz3tHhkiVKh05kSUgG97YtzYNnWt6OXcmYzqHY=" crossorigin="anonymous"></script>
    <style type="text/css">
    img {
      display: block;
      max-width: 100%;
    .preview {
      overflow: hidden;
      width: 160px; 
      height: 160px;
      margin: 10px;
      border: 1px solid red;
      max-width: 1000px !important;
    <div class="container mt-5">
        <h2>PHP Crop Image Before Upload using Cropper JS -</h2>
        <form method="post">
        <div class="custom-file">
        <input type="file" class="custom-file-input image" id="customFile" name="image">
        <label class="custom-file-label" for="customFile">Choose Images</label>

    <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" id="modalLabel">PHP Crop Image Before Upload using Cropper JS -</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 id="image" src="">
                    <div class="col-md-4">
                        <div class="preview"></div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
            <button type="button" class="btn btn-primary" id="crop">Crop</button>


    // Add the following code if you want the name of the file appear on select
    $(".custom-file-input").on("change", function() {
      var fileName = $(this).val().split("\\").pop();

    var $modal = $('#modal');
    var image = document.getElementById('image');
    var cropper;
    $("body").on("change", ".image", function(e){
        var files =;
        var done = function (url) {
          image.src = url;
        var reader;
        var file;
        var url;

        if (files && files.length > 0) {
          file = files[0];

          if (URL) {
          } else if (FileReader) {
            reader = new FileReader();
            reader.onload = function (e) {

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

        canvas = cropper.getCroppedCanvas({
          width: 160,
          height: 160,

        canvas.toBlob(function(blob) {
            url = URL.createObjectURL(blob);
            var reader = new FileReader();
             reader.onloadend = function() {
                var base64data = reader.result;  
                    type: "POST",
                    dataType: "json",
                    url: "upload.php",
                    data: {image: base64data},
                    success: function(data){
                        alert("success upload image");




    $folderPath = 'uploads/';

    $image_parts = explode(";base64,", $_POST['image']);
    $image_type_aux = explode("image/", $image_parts[0]);
    $image_type = $image_type_aux[1];
    $image_base64 = base64_decode($image_parts[1]);
    $file = $folderPath . uniqid() . '.png';

    file_put_contents($file, $image_base64);

    echo json_encode(["image uploaded successfully."]);


You will create to upload folder in root directory

I Hope It will help you..