douji5746 2010-05-27 02:49
浏览 44

我需要帮助在PHP(GD)中裁剪图像

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

  • 写回答

1条回答 默认 最新

  • douzhi2988 2010-05-27 02:57
    关注
    • 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.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?