2011-06-16 69 views
0

所以我正在制作一个网站,其中的一部分有用户可以发表帖子并放置图像。当用户上传照片时,我希望它调整尺寸以适合邮件大小。问题是一些用户将在横向上上传,而其他用户则上传纵向。我也不知道长宽比(3:4,16:9)。我怎样才能设置我的网站呢?我知道如何调整图像大小,但是我应该只调整宽度并保持高度比例?以及如何设置我的动态HTML?动态html内容,php长宽比

所以这里有些类似于我正在做的事情...... http://fiverr.com/。

他们有一个类似的职位设置。当我把它们的图像宽度混淆时,它所做的就是将它拉伸到位。也似乎他们没有照片是在肖像视图。有人可以解释这一点,因为我怀疑没有人上传了一张肖像图像

回答

0
  old_height 
new_height= ---------- * new_width 
      old_width 

      = aspect * new_width 
0

您必须将图像的宽高比与比较你的视口的决定是由宽度或高度来调整前。

如果图像显示区域是viewport_w X viewport_h和上载的图像是w X h,然后(伪代码):

viewport_aspect_ratio = viewport_w/viewport_h; 
image_aspect_ratio = w/h; 

if (image_aspect_ratio > viewport_aspect_ratio) { 
    // image aspect ratio is wider than our viewport 
    // resize to fit viewport width 
    new_w = viewport_w; 
    new_h = image_h * viewport_w/image_w; 
} else { 
    // image aspect ratio is taller than our viewport 
    // resize to fit viewport height 
    new_w = image_w * viewport_h/image_h; 
    new_h = viewport_h; 
} 

// resize image to new_w x new_h