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

get origin file size #22

Closed
Yimiprod opened this issue Jun 25, 2015 · 5 comments
Closed

get origin file size #22

Yimiprod opened this issue Jun 25, 2015 · 5 comments

Comments

@Yimiprod
Copy link

Hi

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

@oliver-moran
Copy link
Collaborator

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

@Yimiprod
Copy link
Author

Thanks, you rocks 👍

@eservicesprovider
Copy link

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

@eservicesprovider
Copy link

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'}]

@czycha
Copy link

czycha commented Apr 9, 2018

@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'}]
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants