문제

오래 전에 나는 imagemagick을 사용하여 이미지를 크기를 조정하기위한 작은 라이브러리를 만들었습니다. system(...) PHP의 imagemagick 기능이 충분하다고 생각하지 않았기 때문입니다.

그러나 최근에 나는 이것을 Symfony 프로젝트에 포트해야했고 sfthumbnailplugin을 사용하기로 결정했습니다 (정확하게 기억한다면). 불행히도 여기에는 작물 기능 (즉, 원하는 크기)을 지정하기위한 작물 기능 (예 : 300x300 px)이 포함되지 않았으며 썸네일을 자르려면 적합합니다. 나는이 기능을 직접 구현하기로 결정했지만 뭔가 잘못된 것 같습니다.

이미지를 원하는 크기로 크기를 조정할 때마다 너비가 높이보다 크면 비율이 나사로 나옵니다. 이 예를 살펴보십시오. 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 문의 두 번째 부분입니다.

누구든지 나를 위해 이것을 수정할 수 있습니까? 분명히 계산이 잘못되었습니다.

도움이 되었습니까?

해결책

해결책은 다음과 같습니다.

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

다른 팁

나는 당신이 그것을 시도 할 수있는 한 가지 제안을한다.

이미지 링크를 보았습니다. 조건이 올바르게 확인되지 않았다고 생각합니다.

상태를 확인했습니다 (너비/높이)> (MaxWidth/MaxHeight) 점검 대신

if (너비 == 높이) {} elseif (너비> 높이) {} else (너비 <높이) {}

이 조건을 확인 하고이 조건에 따라 이미지를 자르고 크기를 조정하십시오.

고맙습니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top