Node.js Sharp Module : Image Processing In NodeJS

Other Blogs

Blogs ❯❯ NodeJS

Image could not load

Node.js Image Processing

अगर आपको images के साथ काम करना है, जैसे उनको resize करना, crop करना, या format convert करना, तो Sharp module आपके लिए बहुत useful हो सकता है।

इस blog में हम Node.js Sharp module को detail में समझेंगे और देखेंगे कैसे आप इससे image processing tasks perform कर सकते हैं।

NodeJs sharp Module

sharp एक high-performance Node.js library है जो images को manipulate करने और process करने के लिए use होती है। यह image files को read, resize, crop, convert और compress कर सकती है।

यह library आपको real-time image transformation tasks perform करने कि सुविधा देती है, जो websites और applications में images को optimize करने के लिए perfect है।

Features Sharp Module

  • High Performance : Sharp C++ library libvips के ऊपर build कि गयी है जो fast image processing enable करती है।

  • Multiple Image Formats : JPEG, PNG, WebP, TIFF, GIF, etc. formats के साथ compatible .

  • Image Resizing : Image को various dimensions में resize कर सकती है।

  • Image Cropping : Image को custom size और coordinates पर crop कर सकती है।

  • Image Conversion : एक format से दुसरे format में convert कर सकती है।

  • Image Rotation and Flipping : Images को rotate और flip कर सकती है।

Install sharp Module

Sharp module को install करने के लिए आपको npm package manager का use करना होगा।

npm install sharp

NodeJS Image Processing Example

अब हम देखेंगे कि कैसे आप basic image processing tasks perform कर सकते हैं Sharp module के साथ।

NodeJS Image Resizing Example

const sharp = require('sharp'); // Resize an image to 300x200 sharp('input.jpg') .resize(300, 200) .toFile('output.jpg') .then(() => { console.log('Image resized successfully!'); }) .catch(err => { console.error('Error resizing image:', err); });

NodeJS Image Cropping Example

const sharp = require('sharp'); // Crop an image to a specific region sharp('input.jpg') .extract({ width: 100, height: 100, left: 50, top: 50 }) .toFile('cropped.jpg') .then(() => { console.log('Image cropped successfully!'); }) .catch(err => { console.error('Error cropping image:', err); });

NodeJS Image Conversion Example

const sharp = require('sharp'); // Convert an image to PNG format sharp('input.jpg') .toFormat('png') .toFile('output.png') .then(() => { console.log('Image converted to PNG format successfully!'); }) .catch(err => { console.error('Error converting image:', err); });

NodeJS Image Rotation Example

const sharp = require('sharp'); // Rotate an image by 90 degrees sharp('input.jpg') .rotate(90) .toFile('rotated.jpg') .then(() => { console.log('Image rotated successfully!'); }) .catch(err => { console.error('Error rotating image:', err); });

Real-world Use Cases

  • Image Optimization : Websites पे images को optimize करके load time improve करने के लिए।

  • Thumbnails Creation : Large images को small thumbnails में convert करने के लिए।

  • Image Conversion : Images को एक format से दुसरे format में convert करने के लिए।

  • Dynamic Image Rendering : Applications में dynamic image rendering के लिए।

Conclusion

sharp module Node.js applications में image processing को fast और efficient बनाता है। इस guide के through, आपने देखा कि कैसे आप basic image tasks जैसे resize, crop, convert, और rotate perform कर सकते हैं। Sharp के साथ आप real-time image transformations को efficiently handle कर सकते हैं जो modern web applications के लिए essential है।

sharp module को use करके आप अपनी websites और applications को speed और performance को optimize कर सकते हैं, जो users को better experience provide करता है।

Happy coding with Sharp and Node.js!

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 5.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    Recent Blogs

    Loading ...

    0 Comment(s) found !