php调整图像大小zoomcrop无边框(逻辑)

发布于 2024-12-04 13:29:53 字数 506 浏览 0 评论 0原文

我有一个图像处理类,我想创建始终填充整个新宽度和高度的图像,因此没有边框/纯色背景,我只是无法理解如何使其始终填充高度和宽度(保持上传图像的纵横比)如果图像宽度和高度小于新图像尺寸..

就像phpthumb类中的zoomcrop(zc = 1)一样(我从中查看了代码,但我无法模仿行为)

    public function resizeCrop($newwidth, $newheight) {

                        ...

            $x = $this->getX();
            $y = $this->getY();

            ...

            else if ($x < $newwidth && $y < $newheight)
            {

            // logic ??

            }

    }

i have a image manipulation class, i want to create images that always fill the entire new width and height so there are no borders/solid color background, i just cant understand how i can make it always fill the height and width (mantaining the aspect ratio of the uploaded image) if the image width AND height are smaller than the new image size..

just like the zoomcrop (zc=1) from phpthumb class (i looked the code from it but i couldnt mimic the behavior)

    public function resizeCrop($newwidth, $newheight) {

                        ...

            $x = $this->getX();
            $y = $this->getY();

            ...

            else if ($x < $newwidth && $y < $newheight)
            {

            // logic ??

            }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风无影 2024-12-11 13:29:53

您需要确保较小的一侧(相对于原始图像和容器的纵横比)缩放到最大。看一下这段代码:

public function resizeCrop($newwidth, $newheight) {
                ...
    $x = $this->getX();
    $y = $this->getY();

    // old images width will fit
    if(($x / $y) < ($newwidth/$newheight)){
        $scale = $newwidth/$x;
        $newX = 0;
        $newY = - ($scale * $y - $newheight) / 2;

    // else old image's height will fit
    }else{
        $scale = $newheight/$y;
        $newX = - ($scale * $x - $newwidth) / 2;
        $newY = 0;
    }

    // new image
    $dest = imagecreatetruecolor($newwidth, $newheight);

    // now use imagecopyresampled
    imagecopyresampled($dest, $src, $newX, $newY, 0, 0, $scale * $x, $scale * $y, $x, $y);
    return $dest;
}

更新:更正了函数。它现在工作得很好,我已经在我的开发机器上进行了测试。

You need to make sure that the smaller (relative to the aspect ratio of the original image and that of container) side is zoomed to maximum. Have a look at this code:

public function resizeCrop($newwidth, $newheight) {
                ...
    $x = $this->getX();
    $y = $this->getY();

    // old images width will fit
    if(($x / $y) < ($newwidth/$newheight)){
        $scale = $newwidth/$x;
        $newX = 0;
        $newY = - ($scale * $y - $newheight) / 2;

    // else old image's height will fit
    }else{
        $scale = $newheight/$y;
        $newX = - ($scale * $x - $newwidth) / 2;
        $newY = 0;
    }

    // new image
    $dest = imagecreatetruecolor($newwidth, $newheight);

    // now use imagecopyresampled
    imagecopyresampled($dest, $src, $newX, $newY, 0, 0, $scale * $x, $scale * $y, $x, $y);
    return $dest;
}

Update: Corrected the function. It is now working perfectly, I have tested it on my dev machine.

潦草背影 2024-12-11 13:29:53

保持纵横比,您需要选择:要么使图像太大,然后裁剪原始图像,要么使其恰到好处,然后用纯色。

前者,您可以分别比较宽度和高度的百分比变化,然后使用较大的百分比作为两个尺寸的乘数。对于想要适合 2w x 3h 单位区域并保持纵横比的 1 x 1 单位图像:您可以使用 3x 乘数,获得 3 x 3 单位图像,并向左/右裁剪 0.5 个单位。

后者,您使用较小的百分比作为乘数。对于想要适合 2w x 3h 单位区域并保持纵横比的 1 x 1 单位图像:您可以使用 2x 乘数,获得 2 x 2 单位图像,并添加 0.5 单位的纯色顶部/底部。

To maintain aspect ratio, you need to choose: either make the image too big and then crop the original, or you make it fit just right and then fill in the parts that don't reach the edges with a solid color.

The former, you can compare the percentage change for width and height respectively, then use the larger percentage as a multiplier against both dimensions. For a 1 x 1 unit image that you want to fit into a 2w x 3h unit area and maintain aspect ratio: you can use a 3x multiplier, get a 3 x 3 unit image, and crop .5 units left/right.

The latter, you use the smaller percentage as a multiplier. For a 1 x 1 unit image that you want to fit into a 2w x 3h unit area and maintain aspect ratio: you can use a 2x multiplier, get a 2 x 2 unit image, and add .5 units of solid color top/bottom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文