给定图像和纵横比的最大裁剪区域 [英] Max crop area for a given image and aspect ratio

查看:146
本文介绍了给定图像和纵横比的最大裁剪区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何PHP / GD函数可以计算:

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).

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

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

推荐答案

没有函数计算这个,因为它是基本数学:

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.

这篇关于给定图像和纵横比的最大裁剪区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆