<?
class thumb {
public function __construct() {}
// create thumbnail, will accept jpeg and gif.
public static function create_thumbnail($file, $max_side = false, $fixed = false) {
// 1 = GIF, 2 = JPEG
if(!$max_side) $max_side = 100;
if(file_exists($file)) {
$type = getimagesize($file);
if(!function_exists('imagegif') && $type[2] == 1) {
$error = 'Filetype not supported. Thumbnail not created.';
}
elseif (!function_exists('imagejpeg') && $type[2] == 2) {
$error = 'Filetype not supported. Thumbnail not created.';
}
else {
// create the initial copy from the original file
if($type[2] == 1) {
$image = imagecreatefromgif($file);
}
elseif($type[2] == 2) {
$image = imagecreatefromjpeg($file);
}
if(function_exists('imageantialias'))
imageantialias($image, TRUE);
$image_attr = getimagesize($file);
// figure out the longest side
if($image_attr[0] > $image_attr[1]):
$image_width = $image_attr[0];
$image_height = $image_attr[1];
if($fixed) {
$image_new_width = $max_side;
$image_new_height = (int)($max_side * 3 / 4);
// 4:3 ratio
} else {
$image_new_width = $max_side;
$image_ratio = $image_width / $image_new_width;
$image_new_height = (int) ($image_height / $image_ratio);
}
//width > height
else:
$image_width = $image_attr[0];
$image_height = $image_attr[1];
if($fixed) {
$image_new_height = $max_side;
$image_new_width = (int)($max_side * 3 / 4);
// 3:4 ratio
} else {
$image_new_height = $max_side;
$image_ratio = $image_height / $image_new_height;
$image_new_width = (int) ($image_width / $image_ratio);
}
//height > width
endif;
$thumbnail = imagecreatetruecolor($image_new_width, $image_new_height);
@ imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1]);
$thumb = preg_replace('!(\.[^.]+)?$!', '.thumbnail'.'$1', basename($file), 1);
$thumbpath = str_replace(basename($file), $thumb, $file);
// move the thumbnail to it's final destination
if($type[2] == 1) {
if (!imagegif($thumbnail, $thumbpath)) {
$error = 'Thumbnail path invalid';
}
}
elseif($type[2] == 2) {
if (!imagejpeg($thumbnail, $thumbpath)) {
$error = 'Thumbnail path invalid';
}
}
}
} else {
$error = 'File not found';
}
if(!empty($error)) {
die($error);
} else {
return $thumbpath;
}
}
};
$imagethumb = thumb::create_thumbnail("img/junmin21_15.jpg", 115);
// 115에 맞는 비율로 정확히 줄인다.
$imagethumb = thumb::create_thumbnail("img/junmin21_15.jpg", 115, 1);
// 115로 줄이지만 4:3 이나 3:4이다.
if ( $type == 1 ) { $img_source = imagecreatefromgif($source_file); }
else if ( $type == 2 ) { $img_source = imagecreatefromjpeg($source_file); }
else if ( $type == 3 ) { $img_source = imagecreatefrompng($source_file); }
else if ( $type == 15 ) { $img_source = imagecreatefromwbmp($source_file); }
?>
[출처] 마루아라
"쇼핑몰·홈페이지·오픈마켓
블로그·페이스북·이메일 등의 각종 마케팅 글쓰기, 각종 광고, 영업, 판매, 제안서, 전단지 반응율 3배×10배 이상 높이는 마법의 8단계 공식" |
![]() |
☞자세히보기 |
|
|