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

标签 php gd php-gd

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

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,a.r.是 1.25:最大裁剪为 625x500。图像为 100x110,最大裁剪为:80x110。

最佳答案

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

$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

关于php - 给定图像和纵横比的最大裁剪区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8541380/

相关文章:

php - Gd 已安装,但 "Call to undefined function imagecreatefromjpeg()"

php - 为什么我不能在用 php 旋转后使 png 的背景透明?

php - 为什么修复 GD 的 png 透明度问题的解决方案不起作用?

php - 通过php连接到数据库后无法显示存储在MySQL数据库中的图像

ajax - 通过ajax请求将字符串发送到php gd

php - 如何在 PHP 中获取本地系统时间?

php - 如何在我的开发机器上支持 "AddType x-mapp-php5 .php"

PHP 将索引数组导出到 CSV

php - 使用 cookie 登录 iOS Web 服务

php - imagecreatefromwebp() -> imagejpeg() 导致蓝色 channel 缺失