2009-12-15 4 views
2

누군가가 PHP로 작성된 이미지 크기 조정 기능을 지원하지만 PHPThumb과 같은 방식으로 이미지의 크기를 조정해야하는지 궁금합니다. 따라서 새 이미지의 너비와 높이를 설정하면이 함수는 업로드 된 이미지 (그리고 mantain 종횡비)를 새 너비와 높이에 맞춰야합니다.PHP에서 이미지 크기를 조정하는 현명한 방법

도움을 주시면 감사하겠습니다.

감사합니다.

+0

글쎄, 검색은 분명히 도움이 될 것입니다. '관련'참조 –

답변

0

GD 또는 ImageMagick을 사용합니다. 두 가지 모두 훌륭한 라이브러리가 있습니다.

0

적어도이 작은 (유용한)입니다 : 내가 OOP를 배우기 시작했을 때

+0

감사합니다. 제가 확인하겠습니다. – Psyche

3

http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php, 난 나 자신을 연습 this image class을 wroted.

GD를 사용하면 확실히 향상시킬 수 있으며, 도움이 되길 바랍니다.

편집 : 아무런 통제력이없는 문제에 대해 2 번 downvotes 이후, 나는 파스 티에 수업을 씁니다. The Example

기억

The class

클래스는 잘 작동하지만, 기본 OOP를 배우기 위해 연습했다. 원본 이미지의 가로 세로 비율을 계산 : 나는이, 당신이 게으른 사용자 확인이되기를 바랍니다

크리스마스에)

우리는 더 나은 사람

+1

LOL 저는 믿을 수 없습니다, 당신이 로그인을 요구하는 사람은 아닙니다, 그건 내 잘못이 아니라 phpclasses.org의 작동 방식입니다. 그 사이트를 본 적이 없습니까? – Strae

+0

그리고 내가 왜 그렇게 나쁜 평판을 가지고 있는지 물어볼 수 있습니까? – Strae

+0

hahahah 감사합니다,하지만 지금은 꽤 phpclasses가 왜 빨아 .. 궁금해 꽤 궁금해 .. 단지 몇 시간을 사용하지만, 나쁘진 보이지 않는;) 나는 그것을 위해 구글거야! – Strae

2
  • 크기 조정 후 작물 접근 방식은 대상 이미지 (폭 또는 높이)보다 더 커지는 중간 이미지를 만들기 위해 제약 조건으로 사용합니다. 최종적으로 중간 이미지를 대상 차원으로 자릅니다.

  • seam carving! 당신이 당신의 서버에 설치되는 ImageMagick에 의존하는 경우도 "liquid rescale" in ImageMagick라고

를 (또한 there 참조), 그것은 GD보다 더 많은 기능을 제공합니다. 나의 이해에서

2

은 영업 이익의 문제는 새로운 차원을 계산보다는

@Psyche 크기 조정에 대한 자세한이며, 이것은 실제로 간단한 연산 문제입니다. 640x480 이미지가 있고이를 200x200 "상자"에 표시하려고한다고 가정합니다.

$sw = 640; $sh = 480; 
$dw = 200; $dh = 200; 

는 상자의 가로 세로 비율을 찾아 원래의와 비교하고이 당신에게 200x150을주는 새로운 이미지

$sr = $sw/$sh; 
$dr = $dw/$dh; 
if($sr > $dr) 
    $dh = round($dw/$sr); 
else 
    $dw = round($dh * $sr); 

에 대한 하나의 높이의 폭을 계산하고이되는 크기입니다

+0

알겠습니다. 고마워! – Psyche

+0

귀여운 : P +1 크기가 조정 된 코드 – Kumar

7

나는 이것을 몇 년 전에 작성 했으므로 정확히 찾고있는 것을 수행한다. 이것은 너비와 높이를 계산하는 것 뿐이므로 Imagick을 직접 호출하여 이러한 계산을 실제로 적용해야합니다.

/** 
* ImageIntelligentResize() 
* 
* @global Intelligently resizes images using a providid max width and height 
* @param mixed $imagePath 
* @param mixed $maxWidth 
* @param mixed $maxHeight 
* @param mixed $alwaysUpscale 
* @return 
*/ 
function ImageIntelligentResize($imagePath, $maxWidth, $maxHeight, $alwaysUpscale) 
{ 
    // garbage in, garbage out 
    if (IsNullOrEmpty($imagePath) || !is_file($imagePath) || IsNullOrEmpty($maxWidth) || IsNullOrEmpty($maxHeight)) 
    { 
     return array("width"=>"", "height"=>""); 
    } 

    // if our thumbnail size is too big, adjust it via HTML 
    $size = getimagesize($imagePath); 
    $origWidth = $size[0]; 
    $origHeight = $size[1]; 

    // Check if the image we're grabbing is larger than the max width or height or if we always want it resized 
    if ($alwaysUpscale || $origWidth > $maxWidth || $origHeight > $maxHeight) 
    { 
     // it is so let's resize the image intelligently 
     // check if our image is landscape or portrait 
     if ($origWidth > $origHeight) 
     { 
      // target image is landscape/wide (ex: 4x3) 
      $newWidth = $maxWidth; 
      $ratio = $maxWidth/$origWidth; 
      $newHeight = floor($origHeight * $ratio); 
      // make sure the image wasn't heigher than expected 
      if ($newHeight > $maxHeight) 
      { 
       // it is so limit by the height 
       $newHeight = $maxHeight; 
       $ratio = $maxHeight/$origHeight; 
       $newWidth = floor($origWidth * $ratio); 
      } 
     } 
     else 
     { 
      // target image is portrait/tall (ex: 3x4) 
      $newHeight = $maxHeight; 
      $ratio = $maxHeight/$origHeight; 
      $newWidth = floor($origWidth * $ratio); 
      // make sure the image wasn't wider than expected 
      if ($newWidth > $maxWidth) 
      { 
       // it is so limit by the width 
       $newWidth = $maxWidth; 
       $ratio = $maxWidth/$origWidth; 
       $newHeight = floor($origHeight * $ratio); 
      } 
     } 
    } 
    // it's not, so just use the current height and width 
    else 
    { 
     $newWidth = $origWidth; 
     $newHeight = $origHeight; 
    } 

    return array("width"=>$newWidth, "height"=>$newHeight); 
} 
관련 문제