Image Processing API
Sharp ให้ฟังก์ชันการประมวลผลรูปภาพที่หลากหลาย รวมถึงการปรับขนาด การตัด การหมุน ฟิลเตอร์ และอื่นๆ
การปรับขนาด (resize)
การใช้งานพื้นฐาน
javascript
import sharp from 'sharp';
// ปรับเป็นขนาดที่ระบุ
sharp('input.jpg')
.resize(300, 200)
.toFile('output.jpg');
// ระบุเฉพาะความกว้าง ความสูงจะคำนวณอัตโนมัติ
sharp('input.jpg')
.resize(300)
.toFile('output.jpg');
// ระบุเฉพาะความสูง ความกว้างจะคำนวณอัตโนมัติ
sharp('input.jpg')
.resize(null, 200)
.toFile('output.jpg');ตัวเลือกการปรับขนาด
javascript
sharp('input.jpg')
.resize(300, 200, {
// อัลกอริทึมการปรับขนาด
kernel: sharp.kernel.lanczos3,
// ตำแหน่ง
position: 'center',
// สีพื้นหลัง
background: { r: 255, g: 255, b: 255, alpha: 1 },
// ควรรักษาอัตราส่วนกว้างยาวหรือไม่
fit: 'cover',
// ไม่ขยายหรือไม่
withoutEnlargement: true,
// ไม่ย่อหรือไม่
withoutReduction: false
})
.toFile('output.jpg');อัลกอริทึมการปรับขนาด
javascript
// อัลกอริทึมการปรับขนาดที่มี
sharp.kernel.nearest // เพื่อนบ้านที่ใกล้ที่สุด
sharp.kernel.cubic // การสอดแทรกแบบลูกบาศก์
sharp.kernel.mitchell // Mitchell-Netravali
sharp.kernel.lanczos2 // Lanczos 2-lobed
sharp.kernel.lanczos3 // Lanczos 3-lobed (ค่าเริ่มต้น)โหมดการปรับให้พอดี
javascript
// cover: รักษาอัตราส่วนกว้างยาว ตัดส่วนที่เกิน
sharp('input.jpg').resize(300, 200, { fit: 'cover' })
// contain: รักษาอัตราส่วนกว้างยาว เพิ่มพื้นหลัง
sharp('input.jpg').resize(300, 200, { fit: 'contain' })
// fill: ยืดให้เป็นขนาดที่ระบุ
sharp('input.jpg').resize(300, 200, { fit: 'fill' })
// inside: รักษาอัตราส่วนกว้างยาว ไม่เกินขนาดรูปภาพเดิม
sharp('input.jpg').resize(300, 200, { fit: 'inside' })
// outside: รักษาอัตราส่วนกว้างยาว อย่างน้อยถึงขนาดที่ระบุ
sharp('input.jpg').resize(300, 200, { fit: 'outside' })การตัด (extract)
javascript
// ตัดพื้นที่ที่ระบุ
sharp('input.jpg')
.extract({ left: 100, top: 100, width: 300, height: 200 })
.toFile('output.jpg');
// ตัดและปรับขนาด
sharp('input.jpg')
.extract({ left: 100, top: 100, width: 300, height: 200 })
.resize(150, 100)
.toFile('output.jpg');การหมุน (rotate)
javascript
// หมุน 90 องศา
sharp('input.jpg')
.rotate(90)
.toFile('output.jpg');
// หมุนและระบุสีพื้นหลัง
sharp('input.jpg')
.rotate(45, { background: { r: 255, g: 255, b: 255, alpha: 1 } })
.toFile('output.jpg');การพลิก (flip/flop)
javascript
// พลิกแนวตั้ง
sharp('input.jpg')
.flip()
.toFile('output.jpg');
// พลิกแนวนอน
sharp('input.jpg')
.flop()
.toFile('output.jpg');การเบลอ (blur)
javascript
// การเบลอแบบเกาส์
sharp('input.jpg')
.blur(5)
.toFile('output.jpg');
// การทำให้คมชัด
sharp('input.jpg')
.sharpen()
.toFile('output.jpg');
// พารามิเตอร์การทำให้คมชัดที่กำหนดเอง
sharp('input.jpg')
.sharpen({
sigma: 1,
flat: 1,
jagged: 2
})
.toFile('output.jpg');ฟิลเตอร์ (filters)
ระดับสีเทา
javascript
sharp('input.jpg')
.grayscale()
.toFile('output.jpg');การกลับสี
javascript
sharp('input.jpg')
.negate()
.toFile('output.jpg');การแก้ไขแกมมา
javascript
sharp('input.jpg')
.gamma(2.2)
.toFile('output.jpg');ความสว่าง/ความคมชัด
javascript
sharp('input.jpg')
.modulate({
brightness: 1.2, // ความสว่าง (0.1-2.0)
saturation: 0.8, // ความอิ่มตัว (0-2.0)
hue: 180 // สี (0-360)
})
.toFile('output.jpg');การทำงานกับสี
การแยกโทนสี
javascript
sharp('input.jpg')
.tint({ r: 255, g: 0, b: 0 })
.toFile('output.jpg');เมทริกซ์สี
javascript
sharp('input.jpg')
.recomb([
[0.3588, 0.7044, 0.1368],
[0.2990, 0.5870, 0.1140],
[0.0000, 0.0000, 1.0000]
])
.toFile('output.jpg');การทำงานกับช่องสัญญาณ
การแยกช่องสัญญาณ
javascript
// ดึงช่องสัญญาณสีแดง
sharp('input.jpg')
.extractChannel('red')
.toFile('red-channel.jpg');
// ดึงช่องสัญญาณทั้งหมด
const channels = await sharp('input.jpg').separate();การรวมช่องสัญญาณ
javascript
// รวมจากไฟล์ช่องสัญญาณแยก
sharp('red.jpg')
.joinChannel(['green.jpg', 'blue.jpg'])
.toFile('merged.jpg');การดำเนินการแบบรวม
javascript
// การดำเนินการแบบเชื่อมต่อ
sharp('input.jpg')
.resize(800, 600)
.rotate(90)
.blur(2)
.sharpen()
.jpeg({ quality: 80 })
.toFile('output.jpg');การเพิ่มประสิทธิภาพ
การประมวลผลแบบสตรีม
javascript
const fs = require('fs');
fs.createReadStream('input.jpg')
.pipe(sharp().resize(300, 200))
.pipe(fs.createWriteStream('output.jpg'));การประมวลผลแบบกลุ่ม
javascript
const sharp = require('sharp');
const fs = require('fs').promises;
async function processImages() {
const files = await fs.readdir('./images');
const promises = files
.filter(file => file.endsWith('.jpg'))
.map(async file => {
await sharp(`./images/${file}`)
.resize(300, 200)
.jpeg({ quality: 80 })
.toFile(`./output/${file}`);
});
await Promise.all(promises);
}การจัดการข้อผิดพลาด
javascript
try {
await sharp('input.jpg')
.resize(300, 200)
.toFile('output.jpg');
} catch (error) {
console.error('การประมวลผลรูปภาพล้มเหลว:', error.message);
}