我正在寻找某种功能,可以将图像大小调整为 100x100、200x200 等,但保持上传图像的纵横比。
我的计划是使用某种裁剪,以便在图像的高度或宽度达到 100 后在图像中心进行裁剪。
所以我的逻辑有点像这样。
if ( $currentHeight > $currentWidth ) {
//resize width to 100 and crop top and bottom off so it is 100 high
}
elseif ( $currentWidth > $currentHeight ) {
//resize height to 100 and crop left and right off so it is 100 wide
}
在我看来,图像将是 100x100,看起来并不奇怪!
有谁知道我如何在整洁的功能中做到这一点?
您还可以使用纯 css 裁剪图像以适应特定尺寸 clip:rect:
<style>
/*Size of div that contains the dox*/
.crop{
float:left;
position:relative;
width:100px;
height:100px;
margin:0 auto;
border: 2px solid #474747;
}
.crop img{
margin:0;
position:absolute;
top:-25px;
left:-25px;
/* (Top,Right,Bottom,Left) */
clip:rect(25px 125px 125px 25px);
}
</style>
<div class="crop">
<img src="http://static.php.net/www.php.net/images/php.gif" width="200" title="" alt="" />
<div>
<br/>
<div class="crop">
<img src="http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=5" title="" alt="" />
<div>
查看在行动
如果你想裁剪中心部分,那么我想计算坐标的逻辑可以是这样的:
1)计算作物面积的坐标:
$horizontal_center = $current_width/2;
$left = $horizontal_center - $new_width/2;
$right = $horizontal_center + $new_width/2;
2)计算裁剪面积的垂直坐标:
$vertical_center = $current_height/2;
$top = $vertical_center - $new_height/2;
$bottom = $vertical_center + $new_height/2;
这是我
的答案 - 我写了一个imagemagick/php函数来做到这一点:
stackoverflow.com/questions/20927238/