質問

昔、私がビルド中ImageMagickの機能PHPのために十分であったと感じていなかったのでsystem(...)経由のImageMagickを使用して画像をリサイズのための小さなライブラリを作成します。

しかし、最近私はsymfonyのプロジェクトにポートにこれを持っていたし、私は(私の記憶が正しければ)sfThumbnailPluginを使用することにしました。これは、残念ながら、作物の機能が含まれていなかった - すなわち、例えば、所望のサイズを指定します300×300 pxとサムネイルを持っているが、それが収まるようにトリミング。私は自分自身をこの機能を実装することを選んだが、何か間違ったことをがあるようです。

幅が高さよりも大きいWHE Iが所望のサイズに画像のサイズを変更するたびに、

、比率はねじ込ま得ます。この例を見てみましょう: 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';
            }

間違ったコードを生成しますif文の第二部です。

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

役に立ちましたか?

解決

ソリューションは、次のようにしました

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

他のヒント

私は、

あなたはそれを試すことができます1つの提案を伝えます

私は条件が正しくチェックされないと思いますイメージ・リンクを見ます。

あなたがチェックした条件の [(幅/高さ)>(maxWidthに/のmaxHeight)] の代わりに、チェックの

の場合(幅==高さ){} ELSEIF(幅>高さ){} そうでなければ(幅<高さ){}

この状態をチェックして、この条件に応じた画像をトリミングし、サイズ変更します。

ありがとうございます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top