如何使用PHP调整和裁剪图像?

如何使用PHP调整和裁剪图像?,php,image,resize,crop,Php,Image,Resize,Crop,我正在寻找某种功能,将图像大小调整为100x100,200x200等,但保持在机智的上传图像的纵横比排序 我的计划是使用某种裁剪,以便在图像的高度或宽度达到100后在图像的中心进行裁剪 所以我的逻辑有点像这样 if ( $currentHeight > $currentWidth ) { //resize width to 100 and crop top and bottom off so it is 100 high } elseif ( $currentWidth > $

我正在寻找某种功能,将图像大小调整为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,它看起来不会奇怪


有人知道我如何在一个整洁的函数中做到这一点吗???

如果你想裁剪中心部分,那么我想计算坐标的逻辑可以是这样的:

1) 计算作物区域的水平坐标:

$horizontal_center = $current_width/2;
$left = $horizontal_center - $new_width/2;
$right = $horizontal_center + $new_width/2;
$vertical_center = $current_height/2;
$top = $vertical_center - $new_height/2;
$bottom = $vertical_center + $new_height/2;
2) 计算作物区域的垂直坐标:

$horizontal_center = $current_width/2;
$left = $horizontal_center - $new_width/2;
$right = $horizontal_center + $new_width/2;
$vertical_center = $current_height/2;
$top = $vertical_center - $new_height/2;
$bottom = $vertical_center + $new_height/2;

您还可以使用纯css裁剪图像以适应特定的尺寸,方法是:


/*包含dox的div的大小*/
.作物{
浮动:左;
位置:相对位置;
宽度:100px;
高度:100px;
保证金:0自动;
边框:2个实心#4747;
}
.作物img{
保证金:0;
位置:绝对位置;
顶部:-25px;
左:-25px;
/*(顶部、右侧、底部、左侧)*/
剪辑:矩形(25px 125px 125px 25px);
}    


以下是我的答案-我编写了一个imagemagick/php函数来实现这一点:


您搜索过了吗?谷歌或这个网站,有数百万个例子,你可以从php手册中的图像函数开始,在我搜索了几天的可能副本中有大量的例子,所有人提供的都是调整宽度或将其延伸。我不想安装一些不可靠的库。我只想要一个简单的函数。能做到吗?