Jimp: get origin file size

Created on 25 Jun 2015  路  5Comments  路  Source: oliver-moran/jimp

Hi

Is there a way to know the image size in entry?
I need it to do a resize without scale lose.

Most helpful comment

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
});

All 5 comments

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'}]
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

dtrofimov picture dtrofimov  路  5Comments

slidenerd picture slidenerd  路  4Comments

sesirimarco picture sesirimarco  路  3Comments

alyyousuf7 picture alyyousuf7  路  3Comments

Favna picture Favna  路  4Comments