Resize to megapixels

See all posts Reply

Resize to megapixels new!
by Carl Johan, 16 years, 10 months ago
Right, I want to be able to resize images easily to a certain total amount of pixels.

Let's say that my "optimal size" is something like 640x480 = 307200 pixels.

All pictures that have the same aspect ratio will be sized to 640x480 or 480x640.

But pictures with other aspects should be resized to have as many pixels (as good as possible).. for example an image that is 720x480 (= 345600 pixels, dvd size) it should be resized down to something like 679x453 = 307587 pixels.

Am I making sense?Reply
Re: Resize to megapixels new!
by Anubis, 16 years, 10 months ago
Sorry... not really to me...

Can you post an example?Reply
Re: Resize to megapixels new!
by colin, 16 years, 10 months ago
You are making sense :)

As of now, there is no way to do this. But it is a good idea, and I will try to implement it in a future version. Maybe the next release if I can find enough time.

But just one question: what happens if your original picture has a 1:50 ratio for instance? The algorithm you loosely described in your post will result in a very wide (or high) image. I suppose maybe there should be some boundaries (max ratio, or max width, or max height) so that you don't find yourself with such pictures, unless you really want to.Reply
Re: Resize to megapixels new!
by Carl Johan, 16 years, 10 months ago
I'm hacking up something here, I can send it to you when I'm done but it probably won't be too pretty :)

It would make sense to have a value like.. aspect_boundary(value1, value2) that would work together with aspect_crop and aspect_fill. they would be working like image_ratio_fill and image_ratio_crop i guess?Reply
Re: Resize to megapixels new!
by colin, 16 years, 10 months ago
Yes, please send me any code you do, regardless of the quality :)

As for the settings, it looks to me that aspect_crop and aspect_fill won't be needed, since it will size up the picture to a number of pixel, conserving the ratio. But it won't actually have to crop or fill anything since you want to keep the original ratio.

Wouldn't it be more a case for a new setting such as image_ratio_pixels, which would be an integer (the number of pixels)? That setting will resize the picture, conserving ratio, but increasing or reducing the dimensions so that the number of pixels is (approximatively) met.Reply
Re: Resize to megapixels new!
by Carl Johan, 16 years, 10 months ago
Yup, the crop/fill values would only be needed if you wanted to restrict the aspect, so you don't get extremely wide/high results.

Actually it should probably be called a "resize to area", right? since the area of a rectangle is height*width.Reply
Re: Resize to megapixels new!
by Carl Johan, 16 years, 10 months ago
I just did it and it's really not alot of code. Something like this:

$foo = new Upload($_FILES['imgfile'])

$toArea = (640*480);
$fromSize = getimagesize($_FILES['imgfile']['tmp_name']);
$fromArea = $fromSize[0] * $fromSize[1];
$areaDiff = sqrt($toArea / $fromArea);

$foo->image_x = round($fromSize[0] * $areaDiff);
$foo->image_y = round($fromSize[1] * $areaDiff);
$foo->image_resize = true;

$foo->Process('./');

sqrt does the trick!Reply
Re: Resize to megapixels new!
by colin, 16 years, 10 months ago
Thanks, I will look into that. Running a bit out of time at the moment, but I hope to be able to release something during next week.

Thanks for the contribution.Reply