如何裁剪/缩放用户图像,以便显示固定大小的缩略图而不会出现歪斜和拉伸? [英] How can I crop/scale user images so I can display fixed sized thumbnails without skewing and stretching?

查看:223
本文介绍了如何裁剪/缩放用户图像,以便显示固定大小的缩略图而不会出现歪斜和拉伸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正致力于允许用户为我的网站上传个人资料照片。我试图避免的典型例子是 plentyoffish.com ,其中每个用户的图像都是偏斜的,看起来很丑陋:

I'm working on allowing users to upload profile pictures for my site. The classic example of what I'm trying to avoid is plentyoffish.com where each users image is skewed and looks very ugly:

那么,如何在没有上述演示偏差的情况下逐步裁剪/创建标准尺寸版本的图像?

So, how can I progmatically crop/create standard sized versions of an image without the skewing demonstrated above?

推荐答案

好吧,你必须有一个最大的高度和宽度,让我们假设你有的图像大小是正方形,比如100x100。

Well, you must have a maximum height and width, lets assume the image size you have available is square, say 100x100.

当用户上传图像时,得到的尺寸为然后计算出哪个更大,更高或更宽。

When a user uploads an image get the dimensions of it, then work out which is greater, the height or the width.

然后进行最大测量并获得该测量值与目标测量值的比率,然后使用高度和宽度的比例。

Then take the greatest measurement and get the ratio of that measurement to your target measurement, then use that ratio to scale both the height and width.

因此,如果用户上传500高度和450宽度的图片,由于高度最大,您将100除以50 0,你的缩略图大小。这给我们.2作为比率。这意味着宽度将变为90,因此您将缩小到100x90,并且不会发生扭曲。

So if the user uploads a picture of 500 height and 450 width, as the height is greatest you'd divide 100 by 500, your thumbnail size. This gives us .2 as the ratio. which means the width will become 90, so you would shrink to 100x90, and no distortion would occur.

这篇关于如何裁剪/缩放用户图像,以便显示固定大小的缩略图而不会出现歪斜和拉伸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆