gpt4 book ai didi

PHP 图像裁剪问题

转载 作者:可可西里 更新时间:2023-11-01 13:30:14 25 4
gpt4 key购买 nike

我有一种方法可以裁剪图像,但它会保持比例,有时会给我一个长方形图像。有没有办法在不扭曲或扭曲图像的情况下固定宽度和高度?即,120px x 120px

关于如何修改这个方法来做到这一点有什么想法吗?

注意: maxSize 设置为 120px。此外,我传递原始图像的宽度和高度。

protected function calculateSize($width, $height) {
if ($width <= $this->maxSize && $height <= $this->maxSize) {
$ratio = 1;
} elseif ($width > $height) {
$ratio = $this->maxSize / $width;
} else {
$ratio = $this->maxSize / $height;
}

$this->newWidth = round($width * $ratio);
$this->newHeight = round($height * $ratio);
}

最佳答案

假设您总是希望返回方形尺寸,并且放大不是一种选择,那么这样的事情应该可行(除非我遗漏了什么):

protected function calculateSize($width, $height) {
if ($height <= $this->maxSize && $width <= $this->maxSize) {
$new_height = $new_width = min(array($height, $width));
}
else {
if ($height > $width) {
$variable = 'width';
}
else {
$variable = 'height';
}
if ($$variable <= $this->maxSize) {
$new_height = $new_width = $$variable;
}
else {
$new_height = $new_width = min(array($this->maxSize, $$variable));
}
}
$this->newWidth = $new_width;
$this->newHeight = $new_height;
}

关于PHP 图像裁剪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31361425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com