Reply to topic  [ 7 posts ] 
Resizing an image and keeping aspect ratio 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Is it just me who has done this before?

The new back-end dev has written an image download API that allows me to provide X and Y sizes to resize the image.

It was just squashing the image into the given size and screwing with the aspect ratio.

I asked him if he could keep the aspect ratio and make the image "fit" into the size I give. He seriously had no idea what I meant. I drew out a picture telling him I would always provide a perfect square (i.e. same x and y) and the image would be resized to fit in the square.

He then asked what colour I want the surrounding space in the square to be.

I then showed him the iPhone how it resizes based on the orientation but keeps the aspect ratio. Still didn't get it.

In the end I wrote out how to do it in pseudo code for him. I made a mistake though (not on purpose) which meant I was multiplying the given size by the scale instead of the image size. He told me it was done and I checked and it wasn't. I then had to debug my pseudo code to work out what was wrong and let him know so he could change his code.

Surely that is something that could have been checked by him?!

[/rant]

Anyway, would you know what I meant if I asked you to resize an image to fit a given size and keep the aspect ratio? Would you know how to do it?

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Nov 21, 2012 10:23 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:58 pm
Posts: 8767
Location: behind the sofa
Reply with quote
I would ask "what colour I want the surrounding space in the square to be" ...

If you fit the entire picture into a square, then there will be blank space unless the picture is also square. On a phone, I'd probably make that black.

The alternative is to crop the image so the smaller axis fits exactly. However, you say nothing of cropping. This is how the wallpaper on my pad works.

The final alternative is to pick something in between, cropping slightly but still having black bars. This is much like showing wide-screen films on a 4:3 screen.

_________________
jonbwfc's law: "In any forum thread someone will, no matter what the subject, mention Firefly."

When you're feeling too silly for x404, youRwired.net


Wed Nov 21, 2012 11:17 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
JJW009 wrote:
I would ask "what colour I want the surrounding space in the square to be" ...

If you fit the entire picture into a square, then there will be blank space unless the picture is also square. On a phone, I'd probably make that black.

The alternative is to crop the image so the smaller axis fits exactly. However, you say nothing of cropping. This is how the wallpaper on my pad works.

The final alternative is to pick something in between, cropping slightly but still having black bars. This is much like showing wide-screen films on a 4:3 screen.

But the square is just an abstract size. I didn't want the total image to be that size. It's just a constraint on the size of the image.

If that makes sense?

Ah, I'm not actually displaying the image from the API. I'm just storing it to the iPhone. I just don't want to download a 12MP image. So I provide a size constraint and download the resized version. That's what the API is for.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Nov 21, 2012 11:48 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
So you're wanting the image to be resized so that it's the biggest image that could fit into the given rectangular space?

E.g. If I have a square image, and a very wide and short space, the square will be resized so that it's side length is that of the shortest side of the rectangular space?

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Wed Nov 21, 2012 12:31 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
EddArmitage wrote:
So you're wanting the image to be resized so that it's the biggest image that could fit into the given rectangular space?

E.g. If I have a square image, and a very wide and short space, the square will be resized so that it's side length is that of the shortest side of the rectangular space?

Exactly :D

So if you have an image of size 1000x500 and I give you a size to resize it of 100x100.

Then the image will be 100x50 (i.e. scaled to 10% of the original size).

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Nov 21, 2012 12:46 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
I've written a standard resizing library in C# before, it's easy enough to keep the aspect ratio, you just need to know when resizing if you want to:
  • Force to dimensions X,Y - Ignore original aspect ratio
  • Border X,Y - Maintain aspect ratio but with borders (like black ones on films)
  • Cut X,Y - Maintain aspect ratio but cut the outer parts that don't fit
  • Keep ratio to x or y - Keep the aspect ratio, scale so it fits either x or y

If you have any specific questions about the dimensions/logic side of things let me know

Simple logic process with a 800:600 original and a 100x60 space:
get original ratio, calculate scale for x and y (so say 4:3 for x and 3:4 for y)
Attempt to fit object in using first x dimension, so you would get a 100x75 image if you kept x so
if image > bounds, revert to y to get a 80:60 image instead if you wanted to force the image to fit in the dimensions,
otherwise fit to x instead of fit absolute :)

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Wed Nov 21, 2012 12:50 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Yeah, I've done them before too :D

I think I was just surprised that the guy had no idea where to start with it.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Nov 21, 2012 12:54 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.