아래 소개된 코드는 PHP 웹개발 스크립트 언어로 이미지를 다루는 방법에 대해 파악해 볼 수 있는 썸네일(thumbnail)을 생성하는 PHP 함수 소스 코드 입니다. 허용하는 이미지 포멧은 GIF, JPEG, PNG, BMP 포멧이며 파라메터로 넘기는 가로, 세로 값에 의해 생성되는 섬네일의 크기를 결정합니다. 섬네일로 생성할 파일명을 마지막 파라메터로 넘겨줄 경우 넘겨준 파일명으로 섬네일을 생성하며 파라메터가 없는 경우 바로 출력 합니다. 프로그래밍 스타일에 따라 아래 코드를 수정해 보거나 좀더 세련되게 업그레이드 해보면 학습에 도움이 될 것입니다.
<?
make_thumbnail("http://hompy.info/attach/1/1166220204.jpg", 100, 80, "");
function make_thumbnail($source_file, $_width, $_height, $object_file){
list($img_width,$img_height, $type) = getimagesize($source_file);
if ($type==1) $img_sour = imagecreatefromgif($source_file);
else if ($type==2 ) $img_sour = imagecreatefromjpeg($source_file);
else if ($type==3 ) $img_sour = imagecreatefrompng($source_file);
else if ($type==15) $img_sour = imagecreatefromwbmp($source_file);
else return false;
if ($img_width > $img_height) {
$width = round($_height*$img_width/$img_height);
$height = $_height;
} else {
$width = $_width;
$height = round($_width*$img_height/$img_width);
}
if ($width < $_width) {
$width = round(($height + $_width - $width)*$img_width/$img_height);
$height = round(($width + $_width - $width)*$img_height/$img_width);
} else if ($height < $_height) {
$height = round(($width + $_height - $height)*$img_height/$img_width);
$width = round(($height + $_height - $height)*$img_width/$img_height);
}
$x_last = round(($width-$_width)/2);
$y_last = round(($height-$_height)/2);
if ($img_width < $_width || $img_height < $_height) {
$img_last = imagecreatetruecolor($_width, $_height);
$x_last = round(($_width - $img_width)/2);
$y_last = round(($_height - $img_height)/2);
imagecopy($img_last,$img_sour,$x_last,$y_last,0,0,$width,$height);
imagedestroy($img_sour);
$white = imagecolorallocate($img_last,255,255,255);
imagefill($img_last, 0, 0, $white);
} else {
$img_dest = imagecreatetruecolor($width,$height);
imagecopyresampled($img_dest, $img_sour,0,0,0,0,$width,$height,$img_width,$img_height);
$img_last = imagecreatetruecolor($_width,$_height);
imagecopy($img_last,$img_dest,0,0,$x_last,$y_last,$width,$height);
imagedestroy($img_dest);
}
if ($object_file) {
if ($type==1) imagegif($img_last, $object_file, 100);
else if ($type==2 ) imagejpeg($img_last, $object_file, 100);
else if ($type==3 ) imagepng($img_last, $object_file, 100);
else if ($type==15) imagebmp($img_last, $object_file, 100);
} else {
if ($type==1) imagegif($img_last);
else if ($type==2 ) imagejpeg($img_last);
else if ($type==3 ) imagepng($img_last);
else if ($type==15) imagebmp($img_last);
}
imagedestroy($img_last);
return true;
}
?>
웹프로그래머의 홈페이지 정보 블로그 http://hompy.info
<?
make_thumbnail("http://hompy.info/attach/1/1166220204.jpg", 100, 80, "");
function make_thumbnail($source_file, $_width, $_height, $object_file){
list($img_width,$img_height, $type) = getimagesize($source_file);
if ($type==1) $img_sour = imagecreatefromgif($source_file);
else if ($type==2 ) $img_sour = imagecreatefromjpeg($source_file);
else if ($type==3 ) $img_sour = imagecreatefrompng($source_file);
else if ($type==15) $img_sour = imagecreatefromwbmp($source_file);
else return false;
if ($img_width > $img_height) {
$width = round($_height*$img_width/$img_height);
$height = $_height;
} else {
$width = $_width;
$height = round($_width*$img_height/$img_width);
}
if ($width < $_width) {
$width = round(($height + $_width - $width)*$img_width/$img_height);
$height = round(($width + $_width - $width)*$img_height/$img_width);
} else if ($height < $_height) {
$height = round(($width + $_height - $height)*$img_height/$img_width);
$width = round(($height + $_height - $height)*$img_width/$img_height);
}
$x_last = round(($width-$_width)/2);
$y_last = round(($height-$_height)/2);
if ($img_width < $_width || $img_height < $_height) {
$img_last = imagecreatetruecolor($_width, $_height);
$x_last = round(($_width - $img_width)/2);
$y_last = round(($_height - $img_height)/2);
imagecopy($img_last,$img_sour,$x_last,$y_last,0,0,$width,$height);
imagedestroy($img_sour);
$white = imagecolorallocate($img_last,255,255,255);
imagefill($img_last, 0, 0, $white);
} else {
$img_dest = imagecreatetruecolor($width,$height);
imagecopyresampled($img_dest, $img_sour,0,0,0,0,$width,$height,$img_width,$img_height);
$img_last = imagecreatetruecolor($_width,$_height);
imagecopy($img_last,$img_dest,0,0,$x_last,$y_last,$width,$height);
imagedestroy($img_dest);
}
if ($object_file) {
if ($type==1) imagegif($img_last, $object_file, 100);
else if ($type==2 ) imagejpeg($img_last, $object_file, 100);
else if ($type==3 ) imagepng($img_last, $object_file, 100);
else if ($type==15) imagebmp($img_last, $object_file, 100);
} else {
if ($type==1) imagegif($img_last);
else if ($type==2 ) imagejpeg($img_last);
else if ($type==3 ) imagepng($img_last);
else if ($type==15) imagebmp($img_last);
}
imagedestroy($img_last);
return true;
}
?>
웹프로그래머의 홈페이지 정보 블로그 http://hompy.info




댓글을 달아 주세요
썸네일 소스보다 저 사진에 눈이 가는것은 왜일까요? ^^
저도요 ㄷㄷ;;