2013-03-16 2 views
1

저는 현재 너무 작아 보이지는 않지만 만드는 갤러리의 미리보기 이미지를 허용하도록 이미지의 크기를 조정할 수있는 코드를 구현했습니다.이미지를 특정 높이로 크기 조정

이미지의 크기를 조정하기 위해 미리 작성된 코드를 사용하고 있습니다. 이미지의 크기를 최소 195 픽셀, 너비는 195 픽셀 + 가능한 한 비율로 유지하는 데 어려움을 겪고 있습니다.

페이지로드 시간을 향상시킬 수있는 이미지를 최적화 할 수있는 방법이 있습니까?

다음은 현재 코드입니다. 도움을 주시면 감사하겠습니다. 감사합니다.

function imageResize() { 
$filename = $row['main']; 

if (exif_imagetype($filename) == IMAGETYPE_GIF) { 
    $create = imagecreatefromgif; 
} 

if (exif_imagetype($filename) == IMAGETYPE_JPEG) { 
    $create = imagecreatefromjpeg; 
} 

if (exif_imagetype($filename) == IMAGETYPE_PNG) { 
    $create = imagecreatefrompng; 
} 

$width = 600; //These values have been tinkered with from their original values 
$height = 500; 

header('Content-Type: image/jpeg'); 

list($width_orig, $height_orig) = getimagesize($filename); 

$ratio_orig = $width_orig/$height_orig; 

if ($height_orig < 300) { 
    $width = $height*$ratio_orig; 
} else { 
    $height = $width/$ratio_orig; 
} 

$image_p = imagecreatetruecolor($width, $height); 
$image = $create($filename); 
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); 

imagejpeg($image_p, null, 100); 
} 
+0

그런 다음 문제가 무엇입니까? 또한'$ create ($ filename)'은 오타입니까? – Amir

+0

내 이미지가 최소 195px로 표시되지 않습니다. $ create는 변수이므로 $을 사용하면 깨뜨릴 수 있습니다. :) 아마도 가장 적절한 이름 변수가 아닐 수 있습니다. –

답변

0

원본 이미지, 너비 또는 너비가 더 작은 것을 확인하십시오. 이 값이 최소 195 픽셀보다 작 으면이 최소값으로 설정하고 이미지의 원래 가로 세로 비율에 따라 다른 값의 배율을 조정하십시오.

$minSize = 195; 
$aspectRatio = $width_orig/$height_orig; 

if ($width_orig < $height_orig) { 
    if ($width_orig < $minSize) { 
     $width = $minSize; 
     $height = $width/$aspectRatio; 
    } 
} else { 
    if ($height_orig < $minSize) { 
     $height = $minSize; 
     $width = $height * $aspectRatio; 
    } 
} 
관련 문제