I need some help cropping an image in PHP (GD)
Posted
by evan
on Stack Overflow
See other posts from Stack Overflow
or by evan
Published on 2010-05-27T02:49:56Z
Indexed on
2010/05/27
2:51 UTC
Read the original article
Hit count: 311
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 losing 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?
© Stack Overflow or respective owner