2014-02-20 3 views
0

사진의 크기를 조정하고 자르려고합니다.PHP로 사진 자르기가 늘어납니다.

사진은 가로 또는 세로 방향 일 수 있지만 크기가 250x250이고 뻗어 있지 않길 원합니다 (측면 또는 상단/하단 자르기를 유지하지만 이미지의 대부분을 새 크기로 유지하고 정사각형으로 자른 경우). WordPress의 미리보기 이미지).

이 내가 무슨 일을하고있는 중이 야

... 그것은 적당한 크기하지만이 사진을 스트레칭입니다, 내 기능입니다?

function make_thumb($src, $dest, $desired_width, $ext) { 

    if ($ext == 'jpg' || $ext == 'jpeg') { 
      $source_image = imagecreatefromjpeg($src);  
    } 
    if ($ext == 'png') { 
      $source_image = imagecreatefrompng($src);  
    } 
    if ($ext == 'gif') { 
      $source_image = imagecreatefromgif($src);  
    } 

    /* read the source image */ 
    $width = imagesx($source_image); 
    $height = imagesy($source_image); 

/* find the "desired height" of this thumbnail, relative to the desired width */ 
    $desired_height = floor($height * ($desired_width/$width)); 
    $desired_height = 250; 

    /* create a new, "virtual" image */ 
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height); 

    /* copy source image at a resized size */ 
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height); 

    /* create the physical thumbnail image to its destination */ 

    if ($ext == 'jpg' || $ext == 'jpeg') { 
      imagejpeg($virtual_image, $dest);  
    } 
    if ($ext == 'png') { 
      imagepng($virtual_image, $dest);   
    } 
    if ($ext == 'gif') { 
      imagegif($virtual_image, $dest);   
    } 
} 

고마워요!

+0

일단 이미지 크기를 조정하면 자르기에'imagecopy'를 사용할 수 있습니까? –

+0

@Tom No, 크기를 복사 할 때 이미지를 왜곡하면 아무 것도 고칠 수 없습니다. – RiggsFolly

+0

@ RiggsFolly 당신 말이 맞아요. 나는 크기 조정이 올바른 비율로 행해질 것이라고 가정하고있었습니다. 'imagecopyresampled'도 자르기에 사용할 수 있기 때문에'imagecopy'를 사용하는 것은 잔상을 보존하고 싶지 않고 자른 미리보기 이미지 만 남기고 싶다면 중복되는 단계입니다. 그러나 우리는 그것을 모른다. –

답변

1

잠깐 얘기하시는 것을 생각해보십시오.

이미지의 너비를 변경하고 너비와 정확히 같은 비율로 높이를 변경하지 않으면 이미지가 항상 왜곡됩니다.

필자는 실제로 calc을 테스트하지는 않았지만 임의의 높이 250으로 계산을 재정의했지만 왜곡없이 코드를 올바르게 수행 할 수 있습니다. 따라서 왜곡이 발생합니다.

이 줄을 주석하고 아마 당신은 당신이 항상 250 높이 인 이미지를 원하는 알고있는 경우, 왜곡 그래서

$desired_height = 250; 

중지에 전달보다는 비례 새 폭을 calulate하는 calulation 변경됩니다 매개 변수로.

function make_thumb($src, $dest, $desired_height = 250, $ext) { 

    if ($ext == 'jpg' || $ext == 'jpeg') { $source_image = imagecreatefromjpeg($src); } 
    if ($ext == 'png') { $source_image = imagecreatefrompng($src); } 
    if ($ext == 'gif') { $source_image = imagecreatefromgif($src); } 

    /* read the source image */ 
    $width = imagesx($source_image); 
    $height = imagesy($source_image); 

    /* calc the new width */ 
    $desired_width = floor($width * ($desired_height/$height)); 

    /* create a new, "virtual" image */ 
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height); 

    /* copy source image at a resized size */ 
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height); 

    /* create the physical thumbnail image to its destination */ 

    if ($ext == 'jpg' || $ext == 'jpeg') { imagejpeg($virtual_image, $dest); } 
    if ($ext == 'png') { imagepng($virtual_image, $dest); } 
    if ($ext == 'gif') { imagegif($virtual_image, $dest); } 
} 

물론 너비는 원본 이미지의 실제 크기에 따라 조금씩 다릅니다. 그러나 왜곡을 멈추고 싶다면 그렇게해야합니다.

+0

250을 250으로 설정하고 하나를 타협하지 않는 방법이 있습니까? –

+0

원본 사진의 크기가 너비와 높이가 같은 경우에만 (예 : 1000 x 1000의 정사각형) 사진은 일반적으로 사각형이 아닙니다. 실제 크기에서 정사각형으로 잘라낸 다음 250x250으로 줄일 수 있지만 PHP에는 눈이 없으므로 사진의 좋은 비트를 얻지 못했을 것입니다. – RiggsFolly

관련 문제