给定图像和纵横比的最大裁剪区域

发布于 2024-12-21 15:50:15 字数 235 浏览 5 评论 0 原文

有没有 PHP/GD 函数可以计算这个:

输入:图像宽度、图像高度和要遵循的纵横比。输出: 尊重给定纵横比的最大居中裁剪的宽度/高度 (尽管图像原始长宽比)。

示例:图像为 1000x500,ar 为 1.25:最大裁剪为 625x500。图像为 100x110,最大裁剪为:80x110。

Is there any PHP/GD function that can calculate this:

Input: image width, image height and an aspect ratio to honor. Output:
the width/height of the max centered crop that respects the given aspect ratio
(despite image original aspect ratio).

Example: image is 1000x500, a.r. is 1.25: max crop is 625x500. image is 100x110, max crop is: 80x110.

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

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

发布评论

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

评论(2

清引 2024-12-28 15:50:15

没有函数可以计算此值,因为它是初等数学:

$imageWidth = 1000;
$imageHeight = 500;
$ar = 1.25;

if ($ar < 1) { // "tall" crop
    $cropWidth = min($imageHeight * $ar, $imageWidth);
    $cropHeight = $cropWidth / $ar;
}
else { // "wide" or square crop
    $cropHeight = min($imageWidth / $ar, $imageHeight);
    $cropWidth = $cropHeight * $ar;
}

查看实际操作

There is no function that calculates this because it's elementary math:

$imageWidth = 1000;
$imageHeight = 500;
$ar = 1.25;

if ($ar < 1) { // "tall" crop
    $cropWidth = min($imageHeight * $ar, $imageWidth);
    $cropHeight = $cropWidth / $ar;
}
else { // "wide" or square crop
    $cropHeight = min($imageWidth / $ar, $imageHeight);
    $cropWidth = $cropHeight * $ar;
}

See it in action.

终难愈 2024-12-28 15:50:15

作为 @Jon答案,这里是 PHP-GD 库

/**
 * Crops image by taking largest area rectangle from center of image so that the desired aspect ratio is realized.
 * @param resource $src_image image resource to be cropped
 * @param float $required_aspect_ratio Desired aspect ratio to be achieved via cropping
 * @return resource cropped image
 */
public function withCenterCrop($src_image, float $required_aspect_ratio) {
    $crr_width = imagesx($src_image);
    $crr_height = imagesy($src_image);
    $crr_aspect_ratio = $crr_width / $crr_height;

    $cropped_image = null;
    if ($crr_aspect_ratio < $required_aspect_ratio) {
        // current image is 'taller' (than what we need), it must be trimmed off from top & bottom
        $new_width = $crr_width;
        $new_height = $new_width / $required_aspect_ratio;
        // calculate the value of 'y' so that central portion of image is cropped
        $crop_y = (int) (($crr_height - $new_height) / 2);
        $cropped_image = imagecrop(
            $src_image,
            ['x' => 0, 'y' => $crop_y, 'width' => $new_width, 'height' => $new_height]
        );
    } else {
        // current image is 'wider' (than what we need), it must be trimmed off from sides
        $new_height = $crr_height;
        $new_width = $new_height * $required_aspect_ratio;
        // calculate the value of 'x' so that central portion of image is cropped
        $crop_x = (int) (($crr_width - $new_width) / 2);
        $cropped_image = imagecrop(
            $src_image,
            ['x' => $crop_x, 'y' => 0, 'width' => $new_width, 'height' => $new_height]
        );
    }

    return $cropped_image;
}

As an extension to @Jon's answer, here's an implementation of this approach in PHP-GD library

/**
 * Crops image by taking largest area rectangle from center of image so that the desired aspect ratio is realized.
 * @param resource $src_image image resource to be cropped
 * @param float $required_aspect_ratio Desired aspect ratio to be achieved via cropping
 * @return resource cropped image
 */
public function withCenterCrop($src_image, float $required_aspect_ratio) {
    $crr_width = imagesx($src_image);
    $crr_height = imagesy($src_image);
    $crr_aspect_ratio = $crr_width / $crr_height;

    $cropped_image = null;
    if ($crr_aspect_ratio < $required_aspect_ratio) {
        // current image is 'taller' (than what we need), it must be trimmed off from top & bottom
        $new_width = $crr_width;
        $new_height = $new_width / $required_aspect_ratio;
        // calculate the value of 'y' so that central portion of image is cropped
        $crop_y = (int) (($crr_height - $new_height) / 2);
        $cropped_image = imagecrop(
            $src_image,
            ['x' => 0, 'y' => $crop_y, 'width' => $new_width, 'height' => $new_height]
        );
    } else {
        // current image is 'wider' (than what we need), it must be trimmed off from sides
        $new_height = $crr_height;
        $new_width = $new_height * $required_aspect_ratio;
        // calculate the value of 'x' so that central portion of image is cropped
        $crop_x = (int) (($crr_width - $new_width) / 2);
        $cropped_image = imagecrop(
            $src_image,
            ['x' => $crop_x, 'y' => 0, 'width' => $new_width, 'height' => $new_height]
        );
    }

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