Is there a way to get width and height metadata after trim?
let transform = sharp()
.trim()
.metadata()
.then(function(metadata) {
console.log(metadata)
})
return readableStream
.pipe(transform)
This doesn't seem to work
Thanks
Hello, metadata() operates on the input but it looks like you need to generate the output.
toBuffer() provides the output dimensions via info, so try something like (untested):
const trimmer = sharp()
.trim()
.toBuffer((err, data, info) => {
console.log(info)
})
return readableStream
.pipe(trimmer)
thanks. How do I then pass the info to the next pipe?
When using Stream-based output, the data piped from a sharp instance is the (compressed) image.
The instance will emit an info event with the data you need, which can be used to update another variable in an outer scope, something like:
// Define this within a scope that writableStream can access
let trimmedInfo
const trimmer = sharp()
.trim()
.on('info', info => {
trimmedInfo = info
})
readableStream
.pipe(trimmer)
.pipe(writableStream);
thanks I mean I want to be able to do something like:
let trimmedInfo = { width: 0, height: 0 }
const trimmer = sharp()
.trim()
.on('info', info => {
trimmedInfo = info
})
.extend({ top: trimmedInfo.height, bottom: trimmedInfo.height, left: trimmedInfo.width, right: trimmedInfo.width })
.background(background)
readableStream
.pipe(trimmer)
It seems width and height is 0 how can I pass them through as they are not updated?
Thanks for the extra context. You'll need to separate this into two operations with two sharp instances, one for the trim, and a second for the extend.
thanks for the tip.
I have this operation below but it doesn't seem to update the padding_width for example into extend parameter. It only seems to persist 0
let padding_width = 0
let padding_height = 0
const transformer2 = sharp()
.trim()
.on('info', trimmed_metadata => {
console.log(height, trimmed_metadata)
padding_width = parseInt((width - trimmed_metadata.width) / 2)
padding_height = (height - trimmed_metadata.height) / 2
if(trimmed_metadata.width > width) {
padding_width = 10
}
if(padding_height >= 50) {
padding_height = parseInt(padding_height)
} else {
padding_height = 50
}
console.log('Trimmed Metadata ', trimmed_metadata)
console.log('Original Metadata ', original_metadata)
if(trimmed_metadata.height == original_metadata.height) {
padding_height = 0
}
console.log(padding_height, padding_width)
})
const transformer3 = sharp()
.extend({ top: padding_height, bottom: padding_height, left: padding_width, right: padding_width })
.background(background)
Thank you for even more context. In the above example, padding_width has a value of 0 when extend() is called on transformer3.
You'll probably want to defer this assignment, something like:
const transformer3 = sharp()
.background(background)
const transformer2 = sharp()
.trim()
.on('info', trimmed_metadata => {
...
transformer3.extend({ ... })
})
thank you it works beautifully!
Most helpful comment
Thank you for even more context. In the above example,
padding_widthhas a value of0whenextend()is called ontransformer3.You'll probably want to defer this assignment, something like: