sharp icon indicating copy to clipboard operation
sharp copied to clipboard

SVG does not render embedded image with a dimension greater than 10000 pixels

Open liuwuqingnian opened this issue 1 year ago • 6 comments

"After upgrading from version 0.33.4 to 0.33.5 of the sharp library, the memory leak issue has been resolved, but there are rendering failures with high-resolution images."

sharp(imgBuffer, { unlimited: true }) .toFormat(type) .withMetadata({ density: dpi, icc: colourspace } as any) .toBuffer((err, data) => {

    })

liuwuqingnian avatar Jan 09 '25 10:01 liuwuqingnian

the memory leak issue has been resolved,

I don't know what this refers to, please can you provide more details.

there are rendering failures with high-resolution images.

Please can you provide complete information that allows someone else to reproduce. This will include complete, standalone code and a sample image.

lovell avatar Jan 09 '25 11:01 lovell

When a certain Base64 is too large, the image generated by sharp will be missing this layer.

code like: `const svgString = '.. base64ImageStr1. base64ImageStr2'; const imgBuffer = Buffer.from(svgString) sharp(imgBuffer, { unlimited: true }) .toFormat(type) .withMetadata({ density: dpi, icc: colourspace } as any) .toBuffer((err, data) => {

})`

liuwuqingnian avatar Jan 09 '25 12:01 liuwuqingnian

successful svg demo failed svg demo

When size of an image in an SVG is too large, version 0.33.5 of Sharp will fail (version 0.33.4 is OK) to render that image during the conversion process. @lovell

demo code(the path should change to your local path) ` const sharp = require('sharp') const fs = require('fs')

// Example function to convert SVG buffer to PNG async function convertSvgToImage(svgBuffer, outputFormat = 'png', outputPath) { try { const imageBuffer = await sharp(svgBuffer).toFormat(outputFormat).toBuffer()

// Write the output image buffer to a file
fs.writeFileSync(outputPath, imageBuffer)
console.log(`Image successfully written to ${outputPath}`)

} catch (err) { console.error('Error converting SVG to image:', err) } }

// Example usage ;(async () => { try { // Read SVG file into a buffer const svgBuffer = fs.readFileSync('./method-draw-image.svg')

// Convert SVG buffer to PNG and save it as a file
await convertSvgToImage(Buffer.from(svgBuffer), 'png', 'output-image.png')

} catch (err) { console.error('Error:', err) } })()`

liuwuqingnian avatar Jan 13 '25 11:01 liuwuqingnian

This is probably related to the upstream librsvg issue at https://gitlab.gnome.org/GNOME/librsvg/-/issues/1093

lovell avatar Jan 13 '25 19:01 lovell

The latest release of librsvg was 9 years ago. How can this issue be fixed using Sharp? Are there any specific solutions? @lovell

liuwuqingnian avatar Jan 14 '25 07:01 liuwuqingnian

The latest release of librsvg was 9 years ago.

There's a new release of librsvg every month or so, please see https://gitlab.gnome.org/GNOME/librsvg/-/tags

The prebuilt binaries provided by sharp always use the most recent version of librsvg available at that point in time. You can view the version you are using via sharp.versions.rsvg.

Please subscribe to the upstream issue at https://gitlab.gnome.org/GNOME/librsvg/-/issues/1093 for updates about this specific problem.

lovell avatar Jan 14 '25 09:01 lovell