使用 php 生成 Imagemagick 缩略图 - 使用 -crop

发布于 2024-08-09 14:06:38 字数 1644 浏览 4 评论 0原文

很久以前,我创建了一个小型库,用于通过 system(...) 使用 imagemagick 调整图像大小,因为我觉得 PHP 的内置 imagemagick 函数不够用。

然而,最近我不得不将其移植到 symfony 项目中,并且我选择使用 sfThumbnailPlugin (如果我没记错的话)。不幸的是,这不包括裁剪功能 - 即指定所需的尺寸,例如 300x300 像素,并裁剪缩略图以使其适合。我选择自己实现这个功能,但似乎有问题。

每当我将图像大小调整到所需尺寸时,当宽度大于高度时,比例就会发生变化。看一下这个示例:http://i37.tinypic.com/9hkqrl.png - 在此示例中,顶行是正确的比例,底行是问题。

在示例中,顶部和底部应该已被裁剪。

以下是完成裁剪部分的代码(变量名称应该是不言自明的):

<?php
    if ($width/$height > $this->maxWidth/$this->maxHeight) {
      // then resize to the new height...
                $command .= ' -resize "x'.$this->maxWidth.'"';

                // ... and get the middle part of the new image
                // what is the resized width?
                $resized_w = ($this->maxWidth/$height) * $width;

                // crop
                $command .= ' -crop "'.$this->maxHeight.'x'.$this->maxWidth.'+'.round(($resized_w - $this->maxWidth)/2).'+0"';
            } else {
                // or else resize to the new width
                $command .= ' -resize "'.$this->maxHeight.'x"';

                // ... and get the middle part of the new image
                // what is the resized height?
                $resized_h = ($this->maxHeight/$width) * $height;

                // crop
                $command .= ' -crop "'.$this->maxWidth.'x'.$this->maxHeight.
                             '+0+'.round(($resized_h - $this->maxHeight)/2).'" +repage';
            }

Is 是产生错误代码的 if 语句的第二部分。

谁能帮我纠正这个问题吗?显然计算是错误的。

Long ago I created a small library for resizing images using imagemagick through system(...) because I did not feel that the build-in imagemagick functions for PHP were sufficient.

However, recently I had to port this to a symfony project and I chose to use sfThumbnailPlugin (if I remember correctly). This unfortunately didn't include the crop functionality - i.e. to specify a desired size, e.g. 300x300 px and have the thumbnail cropped so that it fit. I chose to implement this functionality myself, but there seems to be something wrong.

Whenever I resize an image to a desired size there whe width is greater than the height, the proportions get screwed. Take a look at this example: http://i37.tinypic.com/9hkqrl.png - In this example the top row is the correct proportions and the bottom row is the problem.

In the example, the top and bottom should have been cropped.

Here is the code for the part where the crop is done (the variable names should be self-explanatory):

<?php
    if ($width/$height > $this->maxWidth/$this->maxHeight) {
      // then resize to the new height...
                $command .= ' -resize "x'.$this->maxWidth.'"';

                // ... and get the middle part of the new image
                // what is the resized width?
                $resized_w = ($this->maxWidth/$height) * $width;

                // crop
                $command .= ' -crop "'.$this->maxHeight.'x'.$this->maxWidth.'+'.round(($resized_w - $this->maxWidth)/2).'+0"';
            } else {
                // or else resize to the new width
                $command .= ' -resize "'.$this->maxHeight.'x"';

                // ... and get the middle part of the new image
                // what is the resized height?
                $resized_h = ($this->maxHeight/$width) * $height;

                // crop
                $command .= ' -crop "'.$this->maxWidth.'x'.$this->maxHeight.
                             '+0+'.round(($resized_h - $this->maxHeight)/2).'" +repage';
            }

Is is the second part of the if statement that produces the wrong code.

Can anyone correct this for me? Obviously the calculations are wrong.

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

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

发布评论

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

评论(2

趴在窗边数星星i 2024-08-16 14:06:38

解决方案如下:

    if ($width/$height > $this->maxWidth/$this->maxHeight) {
      // then resize to the new height...
                $command .= ' -resize "x'.$this->maxHeight.'"';

                // ... and get the middle part of the new image
                // what is the resized width?
                $resized_w = ($this->maxHeight/$height) * $width;

                // crop
                $command .= ' -crop "'.$this->maxWidth.'x'.$this->maxHeight.'+'.round(($resized_w - $this->maxWidth)/2).'+0" +repage';
            } else {
              $command .= ' -resize "' . $this->maxWidth . 'x"';
              $resized_h = ($this->maxWidth/$width) * $height;

                // crop
                $command .= ' -crop "'.$this->maxWidth.'x'.$this->maxHeight.
                             '+0+'.round(($resized_h - $this->maxHeight)/2).'" +repage';
            }

The solution was the following:

    if ($width/$height > $this->maxWidth/$this->maxHeight) {
      // then resize to the new height...
                $command .= ' -resize "x'.$this->maxHeight.'"';

                // ... and get the middle part of the new image
                // what is the resized width?
                $resized_w = ($this->maxHeight/$height) * $width;

                // crop
                $command .= ' -crop "'.$this->maxWidth.'x'.$this->maxHeight.'+'.round(($resized_w - $this->maxWidth)/2).'+0" +repage';
            } else {
              $command .= ' -resize "' . $this->maxWidth . 'x"';
              $resized_h = ($this->maxWidth/$width) * $height;

                // crop
                $command .= ' -crop "'.$this->maxWidth.'x'.$this->maxHeight.
                             '+0+'.round(($resized_h - $this->maxHeight)/2).'" +repage';
            }
燃情 2024-08-16 14:06:38

我告诉你一个建议,你可以尝试一下,

我看到了图像链接,我认为条件检查不正确。

您已检查条件[(宽度/高度)> (maxWidth/maxHeight)] 而不是检查

if(width == height) { }
elseif(宽度>高度) { }
else(width < height){ }

检查此条件并根据此条件裁剪图像并调整大小。

谢谢

I tell one suggestion you can try it,

I saw the Image link I think the conditions are not checked correctly.

you have checked condition [(width/height) > (maxWidth/maxHeight)] Instead of check

if(width == height) { }
elseif(width > height) { }
else(width < height){ }

Check this condition and according to this condition crop the image and resize.

Thank you

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