Image Upload,Crop and Resize Using PHP, jQuery and Ajax

Image Upload,Crop and Resize Using PHP, jQuery and Ajax

In this Post We Will Explain About is Image Upload,Crop and Resize Using PHP, jQuery and Ajax With Example and Demo.Welcome on – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to crop image while uploading with jquery Example

In this post we will show you Best way to implement image cropping in php and jquery, hear for crop resize and save image php jquery with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

crop and save image using jquery and php

In this Example,First of all Add or Inluce External Libs Like as a(jQuery, css etc..), and then create a simple index.php or index.html page.After that crate a simple javascript file like as a index.js or main.js, It is also add your web-application First Header Part to some priorty set.After that Include your relavant CSS Class.

Phase 1: Firstof all we shall include all some libs Like as a necessary jquery some plugin and library files.and then We shall keep all some belowcreate files in head section of like as index.php file.

Also Read This πŸ‘‰   read more read less jquery - How to Create Read More Read Less Content Using jQuery?

<! include libs-->

<! include libs-->

<! include libs-->

<! include libs-->

<! include libs-->

Phase 2:Here,We shall Make simple HTML div to show image and some option to change new image.

<div class="col-sm-2"><img class="img-circle" id="live-updateimg" height="128" style="width: 140px;height: 140px" src="custom.jpg" /></div>
        <div class="col-sm-10"><a class="btn btn-primary" id="change-pic">Change Image</a>

Here We Are showing some custom.jpg image file as well as for as a some custom image.

Phase 3:And then Now we shall Make HTML div to upload and simple crop image.

<div id="changePic" class="">
                  <form id="cropimage" method="post" enctype="multipart/form-data" action="dashboard.php">
            <label>Upload your image</label>
            <div id='liveData_prev-avatar-dashboard'>
          <div id="thumbs" style="padding:5px;width:600p"></div>
              <div class="modal-footer">
                  <button type="button" class="btn btn-custom">Close</button>
                  <button type="button" id="btn-crop" class="btn btn-primary">Crop & Save</button>

Phase 4: And then will write some source jquery code to show the simple steps to Image Crop div on click of data change pic some button.

jQuery('#change-pic').live('click', function(e){

Phase 5: And then we shall use some jQuery Ajax form some new plugin for HTML form submitting as well as show the simple image for some crop process on simple bootstrap modal box.

jQuery('#live_imgname').live('change', function() 
target: '#liveData_prev-avatar-dashboard',
success:    function() { 
        aspectRatio: '1:1',
        onSelectEnd: getSizes,

Phase 6: And then we shall cropped image and some call save data Ajax method to save image on some hard disk storage.

Also Read This πŸ‘‰   PHP Laravel 5.6 Dynamically Add Active class

  //call on crop iamge button
jQuery('#btn-crop').live('click', function(e){
  all_params = {
            targetUrl: 'profiles/saveAvatarTmp/',
            action: 'profiles_controller_saveAvatarTmp',
      x_axis: jQuery('#first-axis').val(),
      y_axis : jQuery('#second-axis').val(),
      x2_axis: jQuery('#live-x2-axis').val(),
      y2_axis : jQuery('#live-y2-axis').val(),
      thumb_width : jQuery('#live-thumb-width').val(),
function saveCropImage(all_params) {
        url: siteurl + all_params['targetUrl'],
        cache: false,
        dataType: "html",
        data: {
            action: all_params['action'],
            id: jQuery('#new-dashboard-id').val(),
       t: 'ajax',
        type: 'Post',
       // async:false,
        success: function (response) {
        jQuery(".live-imgselect-border1,.live-imgselect-border2,.live-imgselect-border3,.live-imgselect-border4,.live-imgselect-border2,.live-imgselect-outer").css('display', 'none');
        jQuery("#live-updateimg").attr('src', response);
        AlertManager.showNotification('Live Image cropped!', 'Click Save some Profile button to save image.', 'success');
        error: function (xhr, ajaxOptions, thrownError) {
            alert('status Code:' + xhr.status + 'Error Message :' + thrownError);

Phase 7: We Shall define some controller/action method in dashboard.php.

  public function changeAvatar() {
    global $current_user;
    $loggedInId = $current_user->ID;
    $post = isset($_POST) ? $_POST: array();
    $max_width = "500"; 
    $userId = isset($post['new-dashboard-id']) ? intval($post['new-dashboard-id']) : 0;
    $path = get_theme_root(). '\images\uploads\tmp';

    $check_formats = array("jpg", "png", "gif", "bmp","jpeg");
    $name = $_FILES['live_imgname']['name'];
    $size = $_FILES['live_imgname']['size'];
    list($txt, $ext) = explode(".", $name);
      $height = $this->getHeight($live_path);
      if ($width > $max_width){
        $live_scale = $max_width/$width;
        $uploaded = $this->resizeImage($live_path,$width,$height,$live_scale);
        $live_scale = 1;
        $uploaded = $this->resizeImage($live_path,$width,$height,$live_scale);
    $res = $this->Profile->saveAvatar(array(
            'userId' => isset($userId) ? intval($userId) : 0,
                        'avatar' => isset($live_actual_imgname) ? $live_actual_imgname : '',
    //mysql_query("UPDATE users SET profile_image='$live_actual_imgname' WHERE uid='$session_id'");
    echo "<img id='photo' class='' />";
    echo "sorry failed";
    echo "Image your file size max 1 MB"; 
    echo "Invalid your file format.."; 
    echo "Please select any format some image..!";

Phase 8:Simple Model some method to save data image path into mysql database.

Also Read This πŸ‘‰   How to install localhost/codeigniter/ with XAMPP

  function saveAvatar($options){
      //some source code update sql


You are Most welcome in my youtube Channel Please shubscibe my channel. and give me feedBackMore Details……
Angularjs Example


I hope you have Got What is crop image while uploading php And how it works.I would Like to have FeaeBack From My Blog( readers.Your Valuable FeedBack,Any Question,or any Comments abaout This Article( Are Most Always Welcome.