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 Pakainfo.com – 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.













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

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

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.

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){
    e.preventDefault();
    
     jQuery('#changePic').show();
     jQuery('#change-pic').hide();
    
  });

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() 
{ 
jQuery("#liveData_prev-avatar-dashboard").html('');
jQuery("#liveData_prev-avatar-dashboard").html('Uploading....');
jQuery("#cropimage").ajaxForm(
{
target: '#liveData_prev-avatar-dashboard',
success:    function() { 
        jQuery('img#photo').imgAreaSelect({
        aspectRatio: '1:1',
        onSelectEnd: getSizes,
    });
    }
}).submit();
});

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

  //call on crop iamge button
jQuery('#btn-crop').live('click', function(e){
  e.preventDefault();
  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(),
      thumb_height:jQuery('#live-thumb-height').val()
        };
    saveCropImage(all_params);
  });
function saveCropImage(all_params) {
    jQuery.ajax({
        url: siteurl + all_params['targetUrl'],
        cache: false,
        dataType: "html",
        data: {
            action: all_params['action'],
            id: jQuery('#new-dashboard-id').val(),
       t: 'ajax',
                w1:all_params['thumb_width'],
                x1:all_params['x_axis'],
                h1:all_params['thumb_height'],
                y1:all_params['y_axis'],
                x2:all_params['x2_axis'],
                y2:all_params['y2_axis']
        },
        type: 'Post',
       // async:false,
        success: function (response) {
        jQuery('#changePic').hide();
          jQuery('#change-pic').show();
        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);
        jQuery("#liveData_prev-avatar-dashboard").html('');
        jQuery("#live_imgname").val('');
        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'];
    if(strlen($name))
    {
    list($txt, $ext) = explode(".", $name);
    if(in_array($ext,$check_formats))
    {
    if($sizegetWidth($live_path);
      $height = $this->getHeight($live_path);
      if ($width > $max_width){
        $live_scale = $max_width/$width;
        $uploaded = $this->resizeImage($live_path,$width,$height,$live_scale);
      }else{
        $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 "";
    }
    else
    echo "sorry failed";
    }
    else
    echo "Image your file size max 1 MB"; 
    }
    else
    echo "Invalid your file format.."; 
    }
    else
    echo "Please select any format some image..!";
    exit;
    
    
  }

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

  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

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(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Comment