调整图像大小并裁剪图像

发布于 2024-12-09 00:28:40 字数 232 浏览 2 评论 0原文

实际上我不知道是否有任何可用的 php 函数可以使用给定的参数(x1,y1,x2,y2,宽度,高度)和图像名称来裁剪和调整图像大小。

我看起来像下面的函数,它使用现有图像的给定参数创建新图像。

 newimg(x1,y1,x2,y2,width,height,image);

目前我已经使用 javascript 获得了上述所有参数,但现在我想根据上述参数裁剪图像。

Actually I don't know that there is available any php function to crop and re-size a image with given parameters(x1,y1,x2,y2,width,height) and image name.

I am looking like below function which create new image with given parameters from existing one.

 newimg(x1,y1,x2,y2,width,height,image);

Currently i have got all above parameters with javascript but now i want to crop image according to above parameters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘羡 2024-12-16 00:28:40

imagecopyresampled() 可以做到这一点:

imagecopyresampled() 将一幅图像的矩形部分复制到另一幅图像,平滑地插值像素值,尤其是在缩小图像尺寸的情况下仍能保持很高的清晰度。

换句话说,imagecopyresampled()将从src_image中获取宽度src_w和高度src_h的矩形区域> 位置 (src_x,src_y) 并将其放置在宽度为 dst_w 和高度为 dst_image 的矩形区域中dst_h 位于位置 (dst_x,dst_y)。

如果源坐标和目标坐标以及宽度和高度不同,则会对图像片段进行适当的拉伸或收缩。坐标指的是左上角。此函数可用于复制同一图像内的区域(如果 dst_image 与 src_image 相同),但如果区域重叠,结果将是不可预测的。


对于您的情况(未经测试):

function newimg($x1, $y1, $x2, $y2, $width, $height, $image) {
    $newimg = ... // Create new image of $width x $height
    imagecopyresampled(
        $newimg, // Destination
        $image, // Source
        0, // Destination, x
        0, // Destination, y
        $x1, // Source, x
        $y1, // Source, y
        $width, // Destination, width
        $height, // Destination, height
        $x2 - $x1, // Source, width
        $y2 - $y1 // Source, height
    );

    return $newimg;
}

imagecopyresampled() can do just that:

imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.

In other words, imagecopyresampled() will take an rectangular area from src_image of width src_w and height src_h at position (src_x,src_y) and place it in a rectangular area of dst_image of width dst_w and height dst_h at position (dst_x,dst_y).

If the source and destination coordinates and width and heights differ, appropriate stretching or shrinking of the image fragment will be performed. The coordinates refer to the upper left corner. This function can be used to copy regions within the same image (if dst_image is the same as src_image) but if the regions overlap the results will be unpredictable.


In your case (untested):

function newimg($x1, $y1, $x2, $y2, $width, $height, $image) {
    $newimg = ... // Create new image of $width x $height
    imagecopyresampled(
        $newimg, // Destination
        $image, // Source
        0, // Destination, x
        0, // Destination, y
        $x1, // Source, x
        $y1, // Source, y
        $width, // Destination, width
        $height, // Destination, height
        $x2 - $x1, // Source, width
        $y2 - $y1 // Source, height
    );

    return $newimg;
}
墨小墨 2024-12-16 00:28:40

PHP 的主要图像库是 GDImageMagick。两者都可以调整图像大小和裁剪图像。

The main image libraries for PHP are GD and ImageMagick. Both can resize and crop images.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文