How to crop image in php with given coordinates X, Y, Width and Height

I am trying to crop the original image with the given coordinates of X, Y, Width and Height. 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 the code above, I can crop the image, but it does not crop at the given coordinate.

Any help would be helpful.

+6
source share
1 answer

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

So, in your case, it will look like this:

 $to_crop_array = array('x' =>$source_x , 'y' => $source_y, 'width' => $width, 'height'=> $height); $dest = imagecrop($src, $to_crop_array); 
+1
source

Source: https://habr.com/ru/post/980517/


All Articles