Skip to content

Wrap sharp calls in try/catch to avoid crashing on bad images #28640

@KyleAMathews

Description

@KyleAMathews

A follow up to #28614

Here are a few other places where sharp reads/writes images that don't seem to have try/catch setup around them.

const pipeline = sharp(file.absolutePath)

const duotoneWithTransparency = await duotoneImage
.joinChannel(percentTransparency, {
raw: { width: info.width, height: info.height, channels: 1 },
})
.raw()
.toBuffer()
return await originalImage
.composite([
{
input: duotoneWithTransparency,
blend: `over`,
raw: { width: info.width, height: info.height, channels: 4 },
},
])
.toBuffer({ resolveWithObject: true })
.then(({ data, info }) =>
sharp(data, {
raw: info,
}).toFormat(format, { ...options })
)

.toBuffer({ resolveWithObject: true })
.then(({ data, info }) => {
for (let i = 0; i < data.length; i = i + info.channels) {
const r = data[i + 0]
const g = data[i + 1]
const b = data[i + 2]
// @see https://en.wikipedia.org/wiki/Relative_luminance
const avg = Math.round(0.2126 * r + 0.7152 * g + 0.0722 * b)
data[i + 0] = duotoneGradient[avg][0]
data[i + 1] = duotoneGradient[avg][1]
data[i + 2] = duotoneGradient[avg][2]
}
return sharp(data, {
raw: info,
}).toFormat(format, { ...options })
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueIssue that doesn't require previous experience with Gatsbystale?Issue that may be closed soon due to the original author not responding any more.type: bugAn issue or pull request relating to a bug in Gatsby

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions