2015-08-20
썸네일은 원본 이미지를 작게 출력하여 보여주는 것이기 때문에 원본 이미지를 작게 줄이는 기능이 필요한데, php에서는 쓰이는 방법중 2가지를 소개해 보겠다.

1. GD를 사용해서 만들기
function make_thumb($file, $thumb, $t_width,$t_height) 
{
  $source_image = imagecreatefromstring(file_get_contents($file)); //파일읽기
  $width = imagesx($source_image);
  $height = imagesy($source_image);

  $virtual_image = imagecreatetruecolor($t_width, $t_height); //가상 이미지 만들기

  imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $t_width, $t_height, $width, $height); //사이즈 변경하여 복사

  imagepng($virtual_image, $dest); // png파일로 썸네일 생성
}

make_thumb( "./test.jpg", 'copy.png', 100, 100 );
imagecreatetruecolor()함수는 GD 2.0.1버전부터 사용가능하다.

2. Imagick::resizeImage함수 사용하기
해당 서버에 Imagick이 설치되어 있다면 resizeImage()함수를 사용하여 이미지 크기를 줄일수 있다.
해당 함수의 자세한 사용법은 아래를 참조하기 바란다.
링크 : http://php.net/manual/kr/imagick.resizeimage.php