PHP 썸네일 처리 함수 | Server Side

make_thumbnail("이미지경로", 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;
}

ps. 참고로 밑에 부분에 imagepng($img_last,$object_file,100) 이부분에서
만약 사용중인 php버전이 5.x 이상 버전이면 뒤에 100 -> 9 로 바꺼줘야함
5.x 버전 이상부터는 퀄리티가 0~9로 바뀐거 라고 알고 있음.ㅎ 

 

Comment Write
Comment List
등록된 코멘트가 없습니다.