tags:

views:

153

answers:

1

http://i.imgur.com/foT9u.jpg

Using that image as an example, here's what I need to do:

  • Crop the blue square to have the same proportional ratio as that of the black square
  • From doing that, I should then be able to resize the blue square to fit into the black square without stretching it - It'll retain its proportions.

Note: The blue square must be cropped 'from the center'. The original center should remain the center after the crop (it can't be cropped from the top left, for example).

Here's what I'm thinking needs to be done (using the, landscape, blue square as the example):

  • Figure out the difference between the black squares width and height
  • Figure out the difference between the blue squares width and height
  • This should tell me how much to crop the blue square by and with how much of a 'top offset'
  • Once it's cropped to fit the black squares proportions, it can then be resized

I've been messing around with code similar to:

if (BLACK_WIDTH > BLACK_HEIGHT)
{
   $diffHeight = BLACK_WIDTH - BLACK_HEIGHT;
   $newHeight  = $blue_Height - $blue_Height;

   echo $newHeight;
}

And using Photoshop to try and get a feel for how this should be done, but it continues to fail >.<

How should I go about doing this? How can I figure out how much to crop by (depending on if the blue square is landscape or portrait)? How do I then get the offset to retain the blue squares center?

EDIT: Typo

A: 
  • Find the black square width that you desired to crop. Example desired width => $x
  • Find the ratio ($r) of blue square. So after resize you will get new width => $y
  • Create a function, e.g deceaseSize function, the function will do something like a looping, loop until the $x == $y

  • Sorry, ignore my answer.

javaLearner.java
I cant delete my own answer.
javaLearner.java
Are there no better options than to loop? What if I'm starting with a large image that I want to shrink to something much smaller. Looping to shrink the image 1 pixel at a time could cause some speed issues :/
evan