[PHP] 간단한 섬네일(thumbnai) 생성
▶ DreamPHP.com - 비혼의 조그만 세상 (http://dreamphp.com/)
아래의 이미지(예전에 벌초하러 갔다 찍은 사진)를 가지고
PHP의 Image Processing and GD 함수를 이용해 섬네일 제작하는 과정을 적어보겠습니다.
식상한 소재인가요? [웃음]

1024x768, 335 KiB … 편의상 test.jpg 이름으로 저장!
① 빈 이미지 생성
먼저 이미지 함수를 이용해 160x120 크기의 빈 이미지 파일을 만들어보겠습니다.
http://domain/blank.php 접속하면, 아래와 같은 이미지를 만날 수 있습니다.

② 원본 이미지 출력
이미지 함수를 이용해 원본 이미지를 그대로 읽어 출력해보겠습니다.
http://domain/read.php 접속하면, 위의 첫 이미지 그대로 볼 수 있습니다.
③ 원본 이미지 정보 확인
http://domain/read.php 접속하면, 아래와 같은 정보가 출력됩니다.
Array
(
[0] => 1024
[1] => 768
[2] => 2
[3] => width="1024" height="768"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
)
④ 크기 계산
큰 이미지를 작게 줄이는 방법은 많은데, 이 글에서는 지정한 크기에 맞도록 비율 조정을 하겠습니다.
http://domain/size.php 접속하면, 아래와 같은 정보가 출력됩니다.
1024x768 to 133x100
1024x768 to 100x75
1024x768 to 160x120
크기를 바꿔 다양하게 테스트 해보세요.
⑤ 섬네일 생성
위 과정을 이용해 실제 섬네일을 만들어 보겠습니다.
http://domain/size.php 접속하면, 아래와 같은 이미지가 출력됩니다.

160x120, 6.84 KiB
테스트를 위해 jpeg 타입의 이미지를 사용했는데, png와 gif 파일의 섬네일을 생성하기 위해서는
그에 맞는 함수를 사용하면 됩니다.
PNG : imagecreatefrompng(), imagepng() … header('Content-Type: image/png');
GIF : imagecreatefromgif(), imagegif() … header('Content-Type: image/gif');
Tip&Tech에 이미지 타입에 맞게 위 함수를 간단하게 사용할 수 있는 방법을 올릴 테니 참고하세요.
☞ 보러가기
아래의 이미지(예전에 벌초하러 갔다 찍은 사진)를 가지고
PHP의 Image Processing and GD 함수를 이용해 섬네일 제작하는 과정을 적어보겠습니다.
식상한 소재인가요? [웃음]
1024x768, 335 KiB … 편의상 test.jpg 이름으로 저장!
① 빈 이미지 생성
먼저 이미지 함수를 이용해 160x120 크기의 빈 이미지 파일을 만들어보겠습니다.
blank.php
<?php
$width = 160; // 넓이
$height = 120; // 높이
$image = imagecreatetruecolor($width,$height);
header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
?>
$width = 160; // 넓이
$height = 120; // 높이
$image = imagecreatetruecolor($width,$height);
header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
?>
http://domain/blank.php 접속하면, 아래와 같은 이미지를 만날 수 있습니다.
② 원본 이미지 출력
이미지 함수를 이용해 원본 이미지를 그대로 읽어 출력해보겠습니다.
read.php
<?php
$image = imagecreatefromjpeg('./test.jpg');
header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
?>
$image = imagecreatefromjpeg('./test.jpg');
header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
?>
http://domain/read.php 접속하면, 위의 첫 이미지 그대로 볼 수 있습니다.
③ 원본 이미지 정보 확인
info.php
<?php
$file = './test.jpg';
$size = getimagesize($file);
echo '<xmp>'.print_r($size,1).'</xmp>';
?>
$file = './test.jpg';
$size = getimagesize($file);
echo '<xmp>'.print_r($size,1).'</xmp>';
?>
http://domain/read.php 접속하면, 아래와 같은 정보가 출력됩니다.
Array
(
[0] => 1024
[1] => 768
[2] => 2
[3] => width="1024" height="768"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
)
④ 크기 계산
큰 이미지를 작게 줄이는 방법은 많은데, 이 글에서는 지정한 크기에 맞도록 비율 조정을 하겠습니다.
size.php
<?php
// 원본 크기, 섬네일 크기
function thumb_size($image_width,$image_height,$width,$height)
{
// 새로운 크기 계산을 위해 편하게 할당
$new_width = $image_width;
$new_height = $image_height;
// 섬네일 넓이보다 크면 높이 조정
if ( $new_width>$width ) {
$new_height = floor($new_height*$width/$new_width);
$new_width = $width;
}
// 섬네일 높이보다 크면 넓이 조정
if ( $new_height>$height ) {
$new_width = floor($new_width*$height/$new_height);
$new_height = $height;
}
echo $image_width.'x'.$image_height.' to '.$new_width.'x'.$new_height.'<br />'.PHP_EOL;
}
thumb_size(1024,768,200,100);
thumb_size(1024,768,100,200);
thumb_size(1024,768,160,120);
?>
// 원본 크기, 섬네일 크기
function thumb_size($image_width,$image_height,$width,$height)
{
// 새로운 크기 계산을 위해 편하게 할당
$new_width = $image_width;
$new_height = $image_height;
// 섬네일 넓이보다 크면 높이 조정
if ( $new_width>$width ) {
$new_height = floor($new_height*$width/$new_width);
$new_width = $width;
}
// 섬네일 높이보다 크면 넓이 조정
if ( $new_height>$height ) {
$new_width = floor($new_width*$height/$new_height);
$new_height = $height;
}
echo $image_width.'x'.$image_height.' to '.$new_width.'x'.$new_height.'<br />'.PHP_EOL;
}
thumb_size(1024,768,200,100);
thumb_size(1024,768,100,200);
thumb_size(1024,768,160,120);
?>
http://domain/size.php 접속하면, 아래와 같은 정보가 출력됩니다.
1024x768 to 133x100
1024x768 to 100x75
1024x768 to 160x120
크기를 바꿔 다양하게 테스트 해보세요.
⑤ 섬네일 생성
위 과정을 이용해 실제 섬네일을 만들어 보겠습니다.
thumbnail.php
<?php
$file = './test.jpg';
$width = 160;
$height = 120;
$size = getimagesize($file);
$new_width = $size[0];
$new_height = $size[1];
if ( $new_width>$width ) {
$new_height = floor($new_height*$width/$new_width);
$new_width = $width;
}
if ( $new_height>$height ) {
$new_width = floor($new_width*$height/$new_height);
$new_height = $height;
}
$original = imagecreatefromjpeg($file);
$thumbnail = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($thumbnail,$original,0,0,0,0,$new_width,$new_height,$size[0],$size[1]);
header('Content-Type: image/jpeg');
imagejpeg($thumbnail);
imagedestroy($thumbnail);
imagedestroy($original);
?>
$file = './test.jpg';
$width = 160;
$height = 120;
$size = getimagesize($file);
$new_width = $size[0];
$new_height = $size[1];
if ( $new_width>$width ) {
$new_height = floor($new_height*$width/$new_width);
$new_width = $width;
}
if ( $new_height>$height ) {
$new_width = floor($new_width*$height/$new_height);
$new_height = $height;
}
$original = imagecreatefromjpeg($file);
$thumbnail = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($thumbnail,$original,0,0,0,0,$new_width,$new_height,$size[0],$size[1]);
header('Content-Type: image/jpeg');
imagejpeg($thumbnail);
imagedestroy($thumbnail);
imagedestroy($original);
?>
http://domain/size.php 접속하면, 아래와 같은 이미지가 출력됩니다.
160x120, 6.84 KiB
테스트를 위해 jpeg 타입의 이미지를 사용했는데, png와 gif 파일의 섬네일을 생성하기 위해서는
그에 맞는 함수를 사용하면 됩니다.
PNG : imagecreatefrompng(), imagepng() … header('Content-Type: image/png');
GIF : imagecreatefromgif(), imagegif() … header('Content-Type: image/gif');
Tip&Tech에 이미지 타입에 맞게 위 함수를 간단하게 사용할 수 있는 방법을 올릴 테니 참고하세요.
☞ 보러가기
함께 나눈 이야기
댓글댓글 남기기