图片以中心区域自动剪裁成固定大小的图片
有时候在做图片上传的过程中需要对图片进行剪裁,剪裁成我们想要的图片尺寸,如何让图片以中心区域为原点进行剪裁呢?
下面给大家提供一下计算的方法
$org_info[0]:原图宽度 $org_info[1]:原图高度 $thumb_width:新图片宽度 $thumb_height:新图片高度
$dst_x = 0;
$dst_y = 0;
$odst_x = 0;
$odst_y = 0;
if($org_info[0] < $thumb_width && $org_info[1] < $thumb_height)
{
$odst_x = intval(( $thumb_width - $org_info[0])/2);
$odst_y = intval(($thumb_height - $org_info[1] )/2);
}
elseif($org_info[0] < $thumb_width)
{
$odst_x = intval(( $thumb_width - $org_info[0])/2);
$dst_y = intval(($org_info[1] - $thumb_height)/2);
}
elseif($org_info[1] < $thumb_height)
{
$dst_x = intval(($org_info[0] - $thumb_width)/2);
$odst_y = intval(($thumb_height - $org_info[1] )/2);
}else{
$dst_x = intval(($org_info[0] - $thumb_width)/2);
$dst_y = intval(($org_info[1] - $thumb_height)/2);
}
imagecopyresized($img_thumb, $img_org,$odst_x, $odst_y, $dst_x, $dst_y, $org_info[0], $org_info[1], $org_info[0], $org_info[1]);
/* 参数详解 */
imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
$dst_image:新建的图片
$src_image:需要载入的图片
$dst_x:设定需要载入的图片在新图中的x坐标
$dst_y:设定需要载入的图片在新图中的y坐标
$src_x:设定载入图片要载入的区域x坐标
$src_y:设定载入图片要载入的区域y坐标
$dst_w:设定载入的原图的宽度(在此设置缩放)
$dst_h:设定载入的原图的高度(在此设置缩放)
$src_w:原图要载入的宽度
$src_h:原图要载入的高度
该日志未加标签。