【问题标题】:How to make thumb and preserve its proportions如何制作拇指并保持其比例
【发布时间】:2015-12-10 22:24:52
【问题描述】:

我正在尝试制作拇指并保留其原始比例。 Thumb 的 div 应该有 width:189px 和 height 179px。但现在,拇指本身的高度更大,比例也不同了。该怎么做?

我正在尝试这个:

<div class="thumbnail image-thumbnail">
  <a href="someurl"><img src="thumb_src" class="thumb"/></a>
  </div>
.thumbnail {
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: 4px;
    display: block;
    line-height: 1.42857;
    margin-bottom: 20px;
    padding: 4px;
    transition: all 0.2s ease-in-out 0s;
}
.image-thumbnail {
    border: 0 none;
    float: left;
    margin-right: 1%;
    margin-top: 10px;
    padding: 0 !important;
    width: 19.2%;
}
.thumb {
    display: block !important;
    max-height: 178px;
    min-height: 178px;
    min-width: 100%;
  }
$max_width = 289;
$path = $_REQUEST['path'];
list($orig_width, $orig_height) = getimagesize($path);

$width = $orig_width;
$height = $orig_height;

# wider
if ($width > $max_width) {
$height = ($max_width / $width) * $height;
$width = $max_width;
}
$image = Image::factory ( $path, 'GD' );
$image->resize($width,$height)->save($tmbPath,100);

【问题讨论】:

  • PHP 文档中有很多这样的例子。

标签: php css image


【解决方案1】:

除非你真的想do this in php,否则你可以使用css并将图像的max-widthmax-height属性设置为最大尺寸,并将widthheight设置为auto保持纵横比:

img{
    max-width:189px;
    max-height:179px;
    width: auto;
    height: auto;
}

img{
    max-width:180px;
    max-height:179px;
    width: auto;
    height: auto;
}
&lt;img src="http://i.imgur.com/pszAeGh.png"/&gt;

【讨论】:

  • 在我的控制器中,当我创建拇指时,我有:if ($width &gt; $max_width) { $height = ($max_width / $width) * $height; $width = $max_width; },我尝试了你的代码,这很好,但是在我的控制器中使用它变得小于 179px。我的 div 是 height: 179px 并且有空白区域。但我在控制器中使用此代码来保留尺寸。我应该改变身高吗?
猜你喜欢
  • 2013-11-11
  • 1970-01-01
  • 2019-09-14
  • 2010-12-31
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
  • 2023-04-01
  • 2021-11-23
相关资源
最近更新 更多