使用php生成Imagemagick缩略图 - 使用-crop [英] Imagemagick thumbnail generation with php - using -crop

查看:147
本文介绍了使用php生成Imagemagick缩略图 - 使用-crop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

然而,最近我不得不把它移植到symfony项目中,我选择使用sfThumbnailPlugin(如果我没记错的话)。遗憾的是,这不包括作物功能 - 即指定所需的大小,例如, 300x300像素并缩放缩略图使其适合。我自己选择实现这个功能,但似乎有些不对劲。

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.

每当我将图像调整到所需大小时,宽度大于高度,比例搞砸了。请看一下这个例子: http://i37.tinypic.com/9hkqrl.png - 在此示例中,顶行是正确的比例,底行是问题。

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';
            }

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

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.

推荐答案

解决方案如下:

    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';
            }

这篇关于使用php生成Imagemagick缩略图 - 使用-crop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆