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 的基本用法,可以:

Released under the Apache 2.0 License.