PHP에서 GD 라이브러리를 활용해서 썸네일을 만들어주는 함수의 소스 코드입니다. 홈페이지를 만들다 보면 사진첩과 같이 이미지를 다수 노출해야 되는 페이지를 구성할 때 필요한 것이 썸네일이며 이를 이용하면 페이지의 로딩 속도를 향상시켜 주며 원본 이미지를 작게 보여줄 때 좀더 선명하게 표현해줄 수 있습니다. 자신의 필요에 맞게 함수를 구성할 수 있는 분이 아니라면 복사해서 활용해보시면 되겠습니다. 함수 파라메터 끝에는 썸네일을 저장할 파일 경로를 입력하시면 되고 경로를 입력하지 않으면 바로 출력됩니다.

make_thumbnail("원본이미지파일경로", 83, 100, "저장할썸네일파일경로");

웹프로그래머의 홈페이지 정보 블로그 http://hompy.info
<?
make_thumbnail("http://hompy.info/attach/1/1196323499.jpg", 83, 100, "");

function make_thumbnail($source_path, $width, $height, $thumbnail_path){
    list($img_width,$img_height, $type) = getimagesize($source_path);
    if ($type!=1 && $type!=2 && $type!=3 && $type!=15) return;
    if ($type==1) $img_sour = imagecreatefromgif($source_path);
    else if ($type==2 ) $img_sour = imagecreatefromjpeg($source_path);
    else if ($type==3 ) $img_sour = imagecreatefrompng($source_path);
    else if ($type==15) $img_sour = imagecreatefromwbmp($source_path);
    if ($img_width > $img_height) {
        $w = round($height*$img_width/$img_height);
        $h = $height;
        $x_last = round(($w-$width)/2);
        $y_last = 0;
    } else {
        $w = $width;
        $h = round($width*$img_height/$img_width);
        $x_last = 0;
        $y_last = round(($h-$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,$w,$h);
        imagedestroy($img_sour);
        $white = imagecolorallocate($img_last,255,255,255);
        imagefill($img_last, 0, 0, $white);
    } else {
        $img_dest = imagecreatetruecolor($w,$h);
        imagecopyresampled($img_dest, $img_sour,0,0,0,0,$w,$h,$img_width,$img_height);
        $img_last = imagecreatetruecolor($width,$height);
        imagecopy($img_last,$img_dest,0,0,$x_last,$y_last,$w,$h);
        imagedestroy($img_dest);
    }
    if ($thumbnail_path) {
        if ($type==1) imagegif($img_last, $thumbnail_path, 100);
        else if ($type==2 ) imagejpeg($img_last, $thumbnail_path, 100);
        else if ($type==3 ) imagepng($img_last, $thumbnail_path, 100);
        else if ($type==15) imagebmp($img_last, $thumbnail_path, 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);
}
?>
이올린에 북마크하기(0) 이올린에 추천하기(0)

트랙백 주소 :: http://hompy.info/trackback/480

댓글을 달아 주세요