### 그누폐인님의 요청에 따라 기능을 보강하여 원본을 수정하였습니다.
### 추가된 옵션 $copy_rule 값이 비엇거나 width이면 너비를 기준으로 하여 정렬합니다.
### 추가된 옵션 $copy_rule 값이 height이면 높이를 기준으로 하여 정렬합니다.
### 추가된 옵션 $copy_pos 값이 비엇거나 1이면 원본이미지의 왼쪽이나 상단을 기준으로 정렬합니다.
### 추가된 옵션 $copy_pos 값이2이면 원본이미지의 중앙을 기준으로 정렬합니다.
### 추가된 옵션 $copy_pos 값이3이면 원본이미지의 오른쪽이나 하단을 기준으로 정렬합니다.
리스트 스킨 같은 곳에서 이미지 썸네일을 쉽게 만들고 보여주는 방법입니다.
이미 썸네일이 만들어져 있으면 재작업을 하지 않으며,
크기에 따른 개별적 썸네일을 만들수 있으며,
썸네일끼리 영향을 받지 않으며,
브라우져 캐쉬에도 영향을 받지 않습니다.
lib 디렉토리 내의 적당한 라이브러리 파일에 다음 두 함수를 추가합니다.
//원본이미지의 경로, 크기, 높이, 타입을 알 경우에 사용가능
function make_smallimg ($src, $src_w, $src_h, $src_t, $copy, $copy_w, $copy_h, $copy_rule='width', $copy_pos='1'){
if ($src_t == 1)
$src = @imagecreatefromgif($src);
else if ($src_t == 2)
$src = @imagecreatefromjpeg($src);
else if ($src_t == 3)
$src = @imagecreatefrompng($src);
else
return false;
if (empty($src)) return false;
if (empty($copy) || $copy_w < 10 || $copy_h < 10) return false;
$src_x = 0;
$src_y = 0;
$copy_x = 0;
$copy_y = 0;
if ($copy_rule == 'width' || empty($copy_rule)) {//너비에 맞춤
$rate = $src_h / $src_w;
$new_w = $copy_w;
$new_h = (int) ($rate * $copy_w);
if ($new_h < $copy_h){//만들어질 썸네일 높이가 비율대로 줄여진 높이보다 클경우 가운데 위치시킴
$copy_y = (int) (($copy_h - $new_h) / 2);
}
else {
if ($copy_pos == '1' || empty($copy_pos)) {//원본에서 상단을 기준으로 가져옴
//기본값 그대로
}
else if ($copy_pos == '2') {//원본에서 중앙을 기준으로 가져옴
$temp_h = (int) ($copy_h / $copy_w * $src_w);
$src_y = (int) (($src_h - $temp_h) / 2);
}
else if ($copy_pos == '3') {//원본에서 하단을 기준으로 가져옴
$temp_h = (int) ($copy_h / $copy_w * $src_w);
$src_y = $src_h - $temp_h;
}
}
}
else {//높이에 맞춤
$rate = $src_w / $src_h;
$new_h = $copy_h;
$new_w = (int) ($rate * $copy_h);
if ($new_w < $copy_w){//만들어질 썸네일 너비가 비율대로 줄여진 너비보다 클경우 가운데 위치시킴
$copy_x = (int) (($copy_w - $new_w) / 2);
}
else {
if ($copy_pos == '1' || empty($copy_pos)) {//원본에서 왼쪽을 기준으로 가져옴
//기본값 그대로
}
else if ($copy_pos == '2') {//원본에서 중앙을 기준으로 가져옴
$temp_w = (int) ($copy_w / $copy_h * $src_h);
$src_x = (int) (($src_w - $temp_w) / 2);
}
else if ($copy_pos == '3') {//원본에서 오른쪽을 기준으로 가져옴
$temp_w = (int) ($copy_w / $copy_h * $src_h);
$src_x = $src_w - $temp_w;
}
}
}
$dst = @imagecreatetruecolor($copy_w, $copy_h);
if (empty($dst)) return false;
$background_color = @imagecolorallocate($dst, 255, 255, 255);
if (empty($background_color)) return false;
imagefilledrectangle($dst, 0, 0, $copy_w, $copy_h, $background_color);
imagecopyresampled($dst, $src, $copy_x, $copy_y, $src_x, $src_y, $new_w, $new_h, $src_w, $src_h);
imagepng($dst, $copy);
chmod($copy, 0606);
return true;
}
//리스트스킨에서 썸네일을 보여줄때 사용
function get_smallimg_in_list($bo_table, $file, $num, $w, $h, $error_img, $style='', $copy_rule='width', $copy_pos='1'){
global $g4;
$temp_origine_img = "$g4[path]/data/file/$bo_table/" . urlencode($file[$num]['file']);
if (empty($bo_table) || empty($file[$num]) || !is_file($temp_origine_img) || $w < 10 || $h < 10) {
$temp_small = false;
}
else {
if (empty($file[$num]['image_width']) || empty($file[$num]['image_height']) || empty($file[$num]['image_type'])) {
$temp = getimagesize($temp_origine_img);
$temp_origine_img_w = $temp[0];
$temp_origine_img_h = $temp[1];
$temp_origine_img_t = $temp[2];
}
else {
$temp_origine_img_w = $file[$num]['image_width'];
$temp_origine_img_h = $file[$num]['image_height'];
$temp_origine_img_t = $file[$num]['image_type'];
}
$temp_small_img_w = $w;
$temp_small_img_h = $h;
$temp_small_dir = "$g4[path]/data/file/$bo_table/smallimg";
if (!is_dir($temp_small_dir)){
mkdir($temp_small_dir) or die('썸네일을 저장할 디렉토리를 생성할수 없습니다.');
}
$temp_small_img = "$temp_small_dir/s_{$temp_small_img_w}_{$temp_small_img_h}_" . urlencode($file[$num][file]);
if (is_file($temp_origine_img) && !is_file($temp_small_img)) {
$temp_small = make_smallimg ($temp_origine_img, $temp_origine_img_w, $temp_origine_img_h, $temp_origine_img_t, $temp_small_img, $temp_small_img_w, $temp_small_img_h, $copy_rule, $copy_pos);
}
else if (is_file($temp_origine_img) && is_file($temp_small_img)){
$temp_small = true;
}
else {
$temp_small = false;
}
}
if ($temp_small) {
return "<img src='$temp_small_img' width='$temp_small_img_w' height='$temp_small_img_h' border=0 $style onError=\"this.src='$error_img';\">";
}
else {
return "<img src='$error_img' width='$w' height='$h' border=0 $style>";
}
}
그리고 해당 리스트 스킨에서
for ($i=0; $i<count($list); $i++) {
아래에 적당한 위치에 이미지를 출력할 부분에
아래와 같이 넣으면 됩니다.
<?=get_smallimg_in_list($board['bo_table'], $list[$i]['file'], 0, 75, 50, '/img/errorimg.gif', 'style="border:1px solid #000000;"', 'width', '1')?>
위에서 0, 75, 50, '/img/errorimg.gif', 'width', '1' 부분만 설명하면(나머진 그대로 하면 됩니다.)
0은 업로드 된 이미지중 몇번째 것을 썸네일로 보여줄것인지 지정합니다. 0, 1, 2, 3, 4 등이 올수 있습니다.
75는 만들어질 썸네일을 너비 입니다.
50은 만들어질 썸네일의 높이 입니다.
'/img/errorimg.gif' 은 이미지가 없거나 썸네일 생성 실패시 대신 보여줄 이미지 입니다.
'width' 는 너비와 높이중 어느것을 기준으로 할것인지 지정한것입니다. 썸네일로 보여질 모양이 너비가 길면' width', 높이가 길면 'height'로 하는 것이 좋습니다.
'1' 만들어질 썸네일의 비율보다 높이나 너비가 클경우 어느 부분을 짤라올것인지 결정합니다. '1'은 왼쪽이나 상단, '2'는 중앙, '3'은 오른쪽이나 하단을 기준으로 잘라옵니다.
[이 게시물은 morning님에 의해 2009-11-22 17:49:48 홈관리자료실에서 이동 됨]
[이 게시물은 morning님에 의해 2009-11-22 17:56:43 알림판에서 이동 됨]
주님과 함께 모닝커피를