小编Hou*_*nar的帖子

PHP图像裁剪问题

我有一种方法可以裁剪图像,但它保持比例,有时给我一个椭圆形的图像.有没有一种方法可以固定宽度和高度,同时不会使图像偏斜或变暗?即,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);
}
Run Code Online (Sandbox Code Playgroud)

php image crop

6
推荐指数
1
解决办法
159
查看次数

标签 统计

crop ×1

image ×1

php ×1