<?php #php##source###image####썸네일을 원본의 비율대로 생성하여 주는 함수 - gd를 이용 ?>
<?php
/*제작자 : 유창화
사용제한 : 사용은 자유롭습니다. 단, 강의나 책의 내용으로서 사용될 경우 허락을 받으셔야 합니다.*/
//모든 에러를 출력하도록 설정한다.
error_reporting(E_ALL);
//기본 함수 모음을 인클루드
define('YFUNCTION_INCLUDED', true);// 소스를 보여주기 위한 상수
include_once './Yfunction.php';
//처리시간 측정시작
$Ystarttime = Yget_microtime();
//썸네일 이미지 사이지 결정하여 썸네일 생성
//$src_file_size, $dest_file_size 이미지 정보를 담은 배열 0은 너비 1은 높이
function Ysumnail_rule($src_file, $dest_file, $src_file_size, $dest_file_size, $image_quality=90, $lib='gd'){
//생성도중 에러가 날수 있는 것들을 체크 하여 return false
if (!is_array($src_file_size) || !is_array($dest_file_size) || empty($src_file_size[0]) || empty($src_file_size[1]) || empty($dest_file_size[0]) || empty($dest_file_size[0])) {
return false;
}
//$lib 확인후 조건에 맞지 않으면 기본값 세팅
if (empty($lib) || ($lib != 'gd' && $lib != 'netpbm')) $lib = 'gd';
$rate = $src_file_size[1] / $src_file_size[0];
$temp[1] = (int)($dest_file_size[0] * $rate);
if ($dest_file_size[1] < $temp[1]) {
$rate = $src_file_size[0] / $src_file_size[1];
$dest_file_size[0] = (int)($dest_file_size[1] * $rate);
}
else{
$dest_file_size[1] = $temp[1];
}
//썸네일의 너비나 높이가 10 미만인것은 만들지 않는다.
if ($dest_file_size[0] < 10 || $dest_file_size[1] < 10) {
return false;
}
//썸네일 이미지가 원본이미지 크기보다 크게 설정되었을 경우, 원본이미지와 동일하게
if ($dest_file_size[0] > $src_file_size[0]) {
$dest_file_size = $src_file_size;
}
if ($lib == 'netpbm') return Ymake_sumnail_netpbm($src_file, $dest_file, $src_file_size, $dest_file_size, $image_quality);
else return Ymake_sumnail_gd($src_file, $dest_file, $src_file_size, $dest_file_size, $image_quality);
}
//섬네일 생성
function Ymake_sumnail_gd($src_file, $dest_file, $src_file_size, $dest_file_size, $image_quality=90){
//생성도중 에러가 날수 있는 것들을 체크 하여 return false
if (empty($src_file) || empty($dest_file) || !is_file($src_file) || !is_array($src_file_size) || !is_array($dest_file_size) || empty($src_file_size[0]) || empty($src_file_size[1]) || empty($src_file_size[2]) || empty($dest_file_size[0]) || empty($dest_file_size[0])) {
return false;
}
//$image_quality 확인후 조건에 맞지 않으면 기본값 세팅
if(!is_numeric($image_quality) || empty($image_quality)) $image_quality = 90;
//원본사이즈보다 썸네일 사이즈가 더 크면 원본사이즈와 같게 썸네일을 생성
if ($dest_file_size[0] > $src_file_size[0]) {
$dest_file_size = $src_file_size;
}
switch($src_file_size[2]) {
case 1: // GIF image
$src = @ImageCreateFromGIF($src_file); break;
case 2: // JPEG image
$src = @ImageCreateFromJPEG($src_file); break;
case 3: // PNG image
$src = @ImageCreateFromPNG($src_file); break;
default: // 정해진 이외의 포맷은 return false
return false;
}
if (function_exists("imagecreatetruecolor")) {
// This function requires GD 2.0.1 or later.
$dst = @ImageCreateTrueColor($dest_file_size[0], $dest_file_size[1]);
}
else {
$dst = @ImageCreate($dest_file_size[0], $dest_file_size[1]);
}
// 1.00.05 gd 버전에 따라
if (function_exists("imagecopyresampled")) {
@imagecopyresampled($dst, $src, 0, 0, 0, 0, $dest_file_size[0], $dest_file_size[1], $src_file_size[0], $src_file_size[1]);
}
else {
// 1.00.02 imagecopyresized -> imagecopyresampled 로 교체
@imagecopyresized($dst, $src, 0, 0, 0, 0, $dest_file_size[0], $dest_file_size[1], $src_file_size[0], $src_file_size[1]);
}
@ImageJPEG($dst, $dest_file, $image_quality);
@ImageDestroy($src);
@ImageDestroy($dst);
//퍼미션 변경가능 여부를 가지고 썸네일 생성 실패 판단
return @chmod($dest_file, 0777);
}
$src_file = './temp/test.png';
$src_file_size = getimagesize($src_file);
$dest_file = './temp/s_gd_test.png';
$dest_file_size = Array(200, 100);
$result = Ysumnail_rule($src_file, $dest_file, $src_file_size, $dest_file_size, 90, 'gd');
echo "<br>png 원본이미지 <img src='" . $src_file . "' border=0>";
if (empty($result)) echo "<br>png 썸네일 생성에 실패하였습니다. 저장디렉토리의 퍼미션이나 원본의 이미지포맷, 또는 gd지원여부를 확인하세요.";
else echo "<br>png 썸네일이미지 <img src='" . $dest_file . "' border=0>";
$src_file = './temp/test.jpg';
$src_file_size = getimagesize($src_file);
$dest_file = './temp/s_gd_test.jpg';
$dest_file_size = Array(200, 100);
$result = Ysumnail_rule($src_file, $dest_file, $src_file_size, $dest_file_size, 90, 'gd');
echo "<br>jpg 원본이미지 <img src='" . $src_file . "' border=0>";
if (empty($result)) echo "<br><b>jpg 썸네일 생성에 실패하였습니다. 저장디렉토리의 퍼미션이나 원본의 이미지포맷, 또는 gd지원여부를 확인하세요.</b>";
else echo "<br>jpg 썸네일이미지 <img src='" . $dest_file . "' border=0>";
$src_file = './temp/test.gif';
$src_file_size = getimagesize($src_file);
$dest_file = './temp/s_gd_test.gif';
$dest_file_size = Array(200, 100);
$result = Ysumnail_rule($src_file, $dest_file, $src_file_size, $dest_file_size, 90, 'gd');
echo "<br>gif 원본이미지 <img src='" . $src_file . "' border=0>";
if (empty($result)) echo "<br><b>gif 썸네일 생성에 실패하였습니다. 저장디렉토리의 퍼미션이나 원본의 이미지포맷, 또는 gd지원여부를 확인하세요.</b>";
else echo "<br>gif 썸네일이미지 <img src='" . $dest_file . "' border=0>";
$src_file = './temp/test.bmp';
$src_file_size = getimagesize($src_file);
$dest_file = './temp/s_gd_test.bmp';
$dest_file_size = Array(200, 100);
$result = Ysumnail_rule($src_file, $dest_file, $src_file_size, $dest_file_size, 90, 'gd');
echo "<br>bmp 원본이미지 <img src='" . $src_file . "' border=0>";
if (empty($result)) echo "<br><b>bmp 썸네일 생성에 실패하였습니다. 저장디렉토리의 퍼미션이나 원본의 이미지포맷, 또는 gd지원여부를 확인하세요.</b>";
else echo "<br>bmp 썸네일이미지 <img src='" . $dest_file . "' border=0>";
?>
<?php
//처리시간 출력
Yecho_usetime($Ystarttime);
//설명글 출력
$guide_text = '
[Ysumnail_rule 요약]
썸네일을 원본의 비율대로 생성하여 줍니다.
gd를 사용하여 썸네일을 만듭니다.
gif, jpeg, png 포맷만 가능합니다.
썸네일의 확장자는 원본과 동일하나 파일포맷은 모두 jpeg입니다.
php 에서 gd가 지원되어야만 사용가능합니다.
[리턴값]
썸네일이나 복사 성공시 true, 실패시 false;
[사용법]
Ysumnail_rule(원본파일 경로, 생성할 썸네일경로, 이미지정보배열(0=>원본 너비, 1=>원본 높이, 2=>이미지포맷정보), 생성할 썸네일정보배열(0=>원본 너비, 1=>원본 높이), 썸네일생성질(100 이하의 숫자입력), 사용할라이브러리 지정(gd 나 netpbm))
';
Yecho_guide($guide_text);
//소스보기 출력
Yecho_viewsource();
?><div class='small'>[이 게시물은 관리자님에 의해 2011-10-31 17:12:10 PHP & HTML에서 이동 됨]</div>
<?php
/*제작자 : 유창화
사용제한 : 사용은 자유롭습니다. 단, 강의나 책의 내용으로서 사용될 경우 허락을 받으셔야 합니다.*/
//모든 에러를 출력하도록 설정한다.
error_reporting(E_ALL);
//기본 함수 모음을 인클루드
define('YFUNCTION_INCLUDED', true);// 소스를 보여주기 위한 상수
include_once './Yfunction.php';
//처리시간 측정시작
$Ystarttime = Yget_microtime();
//썸네일 이미지 사이지 결정하여 썸네일 생성
//$src_file_size, $dest_file_size 이미지 정보를 담은 배열 0은 너비 1은 높이
function Ysumnail_rule($src_file, $dest_file, $src_file_size, $dest_file_size, $image_quality=90, $lib='gd'){
//생성도중 에러가 날수 있는 것들을 체크 하여 return false
if (!is_array($src_file_size) || !is_array($dest_file_size) || empty($src_file_size[0]) || empty($src_file_size[1]) || empty($dest_file_size[0]) || empty($dest_file_size[0])) {
return false;
}
//$lib 확인후 조건에 맞지 않으면 기본값 세팅
if (empty($lib) || ($lib != 'gd' && $lib != 'netpbm')) $lib = 'gd';
$rate = $src_file_size[1] / $src_file_size[0];
$temp[1] = (int)($dest_file_size[0] * $rate);
if ($dest_file_size[1] < $temp[1]) {
$rate = $src_file_size[0] / $src_file_size[1];
$dest_file_size[0] = (int)($dest_file_size[1] * $rate);
}
else{
$dest_file_size[1] = $temp[1];
}
//썸네일의 너비나 높이가 10 미만인것은 만들지 않는다.
if ($dest_file_size[0] < 10 || $dest_file_size[1] < 10) {
return false;
}
//썸네일 이미지가 원본이미지 크기보다 크게 설정되었을 경우, 원본이미지와 동일하게
if ($dest_file_size[0] > $src_file_size[0]) {
$dest_file_size = $src_file_size;
}
if ($lib == 'netpbm') return Ymake_sumnail_netpbm($src_file, $dest_file, $src_file_size, $dest_file_size, $image_quality);
else return Ymake_sumnail_gd($src_file, $dest_file, $src_file_size, $dest_file_size, $image_quality);
}
//섬네일 생성
function Ymake_sumnail_gd($src_file, $dest_file, $src_file_size, $dest_file_size, $image_quality=90){
//생성도중 에러가 날수 있는 것들을 체크 하여 return false
if (empty($src_file) || empty($dest_file) || !is_file($src_file) || !is_array($src_file_size) || !is_array($dest_file_size) || empty($src_file_size[0]) || empty($src_file_size[1]) || empty($src_file_size[2]) || empty($dest_file_size[0]) || empty($dest_file_size[0])) {
return false;
}
//$image_quality 확인후 조건에 맞지 않으면 기본값 세팅
if(!is_numeric($image_quality) || empty($image_quality)) $image_quality = 90;
//원본사이즈보다 썸네일 사이즈가 더 크면 원본사이즈와 같게 썸네일을 생성
if ($dest_file_size[0] > $src_file_size[0]) {
$dest_file_size = $src_file_size;
}
switch($src_file_size[2]) {
case 1: // GIF image
$src = @ImageCreateFromGIF($src_file); break;
case 2: // JPEG image
$src = @ImageCreateFromJPEG($src_file); break;
case 3: // PNG image
$src = @ImageCreateFromPNG($src_file); break;
default: // 정해진 이외의 포맷은 return false
return false;
}
if (function_exists("imagecreatetruecolor")) {
// This function requires GD 2.0.1 or later.
$dst = @ImageCreateTrueColor($dest_file_size[0], $dest_file_size[1]);
}
else {
$dst = @ImageCreate($dest_file_size[0], $dest_file_size[1]);
}
// 1.00.05 gd 버전에 따라
if (function_exists("imagecopyresampled")) {
@imagecopyresampled($dst, $src, 0, 0, 0, 0, $dest_file_size[0], $dest_file_size[1], $src_file_size[0], $src_file_size[1]);
}
else {
// 1.00.02 imagecopyresized -> imagecopyresampled 로 교체
@imagecopyresized($dst, $src, 0, 0, 0, 0, $dest_file_size[0], $dest_file_size[1], $src_file_size[0], $src_file_size[1]);
}
@ImageJPEG($dst, $dest_file, $image_quality);
@ImageDestroy($src);
@ImageDestroy($dst);
//퍼미션 변경가능 여부를 가지고 썸네일 생성 실패 판단
return @chmod($dest_file, 0777);
}
$src_file = './temp/test.png';
$src_file_size = getimagesize($src_file);
$dest_file = './temp/s_gd_test.png';
$dest_file_size = Array(200, 100);
$result = Ysumnail_rule($src_file, $dest_file, $src_file_size, $dest_file_size, 90, 'gd');
echo "<br>png 원본이미지 <img src='" . $src_file . "' border=0>";
if (empty($result)) echo "<br>png 썸네일 생성에 실패하였습니다. 저장디렉토리의 퍼미션이나 원본의 이미지포맷, 또는 gd지원여부를 확인하세요.";
else echo "<br>png 썸네일이미지 <img src='" . $dest_file . "' border=0>";
$src_file = './temp/test.jpg';
$src_file_size = getimagesize($src_file);
$dest_file = './temp/s_gd_test.jpg';
$dest_file_size = Array(200, 100);
$result = Ysumnail_rule($src_file, $dest_file, $src_file_size, $dest_file_size, 90, 'gd');
echo "<br>jpg 원본이미지 <img src='" . $src_file . "' border=0>";
if (empty($result)) echo "<br><b>jpg 썸네일 생성에 실패하였습니다. 저장디렉토리의 퍼미션이나 원본의 이미지포맷, 또는 gd지원여부를 확인하세요.</b>";
else echo "<br>jpg 썸네일이미지 <img src='" . $dest_file . "' border=0>";
$src_file = './temp/test.gif';
$src_file_size = getimagesize($src_file);
$dest_file = './temp/s_gd_test.gif';
$dest_file_size = Array(200, 100);
$result = Ysumnail_rule($src_file, $dest_file, $src_file_size, $dest_file_size, 90, 'gd');
echo "<br>gif 원본이미지 <img src='" . $src_file . "' border=0>";
if (empty($result)) echo "<br><b>gif 썸네일 생성에 실패하였습니다. 저장디렉토리의 퍼미션이나 원본의 이미지포맷, 또는 gd지원여부를 확인하세요.</b>";
else echo "<br>gif 썸네일이미지 <img src='" . $dest_file . "' border=0>";
$src_file = './temp/test.bmp';
$src_file_size = getimagesize($src_file);
$dest_file = './temp/s_gd_test.bmp';
$dest_file_size = Array(200, 100);
$result = Ysumnail_rule($src_file, $dest_file, $src_file_size, $dest_file_size, 90, 'gd');
echo "<br>bmp 원본이미지 <img src='" . $src_file . "' border=0>";
if (empty($result)) echo "<br><b>bmp 썸네일 생성에 실패하였습니다. 저장디렉토리의 퍼미션이나 원본의 이미지포맷, 또는 gd지원여부를 확인하세요.</b>";
else echo "<br>bmp 썸네일이미지 <img src='" . $dest_file . "' border=0>";
?>
<?php
//처리시간 출력
Yecho_usetime($Ystarttime);
//설명글 출력
$guide_text = '
[Ysumnail_rule 요약]
썸네일을 원본의 비율대로 생성하여 줍니다.
gd를 사용하여 썸네일을 만듭니다.
gif, jpeg, png 포맷만 가능합니다.
썸네일의 확장자는 원본과 동일하나 파일포맷은 모두 jpeg입니다.
php 에서 gd가 지원되어야만 사용가능합니다.
[리턴값]
썸네일이나 복사 성공시 true, 실패시 false;
[사용법]
Ysumnail_rule(원본파일 경로, 생성할 썸네일경로, 이미지정보배열(0=>원본 너비, 1=>원본 높이, 2=>이미지포맷정보), 생성할 썸네일정보배열(0=>원본 너비, 1=>원본 높이), 썸네일생성질(100 이하의 숫자입력), 사용할라이브러리 지정(gd 나 netpbm))
';
Yecho_guide($guide_text);
//소스보기 출력
Yecho_viewsource();
?><div class='small'>[이 게시물은 관리자님에 의해 2011-10-31 17:12:10 PHP & HTML에서 이동 됨]</div>
댓글 2개
sjsjin
19년 전
거의 유사하군요..
저도 여기다 올릴걸..ㅋ~
http://www.sir.co.kr/bbs/board.php?bo_table=g4_tiptech&wr_id=3120&sca=&sfl=wr_name%2C1&stx=sjsjin&sop=and
저도 여기다 올릴걸..ㅋ~
http://www.sir.co.kr/bbs/board.php?bo_table=g4_tiptech&wr_id=3120&sca=&sfl=wr_name%2C1&stx=sjsjin&sop=and
Photofly
19년 전
감사합니다... 필요할때 요긴하게 쓰이겠습니다....^^
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7630 | 10년 전 | 647 | ||
| 7629 |
|
10년 전 | 2379 | |
| 7628 | 10년 전 | 786 | ||
| 7627 |
|
10년 전 | 1017 | |
| 7626 |
|
10년 전 | 1785 | |
| 7625 | 10년 전 | 691 | ||
| 7624 | 10년 전 | 708 | ||
| 7623 |
|
10년 전 | 3074 | |
| 7622 | 10년 전 | 719 | ||
| 7621 |
leeleeleelee
|
10년 전 | 580 | |
| 7620 | 10년 전 | 537 | ||
| 7619 | 10년 전 | 479 | ||
| 7618 | 10년 전 | 1013 | ||
| 7617 | 10년 전 | 721 | ||
| 7616 | 10년 전 | 632 | ||
| 7615 | 10년 전 | 725 | ||
| 7614 | 10년 전 | 1253 | ||
| 7613 |
|
10년 전 | 2081 | |
| 7612 | 10년 전 | 1136 | ||
| 7611 | 10년 전 | 1405 | ||
| 7610 |
|
10년 전 | 1897 | |
| 7609 |
|
10년 전 | 1328 | |
| 7608 |
mwdkim
|
10년 전 | 1126 | |
| 7607 |
|
10년 전 | 1050 | |
| 7606 |
mwdkim
|
10년 전 | 3920 | |
| 7605 | 10년 전 | 685 | ||
| 7604 | 10년 전 | 1023 | ||
| 7603 | 10년 전 | 1645 | ||
| 7602 |
|
10년 전 | 1066 | |
| 7601 |
AniNest
|
10년 전 | 2783 | |
| 7600 |
port443
|
10년 전 | 1021 | |
| 7599 | 10년 전 | 942 | ||
| 7598 | 10년 전 | 1012 | ||
| 7597 | 10년 전 | 4566 | ||
| 7596 |
SeungYeon
|
10년 전 | 886 | |
| 7595 |
untitled
|
10년 전 | 2414 | |
| 7594 |
프로그래머7
|
10년 전 | 1718 | |
| 7593 |
untitled
|
10년 전 | 2357 | |
| 7592 |
untitled
|
10년 전 | 1931 | |
| 7591 |
untitled
|
10년 전 | 2673 | |
| 7590 |
아리마2001
|
10년 전 | 850 | |
| 7589 | 10년 전 | 1106 | ||
| 7588 |
|
10년 전 | 2913 | |
| 7587 | 10년 전 | 1301 | ||
| 7586 | 10년 전 | 666 | ||
| 7585 | 10년 전 | 1688 | ||
| 7584 | 10년 전 | 1407 | ||
| 7583 |
leeleeleelee
|
10년 전 | 1157 | |
| 7582 |
|
10년 전 | 1103 | |
| 7581 | 10년 전 | 1320 | ||
| 7580 | 10년 전 | 983 | ||
| 7579 |
|
10년 전 | 603 | |
| 7578 | 10년 전 | 1426 | ||
| 7577 |
|
10년 전 | 1869 | |
| 7576 | 10년 전 | 1382 | ||
| 7575 |
멋진남자임
|
10년 전 | 1459 | |
| 7574 | 10년 전 | 2114 | ||
| 7573 | 10년 전 | 3247 | ||
| 7572 | 10년 전 | 757 | ||
| 7571 |
|
10년 전 | 778 | |
| 7570 |
|
10년 전 | 1306 | |
| 7569 | 10년 전 | 1545 | ||
| 7568 |
this1mg
|
10년 전 | 1046 | |
| 7567 |
|
10년 전 | 755 | |
| 7566 | 10년 전 | 914 | ||
| 7565 |
Angel하늘
|
10년 전 | 987 | |
| 7564 |
seoldi
|
10년 전 | 1229 | |
| 7563 |
|
10년 전 | 1368 | |
| 7562 |
멋진남자임
|
10년 전 | 2076 | |
| 7561 | 10년 전 | 708 | ||
| 7560 |
leeleeleelee
|
10년 전 | 896 | |
| 7559 | 10년 전 | 5035 | ||
| 7558 |
RinaP
|
10년 전 | 774 | |
| 7557 |
|
10년 전 | 1235 | |
| 7556 | 10년 전 | 1190 | ||
| 7555 |
hyohyojj1234
|
10년 전 | 1655 | |
| 7554 | 10년 전 | 1089 | ||
| 7553 |
senseme
|
10년 전 | 1334 | |
| 7552 |
ehdltdoit
|
10년 전 | 1432 | |
| 7551 |
|
10년 전 | 1815 | |
| 7550 |
leeleeleelee
|
10년 전 | 1584 | |
| 7549 | 10년 전 | 2416 | ||
| 7548 | 10년 전 | 1835 | ||
| 7547 |
멋진남자임
|
10년 전 | 1956 | |
| 7546 | 10년 전 | 998 | ||
| 7545 |
ILMare1003
|
10년 전 | 1278 | |
| 7544 |
|
10년 전 | 1246 | |
| 7543 | 10년 전 | 881 | ||
| 7542 | 10년 전 | 653 | ||
| 7541 |
울라라라우
|
10년 전 | 860 | |
| 7540 | 10년 전 | 1596 | ||
| 7539 | 10년 전 | 926 | ||
| 7538 |
|
10년 전 | 1828 | |
| 7537 | 10년 전 | 3607 | ||
| 7536 |
Gaumi
|
10년 전 | 1406 | |
| 7535 |
프로그램은어려워
|
10년 전 | 1261 | |
| 7534 |
senseme
|
10년 전 | 1202 | |
| 7533 | 10년 전 | 1190 | ||
| 7532 | 10년 전 | 854 | ||
| 7531 | 10년 전 | 2044 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기