Hi
Is there a way to know the image size in entry?
I need it to do a resize without scale lose.
Yes, you can get the width and height of an image like this:
var image = new Jimp("./path/to/image.jpg", function (err, image) {
var w = image.bitmap.width; // the width of the image
var h = image.bitmap.height; // the height of the image
});
You can also scale an image and keep its proportions like this:
var image = new Jimp("./path/to/image.jpg", function (err, image) {
image.scale( 0.5 ); // half the size of the image
});
Thanks, you rocks :+1:
How do I get the original filename or url ex
jimp.read('http://www.mysite.com/imgs/bugsbunny.png',function (err,img) {
console.log(img.src);
how do i process an array of images and return an array such as
[{'url':'http://www.mysite.com/imgs/bugsbunny.png'},{'url':'http://www.mysite.com/imgs/daffyduck.png'}]
@eservicesprovider Jimp does not store the original filename so that needs to be provided by some other means. The following code may be something like what you are asking for:
const Jimp = require("jimp");
const imagePaths = [ // Here is an array of image paths to work on
'http://www.mysite.com/imgs/bugsbunny.png',
'http://www.mysite.com/imgs/daffyduck.png'
];
// Map imagePaths to Jimp.read Promises that return { url: path } after operating on image
// Promise.all executes all the Promise functions synchronously and returns an array of final results once all Promises are resolved
Promise.all(imagePaths.map(path => (
Jimp.read(path).then(img => {
// Do something to img
return { url: path };
})
))).then(urls => {
// You now have an array of urls:
// urls = [{url: 'http://www.mysite.com/imgs/bugsbunny.png'}, {url: 'http://www.mysite.com/imgs/daffyduck.png'}]
});
Most helpful comment
Yes, you can get the width and height of an image like this:
You can also scale an image and keep its proportions like this: