图片裁剪,定位裁剪!花了一天时间测试和修改!望采纳!
图片过小时,等比拉伸,再裁剪!
图片有一边小于限定尺寸时! 放大图片到限定尺寸再裁剪!
希望老大更新到库里!包括png 裁剪时 透明背景变黑色问题!这里生成的缩略图,全部为jpg格式!
所以png裁剪,背景我设定为白色了!
本来想自己写成函数,但是发现创建的缓存目录写不进去
c4ca4238a0b923820dcc509a6f75849b
所以放弃了,贡献出来,希望能升级昨天那个居中裁剪!
<?php
/**
* 图片裁剪函数,支持指定定点裁剪和方位裁剪两种裁剪模式
* @param <string> $src_file 原图片路径
* @param <int> $new_width 裁剪后图片宽度(当宽度超过原图片宽度时,去原图片宽度)
* @param <int> $new_height 裁剪后图片高度(当宽度超过原图片宽度时,去原图片高度)
* @param <int> $type 裁剪方式,1-方位模式裁剪;0-定点模式裁剪。
* @param <int> $pos 方位模式裁剪时的起始方位(当选定点模式裁剪时,此参数不起作用)
* 1为顶端居左,2为顶端居中,3为顶端居右;
* 4为中部居左,5为中部居中,6为中部居右;
* 7为底端居左,8为底端居中,9为底端居右;
* @param <int> $start_x 起始位置X (当选定方位模式裁剪时,此参数不起作用)
* @param <int> $start_y 起始位置Y(当选定方位模式裁剪时,此参数不起作用)
* @return <string> 裁剪图片存储路径
*/
thumb('D:\123www\bb.com\static\assets\logo-web.png', '210', '140');
function thumb($src_file, $new_width, $new_height, $type = 1, $pos = 5, $start_x = 0, $start_y = 0)
{
$pathinfo = pathinfo($src_file);
//$dst_file = $pathinfo['dirname'] . '/' . $pathinfo['filename'] .'_'. $new_width . 'x' . $new_height . '.' . $pathinfo['extension'];
//$dst_file = $pathinfo['dirname'] . '/' . $pathinfo['filename'] .'_'. $new_width . 'x' . $new_height . '.jpg';
//$dst_file = $pathinfo['filename'] . '_' . $new_width . 'x' . $new_height . '.jpg';
$dst_file='D:\123www\bb.com/uploadfile/202004/thumb/c4ca4238a0b923820dcc509a6f75849b/210x140_middiy.jpg';
echo $dst_file;
if (!file_exists($dst_file)) {
if ($new_width < 1 || $new_height < 1) {
echo "params width or height error !";
return;
//exit();
}
if (!file_exists($src_file)) {
echo $src_file . " is not exists !";
return;
// exit();
}
$img_type = pathinfo($src_file, PATHINFO_EXTENSION);
$img_type = strtolower($img_type);
/* 载入图像 */
switch ($img_type) {
case 'jpg':
if (@!($src_img = imagecreatefromjpeg($src_file))) {
if (@!($src_img = imagecreatefrompng($src_file))) {
$src_img = imagecreatefromgif($src_file);
}
}
break;
case 'png':
if (@!($src_img = imagecreatefrompng($src_file))) {
if (@!($src_img = imagecreatefromjpeg($src_file))) {
$src_img = imagecreatefromgif($src_file);
}
}
break;
case 'gif':
if (@!($src_img = imagecreatefromgif($src_file))) {
if (@!($src_img = imagecreatefrompng($src_file))) {
$src_img = imagecreatefromjpeg($src_file);
}
}
break;
default:
echo "载入图像错误!";
return;
//exit();
}
/* 获取源图片的宽度和高度 */
$w = imagesx($src_img);
$h = imagesy($src_img);
$ratio_w = 1.0 * $new_width / $w;
$ratio_h = 1.0 * $new_height / $h;
$ratio = 1.0;
/* 计算剪切图片的宽度和高度 */
$mid_width = ($w < $new_width) ? $w : $new_width;
$mid_height = ($h < $new_height) ? $h : $new_height;
/* 初始化源图片剪切裁剪的起始位置坐标 */
switch ($pos * $type) {
case 1: //1为顶端居左
$start_x = 0;
$start_y = 0;
break;
case 2: //2为顶端居中
$start_x = ($w - $mid_width) / 2;
$start_y = 0;
break;
case 3: //3为顶端居右
$start_x = $w - $mid_width;
$start_y = 0;
break;
case 4: //4为中部居左
$start_x = 0;
$start_y = ($h - $mid_height) / 2;
break;
case 5: //5为中部居中
$start_x = ($w - $mid_width) / 2;
$start_y = ($h - $mid_height) / 2;
break;
case 6: //6为中部居右
$start_x = $w - $mid_width;
$start_y = ($h - $mid_height) / 2;
break;
case 7: //7为底端居左
$start_x = 0;
$start_y = $h - $mid_height;
break;
case 8: //8为底端居中
$start_x = ($w - $mid_width) / 2;
$start_y = $h - $mid_height;
break;
case 9: //9为底端居右
$start_x = $w - $mid_width;
$start_y = $h - $mid_height;
break;
default: //随机
break;
}
// 生成的图像的高宽比原来的都小,或都大 ,原则是 取大比例放大,取大比例缩小(缩小的比例就比较小了)
if (($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1)) {
//echo "都小,或都大";
$inter_img = imagecreatetruecolor($mid_width, $mid_height);
$color = imagecolorallocate($inter_img, 255, 255, 255); //2.上色
imagecolortransparent($inter_img, $color); //3.设置透明色
imagefill($inter_img, 0, 0, $color); //4.填充透明色
//var_dump($inter_img);
imagecopy($inter_img, $src_img, 0, 0, $start_x, $start_y, $mid_width, $mid_height);
// 生成一个以最大边长度为大小的是目标图像$ratio比例的临时图像
// 定义一个新的图像
$new_img = imagecreatetruecolor($new_width, $new_height);
$color = imagecolorallocate($new_img, 255, 255, 255); //2.上色
imagecolortransparent($new_img, $color); //3.设置透明色
imagefill($new_img, 0, 0, $color); //4.填充透明色
//var_dump($new_img);exit();
imagecopyresampled($new_img, $inter_img, 0, 0, 0, 0, $new_width, $new_height, $mid_width, $mid_height);
imagejpeg($new_img, $dst_file, 100);
} else {
//目标图像 的一个边大于原图,一个边小于原图 ,先放大平普图像,然后裁剪
// *******
// if( ($ratio_w < 1 && $ratio_h > 1) || ($ratio_w >1 && $ratio_h <1) ){
// *******
//echo "一个边大于原图,一个边小于原图";
$ratio = $ratio_h > $ratio_w ? $ratio_h : $ratio_w; //取比例大的那个值
echo $ratio;
// 定义一个中间的大图像,该图像的高或宽和目标图像相等,然后对原图放大
$inter_w = (int) ($w * $ratio);
$inter_h = (int) ($h * $ratio);
$inter_img = imagecreatetruecolor($inter_w, $inter_h);
$color = imagecolorallocate($inter_img, 255, 255, 255); //2.上色
imagecolortransparent($inter_img, $color); //3.设置透明色
imagefill($inter_img, 0, 0, $color); //4.填充透明色
//将原图缩放比例后裁剪
imagecopyresampled($inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h, $w, $h);
// 定义一个新的图像
$new_img = imagecreatetruecolor($new_width, $new_height);
$color = imagecolorallocate($new_img, 255, 255, 255); //2.上色
imagecolortransparent($new_img, $color); //3.设置透明色
imagefill($new_img, 0, 0, $color); //4.填充透明色
imagecopy($new_img, $inter_img, 0, 0, $start_x, $start_y, $new_width, $new_height);
imagejpeg($new_img, $dst_file, 100);
}
}
return ltrim($dst_file, '.');
}