Skip to content

빠른 시작

이 가이드는 Sharp를 빠르게 시작하고 기본 이미지 처리 작업을 배우는 데 도움이 됩니다.

기본 사용법

Sharp 가져오기

javascript
import sharp from 'sharp';

또는 CommonJS 사용:

javascript
const sharp = require('sharp');

이미지 읽기

Sharp는 다양한 입력 형식을 지원합니다:

javascript
// 파일에서 읽기
const image = sharp('input.jpg');

// Buffer에서 읽기
const image = sharp(buffer);

// Stream에서 읽기
const image = sharp(stream);

기본 작업

크기 조정

javascript
// 지정된 크기로 조정
await sharp('input.jpg')
  .resize(300, 200)
  .toFile('output.jpg');

// 종횡비 유지
await sharp('input.jpg')
  .resize(300, null) // 너비 300, 높이 자동
  .toFile('output.jpg');

// 다른 적합 모드 사용
await sharp('input.jpg')
  .resize(300, 200, {
    fit: 'cover',    // 잘라서 맞추기
    position: 'center' // 중앙 자르기
  })
  .toFile('output.jpg');

형식 변환

javascript
// JPEG로 변환
await sharp('input.png')
  .jpeg({ quality: 80 })
  .toFile('output.jpg');

// WebP로 변환
await sharp('input.jpg')
  .webp({ quality: 80 })
  .toFile('output.webp');

// PNG로 변환
await sharp('input.jpg')
  .png({ compressionLevel: 9 })
  .toFile('output.png');

자르기

javascript
// 지정된 영역 자르기
await sharp('input.jpg')
  .extract({ left: 100, top: 100, width: 200, height: 200 })
  .toFile('cropped.jpg');

회전

javascript
// 90도 회전
await sharp('input.jpg')
  .rotate(90)
  .toFile('rotated.jpg');

고급 작업

필터 효과

javascript
// 블러
await sharp('input.jpg')
  .blur(5)
  .toFile('blurred.jpg');

// 선명화
await sharp('input.jpg')
  .sharpen()
  .toFile('sharpened.jpg');

// 그레이스케일
await sharp('input.jpg')
  .grayscale()
  .toFile('grayscale.jpg');

색상 조정

javascript
// 밝기 조정
await sharp('input.jpg')
  .modulate({ brightness: 1.2 })
  .toFile('bright.jpg');

// 대비 조정
await sharp('input.jpg')
  .modulate({ contrast: 1.5 })
  .toFile('contrast.jpg');

// 채도 조정
await sharp('input.jpg')
  .modulate({ saturation: 0.8 })
  .toFile('desaturated.jpg');

출력 옵션

파일로 저장

javascript
await sharp('input.jpg')
  .resize(300, 200)
  .toFile('output.jpg');

Buffer로 출력

javascript
const buffer = await sharp('input.jpg')
  .resize(300, 200)
  .jpeg()
  .toBuffer();

Stream으로 출력

javascript
sharp('input.jpg')
  .resize(300, 200)
  .jpeg()
  .pipe(fs.createWriteStream('output.jpg'));

오류 처리

javascript
try {
  await sharp('input.jpg')
    .resize(300, 200)
    .toFile('output.jpg');
} catch (error) {
  console.error('이미지 처리 실패:', error);
}

성능 최적화

스트림 처리

javascript
// 대용량 파일 처리 시 스트림 사용
const pipeline = sharp()
  .resize(300, 200)
  .jpeg({ quality: 80 });

fs.createReadStream('large-input.jpg')
  .pipe(pipeline)
  .pipe(fs.createWriteStream('output.jpg'));

동시 처리

javascript
// 여러 이미지 동시 처리
const promises = images.map(image => 
  sharp(image)
    .resize(300, 200)
    .jpeg()
    .toBuffer()
);

const results = await Promise.all(promises);

전체 예제

javascript
import sharp from 'sharp';
import fs from 'fs';

async function processImage() {
  try {
    // 썸네일 생성
    await sharp('input.jpg')
      .resize(150, 150, { fit: 'cover' })
      .jpeg({ quality: 90 })
      .toFile('thumbnail.jpg');

    // 중간 크기 이미지 생성
    await sharp('input.jpg')
      .resize(800, 600, { fit: 'inside' })
      .webp({ quality: 80 })
      .toFile('medium.webp');

    // 대용량 이미지 생성
    await sharp('input.jpg')
      .resize(1920, 1080, { fit: 'inside' })
      .jpeg({ quality: 85 })
      .toFile('large.jpg');

    console.log('이미지 처리 완료!');
  } catch (error) {
    console.error('처리 실패:', error);
  }
}

processImage();

다음 단계

이제 Sharp의 기본 사용법을 이해했으므로 다음을 수행할 수 있습니다:

Apache 2.0 라이선스에 따라 릴리스되었습니다.