window.ENTITIES={'/api/snippets/javascript/node.js%20-%20convert%20image%20to%20*.webp%20file%20(lossless%20compression)':[{"result":true,"message":null,"batch":{"type":"javascript","name":"node.js - convert image to *.webp file (lossless compression)","items":[{"id":"pVNQVp","type":"javascript","name":"Node.js - convert image to *.webp file (lossless compression)","content":"// Note: to install sharp package use: `npm install sharp`\n\n\nconst sharp = require('sharp');\n\nconst inputPath = 'input_image.png'; // e.g. download and use: https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png\nconst outputPath = 'output_image.webp';\n\nconst image = sharp(inputPath);\n\nimage.webp({\n lossless: true\n});\n\nimage.toFile(outputPath, (error, info) => {\n if (error) {\n console.error('Error!');\n } else {\n console.log('Saved!');\n }\n});\n\n\n// See also:\n//\n// WebP (lossy compression)\n// 1. https://dirask.com/snippets/Node-js-convert-image-to-webp-file-lossy-compression-1RNkR1\n// 2. https://dirask.com/snippets/Node-js-convert-image-to-webp-file-lossy-compression-j4v2VD\n//\n// WebP (lossless compression)\n// 1. https://dirask.com/snippets/Node-js-convert-image-to-webp-file-lossless-compression-DLOJZ1","source":"","author":{"id":"20wl2D","name":"Brandy-Mccabe","avatar":"1629030393771__20wl2D__w40px_h40px.png","points":754,"role":"BASIC"},"creationTime":1674301096000,"updateTime":1674465066000,"removalTime":null},{"id":"DLOJZ1","type":"javascript","name":"Node.js - convert image to *.webp file (lossless compression)","content":"// Note: to install sharp package use: `npm install sharp`\n\n\nconst sharp = require('sharp');\n\n/**\n * Converts any image to *.webp file using lossless compression.\n *\n * @param {*} inputPath path to input image file (JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF)\n * @param {*} outputPath path to output image file (WebP)\n * @param {*} callback called when file is saved or error occured\n */\nconst toWebp = (inputPath, outputPath, callback) => {\n const image = sharp(inputPath);\n image.webp({\n lossless: true\n });\n image.toFile(outputPath, callback);\n};\n\n\n// Usage example:\n\ntoWebp('input_image.png', 'output_image.webp', (error, info) => {\n if (error) {\n console.error('Error!');\n } else {\n console.log('Saved!');\n }\n});\n\n\n// e.g. download and use: https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png\n\n\n// See also:\n//\n// WebP (lossy compression)\n// 1. https://dirask.com/snippets/Node-js-convert-image-to-webp-file-lossy-compression-1RNkR1\n// 2. https://dirask.com/snippets/Node-js-convert-image-to-webp-file-lossy-compression-j4v2VD\n//\n// WebP (lossless compression)\n// 1. https://dirask.com/snippets/Node-js-convert-image-to-webp-file-lossless-compression-pVNQVp","source":"","author":{"id":"Eagdqo","name":"Palus-Bear","avatar":"1631264467967__Eagdqo__w40px_h40px.jpg","points":1016,"role":"BASIC"},"creationTime":1674302239000,"updateTime":1674465079000,"removalTime":null}]}}]};