开发者

I need some help cropping an image in PHP (GD)

开发者 https://www.devze.com 2023-01-01 00:14 出处:网络
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

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 (us开发者_如何学Cing 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


  • 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消