CI 코드

제목 업로드된 이미지 정사각형으로 썸네일 만드는 함수
글쓴이 한대승(불의회상) 작성시각 2012/01/06 15:14:43
댓글 : 3 추천 : 0 스크랩 : 0 조회수 : 24908   RSS
한대승(불의회상)
업로드된 이미지를 정사각형으로 잘라 썸네일을 만들어 주는 함수 입니다.
이미지 업로드시 관련 컨트롤러에 함수 만들어 넣으시고 호출하여 주시면 됩니다.



 Html( View)
<html>
<head>
 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<table>
<tr>
 <td>upimg</td>
 <td><input type="file" name="upimg" value="" /></td>
</tr>
<tr>
 <td colspan="2"><input type="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
upload 함수(Controller)
 /**
  * Upload한 이미지를 저장 하고 사이즈가 정사각형인 이미지로 썸네일 만든다.
  * @param String $upload_file
  * @param String $target_file
  * @param String $s
  */
 function img_upload($upload_file, $target_file, $s = 90) {
  $ret = FALSE;
  
  // 이미지 업로드 설정
  $config = array(
   'upload_path' => 'image/',
   'overwrite' => TRUE,
   'allowed_types' => 'gif|jpg|png',
   'file_name' => $target_file 
  );
  
  $this->load->library('upload', $config);
  
  if ($this->upload->do_upload($upload_file)) {
   list($w, $h) = getimagesize($config['upload_path'] . $target_file);
   $master_dim = $w > $h ? 'height' : 'width';
   
   $config = array(
    'image_library' => 'gd2',
    'source_image' => $config['upload_path'] . $target_file,
    'new_image' => $config['upload_path'] . 'thumb/'  . $target_file,
    'master_dim' => $master_dim,
    'width' => $s,
    'height' => $s
   );

   $this->load->library('image_lib', $config);
   $this->image_lib->resize();

   list($w, $h) = getimagesize($config['new_image']);
   $config = array(
    'image_library' => 'gd2',
    'source_image' => $config['new_image'],
    'maintain_ratio' => FALSE,    
    'width' => $s,
    'height' => $s,
    'y_axis' => round(($h - $s) / 2),
    'x_axis' => round(($w - $s) / 2)
   );
   
   $this->image_lib->clear();
   $this->image_lib->initialize($config);
   $this->image_lib->crop();
   
   $ret = TRUE;
  }
  
  return $ret;
 }
컨트롤러에서 함수 호출 하는 방법
$this->img_upload('upimg', 'test.jpg', 120);


 다음글 woctopus 계정관리도구 (3)
 이전글 코드 이그나이터를 접하고 처음으로 만들어본 객체 입니다... (7)

댓글

DJ구스 / 2012/01/08 21:12:48 / 추천 0
 와우 .. 역시.. 대단하시네요...

아울러 추신..
flickr와 같은 대용량 및 다량의 이미지를 처리를 할 때에는
gd2 라이브러리 

    http://www.codeigniter-kr.org/user_guide_2.1.0/images/arrow.gif); margin-top: 10px; margin-right: 0px; margin-bottom: 12px; margin-left: 0px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Geneva, sans-serif; font-size: 14px; ">
  • 이미지 크기 변경 Image Resizing
  • 썸네일 생성 Thumbnail Creation
  • 이미지 자르기 Image Cropping
  • 이미지 회전 Image Rotating
  • 이미지 워터마크(이미지위에 글자따위를 새기는것)Image Watermarking0
를 처리가 가능합니다.
기본적으로 GD/GD2, NetPBM, 그리고 ImageMagick등을 
이미지 라이브러리를 사용 가능하지만 가장 퍼포먼스 속도와 다양한 기능이 있는
iMageMagick을 이용하시면 좋구요.. 다만 간단한 처리용으로는 GD2를 권장합니다.
특히 Jpg가 아닌 gif또는 png 처리는 gd2가 속도가 훨등히 빠르구요
고용량 jpeg파일의 경우 C 라이브러리 형태로 제공하는 iMageMagick 을 강추합니다.

예전에 포토바다 하면서 속도 체크했던 샘플과 내용들이 있었는데 
어디갔는지 보이질 않아 올리진 못하지만 제 기억으로는 다량/대용량 처리 시간을 따져보면
플리커와 같이 5gb정도를 업로드 후 썸네일 처리 속도가 1분 이상 차이가 났습니다.
그만큼 CPU와 여러가지 요소가 있겠으나 속도를 줄일려고 ramdisk에 저장해서 
메모리에 상주시켜 최대한 속도 퍼포먼스를 내는데 imagicMagic이 최고였습니다.
다만 더많은 기능과(즉 플리커 같은 결과들)


변종원(웅파) / 2012/01/09 12:10:47 / 추천 0
 만들려다 귀찮아서(???) phpThumb library 사용중이었는데.. ㅎㅎ
한대승(불의회상) / 2012/01/09 18:50:32 / 추천 0
CI 뜯어 볼수록 멋진 프레임워크네요 ^^