이미지 처리
CodeIgniter Image Service로 리사이즈, 크롭(fit), 회전, 워터마크를 처리합니다.
이미지 업로드 & 처리
허용 확장자:
업로드 시 자동으로 썸네일(150x150 crop)과 리사이즈(가로 800px) 버전을 생성합니다.
jpg, jpeg, png, gif, webp | 최대 크기: 5MB업로드 시 자동으로 썸네일(150x150 crop)과 리사이즈(가로 800px) 버전을 생성합니다.
| 구분 | 파일명 | 크기 (bytes) | 해상도 | 미리보기 |
|---|
1
이미지 리사이즈
// CodeIgniter Image Service 호출
$image = \Config\Services::image();
// 리사이즈: 비율 유지, 가로 800px 기준
$image->withFile($sourcePath)
->resize(800, 600, true, 'width') // (가로, 세로, 비율유지, 기준축)
->save($destPath);
// 가로/세로 중 더 큰쪽 기준으로 리사이즈
$image->withFile($source)
->resize(800, 600, true, 'auto')
->save($dest);
2
썸네일 크롭 (fit)
// 정사각형 썸네일: 중앙 기준 크롭
$image->withFile($sourcePath)
->fit(150, 150, 'center') // (가로, 세로, 위치)
->save($thumbPath);
// 위치 옵션: top-left, top, top-right, left,
// center, right, bottom-left, bottom, bottom-right
3
회전 / 좌우 반전 / 포맷 변환
// 회전 (90, 180, 270만 지원)
$image->withFile($src)->rotate(90)->save($dest);
// 좌우 반전
$image->withFile($src)->flip('horizontal')->save($dest);
// JPEG → PNG 변환 (확장자만 바꾸면 자동 변환)
$image->withFile($src)->convert(IMAGETYPE_PNG)->save($destPng);
// 품질 지정 후 저장
$image->withFile($src)
->resize(800, 600, true, 'width')
->save($dest, 80); // JPEG 품질 80
4
워터마크 (텍스트)
$image->withFile($src)
->text('© CI4 Playground', [
'color' => 'ffffff',
'opacity' => 50,
'withShadow' => true,
'hAlign' => 'right',
'vAlign' => 'bottom',
'fontSize' => 20,
'padding' => 20,
])
->save($watermarkedPath);
주요 메서드
| 메서드 | 설명 | 예시 |
|---|---|---|
withFile() | 처리할 이미지 파일을 지정 | withFile('/path/img.jpg') |
resize() | 리사이즈 (비율 유지/무시 옵션) | resize(800, 600, true, 'width') |
fit() | 지정 크기에 맞춰 크롭 (썸네일용) | fit(150, 150, 'center') |
crop() | 좌표 기반 영역 크롭 | crop(100, 100, 50, 50) |
rotate() | 이미지 회전 (90/180/270만 가능) | rotate(90) |
flip() | 이미지 반전 | flip('horizontal') |
convert() | 다른 포맷으로 변환 | convert(IMAGETYPE_PNG) |
text() | 텍스트 워터마크 | text('© 2026', [...]) |
save() | 처리 결과 저장 (품질 옵션) | save($path, 90) |
getFile() | 처리된 Image 객체 반환 | getFile() |
드라이버 안내: 기본 드라이버는
GD입니다. ImageMagick 사용 시
app/Config/Images.php의 $defaultHandler를 'imagick'로 변경하세요.