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

6

我正在尝试使用给定的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);

使用以上代码,我能够裁剪图像,但它没有在给定坐标处裁剪。任何帮助都将是有用的。

https://dev59.com/dWw15IYBdhLWcg3wd7lj - Kiren S
@Kiren Siva 我不想调整图片大小。我想裁剪图片的一部分并仅保存那个被裁剪的图像。 - Talk2Nit
1个回答

1

你应该使用 imagecrop PHP函数。这是手册链接: imagecrop

所以,在你的情况下,它应该是这样的:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接