【问题标题】:How can I crop the image in php with given coordinates of X,Y,Width and Height如何使用给定的 X、Y、宽度和高度坐标在 php 中裁剪图像
【发布时间】:2015-03-02 20:54:43
【问题描述】:

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

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

任何帮助都会很有用。

【问题讨论】:

标签: php image crop


【解决方案1】:

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

所以,在你的情况下,它看起来像这样:

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

【讨论】:

    猜你喜欢
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2014-06-01
    • 2020-12-28
    • 1970-01-01
    相关资源
    最近更新 更多