6

I am trying to crop the original image with given X, Y, Width and Height coordinates. But it does not crop the image properly.

Here is my code

    header('Content-type: image/jpeg');
    $source_x = $_POST['x'];
    $source_y = $_POST['y'];
    $width = $_POST['w'];
    $height = $_POST['h'];

    $dest = imagecreatetruecolor($width, $height);

    $src = imagecreatefromjpeg('path of the orignal Image');

    imagecopy($dest, $src, 30, 30, $source_x, $source_y, $width, $height);

    $cropped_image = "Path where to store the cropped image";

    imagejpeg($dest, $cropped_image, 100);

Using above code, I am able to crop the image, But it doesn't crop in given coordinate.

Any help will be useful.

Talk2Nit
  • 1,115
  • 3
  • 22
  • 38
  • http://stackoverflow.com/questions/6594089/calculating-image-size-ratio-for-resizing – Kiren S Jan 05 '15 at 10:30
  • @Kiren Siva I don't want to resize the image. I want to crop the part of image and save only that cropped image. – Talk2Nit Jan 05 '15 at 10:32

1 Answers1

1

You should use the imagecrop PHP function. Here is the link to the manual: imagecrop

So, in your case it would look like this:

$to_crop_array = array('x' =>$source_x , 'y' => $source_y, 'width' => $width, 'height'=> $height);
$dest = imagecrop($src, $to_crop_array);
onerror
  • 606
  • 5
  • 20