본문 바로가기

PHP

(함수) thumbnail(1)

썸네일 함수

 

 

function thumbnail($org_file, $new_file, $max_width, $max_height) {

	$src_img = ImageCreateFromJPEG($org_file);
	$img_info = getImageSize($org_file);
	$img_width = $img_info[0];
	$img_height = $img_info[1];

	$img_ate = $max_width / $img_info[0];
	if ($img_ate > 1) { //작은경우
		$dst_width = $img_info[0];
		$dst_height = "";
	} else {
		$dst_width = $max_width;
		$dst_height = (int)($img_info[1] * $img_ate);
	}

	$dst_img = imagecreatetruecolor($dst_width, $dst_height);
	ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $img_width, $img_height);

	ImageInterlace($dst_img);
	ImageJPEG($dst_img,  $new_file);
	ImageDestroy($dst_img);
	ImageDestroy($src_img);
}

 

 

 

이미지 업로드 및 섬네일실행

 

 

 

$tmp_name = $_FILES['Filedata']['tmp_name'];
$name = $_FILES['Filedata']['name'];
$error = $_FILES['Filedata']['error'];
$size = $_FILES['Filedata']['size'];

$upfile_url = "./file"
if(!is_dir($upfile_dir)) {
	@mkdir($upfile_dir, 0707); //make directory
}

$file = preg_replace('/^.+\.([^\.]{1,})$/','\\1',$name);
$img_ext = mb_strtolower($file);  //file 확장자 추출

$sFileName = date("Ymdhis").".".$img_ext;//change file name
$sFilepath = $upfile_dir."/".$sFileName;

@move_uploaded_file($tmp_name, $sFilepath) or die("error");
@thumbnail($sFilepath, $sFilepath, $max_img_width, $max_img_height);