PHP & Others

GD 간단한 썸네일생성하기

페이지 정보

본문

우선 간단한 썸네일을 생성해보겠습니다.
준비할건.. 썸네일의 원본으로 쓸.. JPG 파일입니다.
이 파일을 test.jpg로 이름을 바꾼뒤에 다음 소스와 함께 같은 디렉토리에 업로드합니다.

-----소스 test01.php ----------------------------
<?
function thumbnail($file, $save_filename)
{
$src_img = ImageCreateFromJPEG($file); //JPG파일로부터 이미지를 읽어옵니다

$img_info = getImageSize($file);//원본이미지의 정보를 얻어옵니다
$img_width = $img_info[0];
$img_height = $img_info[1];

$dst_width = $img_width /2;
$dst_height = $img_height /2; //가로세로 사이즈를 원본이미지의 반으로 설정합니다
$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, $save_filename); //실제로 이미지파일을 생성합니다
ImageDestroy($dst_img);
ImageDestroy($src_img); //메모리상의 이미지를 삭제합니다.
}
?>
<? thumbnail("test.jpg","thumb1.jpg") ?>

원본이미지
test.jpg

축소된이미지
thumb1.jpg
-----여기까지 ---------------------------------

위의 소스는 간단한 함수의 사용순서를 보기위한 간단한 예제입니다. 아무런 변화없이 원본 이미지의 크기를 반으로 줄여 thumb1.jpg로 생성하는 소스입니다. 위의 결과를 확인하시려면 다음을 클릭하세요..
http://rubusy.com/study/thumb01/test01.php




이제 원하는 썸네일 크기를 입력받아 썸네일을 생성하는 소스를 살펴보겠습니다.
다음과 같은 파일을 생성하여 동일한 디렉토리에 업로드후 실행합니다.
-----소스 test02.php ----------------------------
<?
function thumbnail1($file, $save_filename, $max_width, $max_height)
{
$src_img = ImageCreateFromJPEG($file); //JPG파일로부터 이미지를 읽어옵니다

$img_info = getImageSize($file);//원본이미지의 정보를 얻어옵니다
$img_width = $img_info[0];
$img_height = $img_info[1];

$dst_width = $max_width;
$dst_height = $max_height; //가로세로 사이즈를 입력받은 값으로 설정합니다
$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, $save_filename); //실제로 이미지파일을 생성합니다
ImageDestroy($dst_img);
ImageDestroy($src_img);
}

function thumbnail2($file, $save_filename, $max_width, $max_height)
{
$src_img = ImageCreateFromJPEG($file); //JPG파일로부터 이미지를 읽어옵니다

$img_info = getImageSize($file);//원본이미지의 정보를 얻어옵니다
$img_width = $img_info[0];
$img_height = $img_info[1];

if(($img_width/$max_width) == ($img_height/$max_height))
{//원본과 썸네일의 가로세로비율이 같은경우
$dst_width=$max_width;
$dst_height=$max_height;
}
elseif(($img_width/$max_width) < ($img_height/$max_height))
{//세로에 기준을 둔경우
$dst_width=$max_height*($img_width/$img_height);
$dst_height=$max_height;
}
else{//가로에 기준을 둔경우
$dst_width=$max_width;
$dst_height=$max_width*($img_height/$img_width);
}//그림사이즈를 비교해 원하는 썸네일 크기이하로 가로세로 크기를 설정합니다.

$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, $save_filename); //실제로 이미지파일을 생성합니다
ImageDestroy($dst_img);
ImageDestroy($src_img);
}

function thumbnail3($file, $save_filename, $max_width, $max_height)
{
$src_img = ImageCreateFromJPEG($file); //JPG파일로부터 이미지를 읽어옵니다

$img_info = getImageSize($file);//원본이미지의 정보를 얻어옵니다
$img_width = $img_info[0];
$img_height = $img_info[1];

if(($img_width/$max_width) == ($img_height/$max_height))
{
$srcx=0;
$srcy=0;
$srcW=$img_width;
$srcH=$img_height;
}
elseif(($img_width/$max_width) < ($img_height/$max_height)) //ceil함수는 소수점을 올림합니다
{
$srcx=0;
$srcH=ceil(($img_width / $max_width) * $max_height);
$srcy=($img_height - $srcH) / 2;
$srcW=$img_width;
}
else{
$srcy=0;
$srcW=ceil(($img_height / $max_height) * $max_width);
$srcx=($img_width - $srcW) / 2;
$srcH=$img_height;
} //위의함수와는달리 센터값을 중심으로 썸네일크기에 맞게 남는부분은 잘라냅니다.

$dst_img = imagecreatetruecolor($max_width, $max_height); //타겟이미지를 생성합니다

ImageCopyResized($dst_img, $src_img, 0, 0, $srcx, $srcy, $max_width, $max_height, $srcW, $srcH); //타겟이미지에 원하는 사이즈의 이미지를 저장합니다

ImageInterlace($dst_img);
ImageJPEG($dst_img, $save_filename); //실제로 이미지파일을 생성합니다
ImageDestroy($dst_img);
ImageDestroy($src_img);
}
?>
<? thumbnail1("test.jpg","thumb2.jpg","100","150") ?>
<? thumbnail2("test.jpg","thumb3.jpg","80","150") ?>
<? thumbnail2("test.jpg","thumb4.jpg","150","80") ?>
<? thumbnail3("test.jpg","thumb5.jpg","150","80") ?>


원본이미지
test.jpg



1. thumbnail1("test.jpg","thumb2.jpg","100","150")

100*150 사이즈에 맞게 줄여진 이미지 입니다. 이때 가로세로비율과는 상관없이 100*150 사이즈로 맞춰진걸 확인할수 있습니다.
thumb2.jpg



2. thumbnail2("test.jpg","thumb3.jpg","80","150")

다음은 80*150 사이즈 이하로 줄여진 썸네일입니다. 가로세로 비율을 유지한채로 80*150 사이즈 이내로 줄입니다.

thumb3.jpg



3. thumbnail2("test.jpg","thumb4.jpg","150","80")

위와는 반대로 150*80 사이즈 이내로 줄여진 썸네일입니다.

thumb4.jpg



4. thumbnail3("test.jpg","thumb5.jpg","150","80")

마지막으로 센터값을 중심으로 가로세로비율을 유지하면서 남는부분은 잘라낸 썸네일입니다.

thumb5.jpg

-----여기까지 ---------------------------------

결과확인 http://rubusy.com/study/thumb01/test02.php

이번 소스는 좀 어려웠나요?? 썸네일을 응용하면 여러가지 다양한 기능을 만들어 낼 수 있습니다. 일단 두번째 소스에서는 이미지를 줄이는 방법을 알아봤습니다.

관련자료

등록된 댓글이 없습니다.
Today's proverb
과거를 아프게 들여다보지 말라. 그것은 다시 오지 않는다. 현재를 슬기롭게 이용하라. 그것은 그대의 것이다. 힘찬 기상으로 두려워말고 나아가 무지개를 맞으라.