gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:30:24 24 4
gpt4 key购买 nike

我正在尝试使用给定的 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 函数。这是手册的链接:imagecrop

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

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

关于php - 如何使用给定的 X、Y、宽度和高度坐标在 php 中裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27777505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com