Web Progreming/PHP

[ PHP ] 이미지 업로드 해서 사이즈 지정

예감좋은 2012. 3. 9. 12:40
                        $img = imagecreatefromjpeg($file_names);

$width = imageSX($img);
$height = imageSY($img);
// 이미지 사이즈 지정.. 아래 사이즈 보다 이미지가 작으면 나머지 공간은 검은색으로 모두 채움.
$target_width = 800;
$target_height = 450;

$target_ratio = $target_width / $target_height;
$img_ratio = $width / $height;

if ($target_ratio > $img_ratio) {
$new_height = $target_height;
$new_width = $img_ratio * $target_height;
} else {
$new_height = $target_width / $img_ratio;
$new_width = $target_width;
}

if ($new_height > $target_height) {
$new_height = $target_height;
}
if ($new_width > $target_width) {
$new_height = $target_width;
}

$new_img = ImageCreateTrueColor($target_width, $target_height);
@imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, 0);
@imagecopyresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 0, 0, $new_width, $new_height, $width, $height);
ob_start();
imagejpeg($new_img,$file_names);
ob_end_clean();