[英]PHP Image Crop Issue
I have a method that will crop an image but it maintains ratio and sometimes gives me an oblong image. 我有一种方法可以裁剪图像,但它保持比例,有时给我一个椭圆形的图像。 Is there a way have it fixed width and height while not skewing or distoring the image?
有没有一种方法可以固定宽度和高度,同时不会使图像偏斜或变暗? ie.,
120px x 120px
? 即,
120px x 120px
?
Any ideas on how to modify this method to do that? 有关如何修改此方法的任何想法吗?
Note: maxSize is set to 120px
. 注意: maxSize设置为
120px
。 Additionally, I'm passing in the width and height from the original image. 另外,我从原始图像传递宽度和高度。
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);
}
Assuming you always want square dimensions returned, and that upscaling is not an option, something like this should work (unless I'm missing something): 假设你总是想要返回方形尺寸,并且升级不是一个选项,这样的东西应该有效(除非我遗漏了一些东西):
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.