如何限制上传图片的宽度或高度
我想以与 Pinterest 类似的方式操作/调整图像大小,但我不确定最好的方法是什么。目标是允许混合纵向和横向图像,但对最大高度和宽度施加一些限制。
我看到的问题是,如果我调整宽度,肖像图像可能会变得太薄,而风景图像则相反。
关于如何使用 PHP 实现这些结果有什么想法吗?
I would like manipulate/resize images in a similar way to Pinterest but I am not sure what is the best way to approach it. The goal is to allow a mix of both portrait and landscape images but put some restrictions on the maximum height and width.
The problem i can see is that if I resize to a width, a portrait image may become too thin, and the opposite it true for a landscape image.
Any ideas on how to achieve those sort of results with PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需要了解图像的两个边缘中哪一个较长,并按比例计算另一个尺寸。如果最大长边为 1024,那么如果两条边之一较大,您将把它设置为 1024,并计算另一条以适应比例。然后您将把这两个值传递给您的图像管理功能。
就像这里:
http://www.white-hat- web-design.co.uk/blog/resizing-images-with-php/
或者在这里:
http://www.9lessons.info/2009 /03/upload-and-resize-image-with-php.html
You just need to understand which of the two edges of the image is longer, and compute the other dimension proportionally. If the maximum long-egde is 1024, then if one of the two edges is larger you will set that to 1024, and compute the other to fit the proportions. Then you will pass those two values to your image management functions.
Like here:
http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/
Or here:
http://www.9lessons.info/2009/03/upload-and-resize-image-with-php.html
尝试用这个
欢呼
try with this
cheers
检查最大尺寸,然后根据比率调整大小。这是一个伪代码示例:
它首先检查高度,如果高度较大,则将其缩小。然后它检查宽度。如果宽度太大,它会再次缩小。最终结果,高度和宽度都将在您的范围内。它使用比率来进行缩放。
请注意,这是伪代码。实际的调整大小函数调用将取决于您的图像处理库——获取图像大小的调用也是如此。
Check for a max size and then resize based on a ratio. Here's a pseudo code example:
It first checks the height and if the height is greater, it scales it down. Then it checks the width. If the width is too big, it scales it down again. The end result, both height and width will be with in your bounds. It uses the ratio to do the scaling.
Note, this is pseudocodish. The actual resize function call will depend on your image manipulation library -- same goes for calls to obtain image size.