Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add blur to image #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions bin/lqip-modern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env node

const [, , file, ending = 'webp', size = '16', blur = 'false'] = process.argv
const lqip = require('../index')
const fs = require('node:fs')

lqip(file, {
outputFormat: ending,
resize: +size,
blur: blur === 'true' || blur === 'false' ? Boolean(blur) : +blur
}).then((result) => {
console.log(result.metadata.dataURIBase64)
fs.writeFileSync(file.split('.')[0] + '.' + ending, result.content)
})
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const pMap = require('p-map')
* @param {number} [opts.concurrency=4] - Concurrency when processing an array of input images.
* @param {string} [opts.outputFormat='webp'] - Output format to use; either `webp` or `jpeg` (passing `jpg` is the same as passing `jpeg`).
* @param {Object} [opts.outputOptions] - Output options passed to either `sharp.webp` or `sharp.jpeg` dependent on `opts.outputFormat`.
* @param {number|boolean} [opts.blur] - false to disable blur
* @param {number|any[]} [opts.resize] - Options to pass to `sharp.resize`. Defaults to resizing inputs to a max dimension of `16`, with the other dimension being calculated to maintain aspect ratio. If you want more control, you can pass an array of args here which will be forwarded to `sharp.resize`.
*/
module.exports = async function lqipModern(input, opts = {}) {
Expand All @@ -27,9 +28,9 @@ module.exports = async function lqipModern(input, opts = {}) {
}

async function computeLqipImage(input, opts = {}) {
const { resize = 16, outputFormat = 'webp', outputOptions } = opts
const { resize = 16, outputFormat = 'webp', outputOptions, blur = 1 } = opts

const image = sharp(input).rotate()
const image = sharp(input).blur(blur).rotate()
const metadata = await image.metadata()

const resized = image.resize(
Expand All @@ -41,6 +42,7 @@ async function computeLqipImage(input, opts = {}) {
{ fit: 'inside' }
])
)

let output

if (outputFormat === 'webp') {
Expand Down