0

system(...)ずっと前に、PHPの組み込みのimagemagick関数で十分だとは思わなかったため、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ステートメントの2番目の部分です。

誰かが私のためにこれを修正できますか?明らかに計算は間違っています。

4

2 に答える 2

2

解決策は次のとおりです。

    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';
            }
于 2009-11-06T02:09:05.670 に答える