如何在 php 中以给定的 X、Y、宽度和高度坐标裁剪图像


How can I crop the image in php with given coordinates of X,Y,Width and Height

我正在尝试使用给定的X,Y,宽度和高度坐标裁剪原始图像。但它没有正确裁剪图像。

这是我的代码

    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);

使用上面的代码,我能够裁剪图像,但它不会在给定的坐标中裁剪。

任何帮助都将是有用的。

你应该使用 imagecrop PHP 函数。这是手册的链接:图像裁剪

因此,在您的情况下,它看起来像这样:

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