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

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

Open
liuwuqingnian opened this issue Jan 9, 2025 · 6 comments

Comments

@liuwuqingnian
Copy link

"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) => {

    })
@lovell
Copy link
Owner

lovell commented Jan 9, 2025

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.

@liuwuqingnian
Copy link
Author

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
Copy link
Author

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)
}
})()`

@lovell
Copy link
Owner

lovell commented Jan 13, 2025

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

@liuwuqingnian
Copy link
Author

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

@lovell
Copy link
Owner

lovell commented Jan 14, 2025

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 lovell changed the title Rendering Failure for an Image with Resolution 10000 x 25000 SVG does not render embedded image with a dimension greater than 10000 pixels Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants