Hi I am trying to crop image using codeigniter, jQuery(ui.js). Although the image is being cropped but show black image after cropping. I have made a give called crop_div the div actually has some border and width height. What i am doing is that i am accessing the coordinates of this div using jquery sending them to the php, calling the previous uploaded image and getting the same coordinates from the uploaded image. Here is the code
JS Code
function do_crop(method_url) {
jQuery('#do_crop').on('submit', (function(e) {
e.preventDefault();
var posi = document.getElementById('crop_div');
var top = document.getElementById("top").value=posi.offsetTop;
var left = document.getElementById("left").value=posi.offsetLeft;
var right = document.getElementById("right").value=posi.offsetWidth;
var bottom = document.getElementById("bottom").value=posi.offsetHeight;
jQuery.ajax({
url: method_url,
type: 'POST',
data: { 'top': top, 'left': left, 'bottom': bottom, 'right': right },
success: function(data) {
jQuery('.result').show();
if(data == 'Profile uploaded.') {
window.location.href = reload;
jQuery('#PicprofileBtn').html('Submit');
}
else {
alert(data);
}
}
});
}));
}
PHP Code
public function do_crop() {
$y1 = $this -> input -> post('top');
$x1 = $this -> input -> post('left');
$w = $this -> input -> post('right');
$h = $this -> input -> post('bottom');
$image = "Users_Data/profile/8ca760ccc0bf60566fdd4ccc2cd213e8.jpg";
list($width,$height ) = getimagesize( $image );
$newwidth = 600;
$newheight = 400;
$thumb = imagecreatetruecolor( $newwidth, $newheight );
$source = imagecreatefromjpeg($image);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,$image,100);
$im = imagecreatefromjpeg($image);
$dest = imagecreatetruecolor($w,$h);
imagecopyresampled($dest,$im,0,0,$x1,$y1,$w,$h,$w,$h);
imagejpeg($dest,"Users_Data/thumb/crop_image.jpg", 100);
}