根据长宽比调整图像大小php
我想要图像需要调整大小的新高度和宽度。有两个条件:
- 宽度需要在 180px(170-180) 左右,但 <; 180px(上传的图像始终> 180)
- 高度应最大为180px(上传的图像可能会或可能不会> 180)
I want the new height and width to which the image needs to be resized. There are two conditions
- Width needs to be around 180px(170-180) but < 180px (uploaded image is always > 180)
- Height should be max 180px (uploaded image may or may not be > 180)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正在为 Linux 编写程序,我建议使用 ImageMagick。与任何基于 PHP 的方法相比,它的内存效率更高,并且可能更快。几乎所有服务器都安装了它。下面的代码就可以解决问题。
它会介意纵横比。
编辑:
抱歉造成混乱。我认为以下内容应该满足您的需求。它未经测试,可能需要一点调试,如果您遇到麻烦,我可以尝试再次发布。
If you are writing program for Linux I would recommend using ImageMagick. It is more memory efficient and probably faster than any PHP based method. Almost all servers have it installed. Following code will do the trick.
It will mind the aspect ratio.
Edit:
Sorry for the confusion. I think the following should do what you are looking for. It is not tested and might need a little bit debugging, if you run into trouble I can try and post again.
您只需要几个步骤:
第一个将设置宽度为 180 所需的比例因子(根据您的评论,它总是大于 180)
第二个将检查高度是否大于 180规模。如果是,则比例将为高度/180。如果不是,则您已经达到了最大高度。
然后你还需要步骤来获取实际的宽度和高度:
考虑到你想让 imageWidth 在 170 和 180 之间,我想裁剪图像也是一种可能。如果是这种情况,您需要进行额外检查
You just need a few steps:
The first one will set the scale factor you need to make the width 180 (based on your comment that it is ALWAYS larger then 180)
The second one will check if the height will be larger then 180 with that scale. If it is, then the scale will be height/180. If its not, you already have the max height.
Then you also need steps to get the actual width and height:
Considering you want to make the imageWidth between 170 and 180 I guess cropping the image is also a possibility. If that is the case you need an additional check