2012-05-07 168 views
-2

我正在寻找某种功能,将图像的大小调整为100x100,200x200等,但保持上传图像的纵横比。如何使用PHP调整图像大小和裁剪图像?

我的计划是使用某种裁剪的,所以作物在图像的中心它的高度或宽度后达到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,它不会看起来很奇怪!

有谁知道我怎么可以做到这一点在一个整齐的功能?

+0

你搜索?谷歌或这个网站,有数以百万计的例子 – 2012-05-07 00:52:52

+0

你可以先看看PHP手册中的图像功能,有一堆例子在那里 – bumperbox

+0

http://wideimage.sourceforge.net/ – hakre

回答

0

如果要裁剪中心部分,然后我想逻辑用于计数坐标可以是这样的:

1)计数裁剪区域的horisontal坐标系下:

$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; 
1

您还可以使用纯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> 

See In Action