Quick Start
This guide will help you get started with Sharp quickly, learning basic image processing operations.
Basic Usage
Import Sharp
javascript
import sharp from 'sharp';Or using CommonJS:
javascript
const sharp = require('sharp');Load Images
Sharp supports various input formats:
javascript
// Load from file
const image = sharp('input.jpg');
// Load from Buffer
const image = sharp(buffer);
// Load from Stream
const image = sharp(stream);Basic Operations
Resize
javascript
// Resize to specific dimensions
await sharp('input.jpg')
.resize(300, 200)
.toFile('output.jpg');
// Maintain aspect ratio
await sharp('input.jpg')
.resize(300, null) // Width 300, height auto
.toFile('output.jpg');
// Use different fit modes
await sharp('input.jpg')
.resize(300, 200, {
fit: 'cover', // Crop to fit
position: 'center' // Center crop
})
.toFile('output.jpg');Format Conversion
javascript
// Convert to JPEG
await sharp('input.png')
.jpeg({ quality: 80 })
.toFile('output.jpg');
// Convert to WebP
await sharp('input.jpg')
.webp({ quality: 80 })
.toFile('output.webp');
// Convert to PNG
await sharp('input.jpg')
.png({ compressionLevel: 9 })
.toFile('output.png');Crop
javascript
// Crop specific region
await sharp('input.jpg')
.extract({ left: 100, top: 100, width: 200, height: 200 })
.toFile('cropped.jpg');Rotate
javascript
// Rotate 90 degrees
await sharp('input.jpg')
.rotate(90)
.toFile('rotated.jpg');Advanced Operations
Filter Effects
javascript
// Blur
await sharp('input.jpg')
.blur(5)
.toFile('blurred.jpg');
// Sharpen
await sharp('input.jpg')
.sharpen()
.toFile('sharpened.jpg');
// Grayscale
await sharp('input.jpg')
.grayscale()
.toFile('grayscale.jpg');Color Adjustments
javascript
// Adjust brightness
await sharp('input.jpg')
.modulate({ brightness: 1.2 })
.toFile('bright.jpg');
// Adjust contrast
await sharp('input.jpg')
.modulate({ contrast: 1.5 })
.toFile('contrast.jpg');
// Adjust saturation
await sharp('input.jpg')
.modulate({ saturation: 0.8 })
.toFile('desaturated.jpg');Output Options
Save to File
javascript
await sharp('input.jpg')
.resize(300, 200)
.toFile('output.jpg');Output to Buffer
javascript
const buffer = await sharp('input.jpg')
.resize(300, 200)
.jpeg()
.toBuffer();Output to Stream
javascript
sharp('input.jpg')
.resize(300, 200)
.jpeg()
.pipe(fs.createWriteStream('output.jpg'));Error Handling
javascript
try {
await sharp('input.jpg')
.resize(300, 200)
.toFile('output.jpg');
} catch (error) {
console.error('Image processing failed:', error);
}Performance Optimization
Stream Processing
javascript
// Use streams for large files
const pipeline = sharp()
.resize(300, 200)
.jpeg({ quality: 80 });
fs.createReadStream('large-input.jpg')
.pipe(pipeline)
.pipe(fs.createWriteStream('output.jpg'));Concurrent Processing
javascript
// Process multiple images simultaneously
const promises = images.map(image =>
sharp(image)
.resize(300, 200)
.jpeg()
.toBuffer()
);
const results = await Promise.all(promises);Complete Example
javascript
import sharp from 'sharp';
import fs from 'fs';
async function processImage() {
try {
// Create thumbnail
await sharp('input.jpg')
.resize(150, 150, { fit: 'cover' })
.jpeg({ quality: 90 })
.toFile('thumbnail.jpg');
// Create medium size image
await sharp('input.jpg')
.resize(800, 600, { fit: 'inside' })
.webp({ quality: 80 })
.toFile('medium.webp');
// Create large size image
await sharp('input.jpg')
.resize(1920, 1080, { fit: 'inside' })
.jpeg({ quality: 85 })
.toFile('large.jpg');
console.log('Image processing completed!');
} catch (error) {
console.error('Processing failed:', error);
}
}
processImage();Next Steps
Now that you understand Sharp's basic usage, you can:
- Check out the API Documentation to learn all available methods
- Learn Advanced Examples to master more techniques
- Understand Performance Optimization to improve processing efficiency