So this is the original picture including the rectangle, I want to create a cropped Image from
And this is what I get after the cropping
So it can be seen, that the new image has the right dimensions but the wrong part is being cropped. Here's the JS:
$(document).ready(function()
{
$('#cropimage').Jcrop(
{
aspectRatio: 3 / 4,
maxSize: [150,200],
onSelect: updateCoords
});
});
function updateCoords(c)
{
$('#x').val(c.x);
$('#y').val(c.y);
$('#w').val(c.w);
$('#h').val(c.h);
};
And here is the PHP code
function crop($_POST)
{
$clipX = (int)$_POST['x'];
$clipY = (int)$_POST['y'];
$filename = (string)$_POST['image'];
$resizedHeight = (int)$_POST['h'];
$resizedWidth = (int)$_POST['w'];
// Original image's details
$original = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'images/user_pictures/' . DIRECTORY_SEPARATOR . $filename;
$dimensions = getimagesize($original);
$old_width = $dimensions[0];
$old_height = $dimensions[1];
// image = original_image
$old_image = call_user_func('imagecreatefrom' . 'jpeg', $original);
// Crop image
if (function_exists('imagecreatetruecolor') && ($new_image = imagecreatetruecolor($resizedWidth, $resizedHeight)))
imagecopyresampled($new_image, $old_image, 0, 0, $clipX, $clipY, $resizedWidth, $resizedHeight, $old_width, $old_height);
imagejpeg($new_image,'images/user_pictures/'.$this->getUserID().'_picture.jpg');
}
I never used those php functions before but I've read some tutorials and I dont see any errors there. But there must be at least 1 ... what am I doing wrong ? The while original Image seems to be resized for whatever reason.
if you want to crop based on (x,y,w,h) (10,15,30,35) then your function would be:
imagecopyresampled ( $dst_image , $src_image , 0, 0 , 10 , 15 , 30-10 , 35-15 , 30-10 , 35-15 )
since you are copying 20x20 from the original into a new image, those dimensions are your new and dst_w, dst_h as well as your src_w, src_h.
$old_width and $old_height are now the complete width of the original image while they should be the width and height of the cropped part.
$old_width = $resizedWidth;
$old_height = $resizedHeight;