php 裁剪图片并处理png图片背景变黑
作者:钓赛通
发布时间:2022-05-25
点击数:
我们在项目开发中,可能经常遇到裁剪图片。
在此记录遇到的问题。
/*TODO 图片裁剪*/
function img_cutting($file_old,$file_new,$h,$w){
$image = $file_old; // 原图
$dir = 'xxxxxx';//新地址
if(!is_dir($dir)){
mkdir($dir,0777,true);
}
$imgstream = file_get_contents($image);
$im = imagecreatefromstring($imgstream);
$x = imagesx($im);//获取图片的宽
$y = imagesy($im);//获取图片的高
// 缩略后的大小
$xx = $h;
$yy = $w;
if($x>$y){
//图片宽大于高
$sx = abs(($y-$x)/2);
$sy = 0;
$thumbw = $y;
$thumbh = $y;
} else {
//图片高大于等于宽
$sy = abs(($x-$y)/2.5);
$sx = 0;
$thumbw = $x;
$thumbh = $x;
}
$img_info= getimagesize($file_old);
if(end($img_info) == 'image/png'){
$img = imagecreatefrompng($file_old);
imagesavealpha($img,true);//这里很重要;
if(function_exists("imagecreatetruecolor")) {
$dim = imagecreatetruecolor($yy, $xx); // 创建目标图gd2
} else {
$dim = imagecreate($yy, $xx); // 创建目标图gd1
}
imagealphablending($dim,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
imagesavealpha($dim,true);//这里很重要,意思是不要丢了$thumb图像的透明色;
imageCopyreSampled ($dim,$im,0,0,$sx,$sy,$yy,$xx,$thumbw,$thumbh);
return imagepng($dim,$file_new);
}elseif(end($img_info) != 'image/gif'){
if(function_exists("imagecreatetruecolor")) {
$dim = imagecreatetruecolor($yy, $xx); // 创建目标图gd2
} else {
$dim = imagecreate($yy, $xx); // 创建目标图gd1
}
imageCopyreSampled ($dim,$im,0,0,$sx,$sy,$yy,$xx,$thumbw,$thumbh);
return imagejpeg($dim,$file_new,100);
}
以下是实用函数
//获取裁剪的头像
public function get_crop_img($filename, $x, $y, $width, $height) {
$newname = ROOT_PATH . '/Public/assets/Jcrop/pic/' . pathinfo($filename)['basename'];
//如果文件存在则删除
if (file_exists($newname)) {
chmod($newname, 0755);
unlink($newname);
}
//获取原图的信息
$info = $this->getImageInfo($filename);
//计算新生成的图片尺寸
/*$width = $width * ($info['width']/400);
$height = $height * ($info['height']/400);
$x = $x * ($info['width']/400);
$y = $y * ($info['height']/400);*/
// 缩小后的实际是800; 再计算出缩小后的实际高度
$suxiao_W = 800; //初始800
if ($info['width'] >= $suxiao_W) { // 宽度大于等于800
$suxiao_H = $suxiao_W / ($info['width'] / $info['height']);
$width = $width * ($info['width'] / $suxiao_W); // 实际
$height = ($width * 2);//$height * ($info['width']/$info['height']);
$width = ($width * 2);
$x = $x * ($info['width'] / $suxiao_W);
$x = ($x * 2);
$y = $y * ($info['height'] / $suxiao_H);
$y = ($y * 2);
} else { // 小于800
$width = $width * ($info['width'] / 400);
$height = $width;
$x = $x * ($info['width'] / 400);
$y = $y * ($info['width'] / 400);
}
/*echo "<pre>";
print_r([$info,$suxiao_W,$suxiao_H,$x,$y,$width,$height]);
echo "</pre>";
exit;*/
try {
$newimg = imagecreatetruecolor($suxiao_W, $suxiao_W); //创建新的图层
$color = imagecolorallocate($newimg, 255, 255, 255);
imagefill($newimg, 0, 0, $color);
switch ($info['type']) {
case 'gif': //gif
$img = imagecreatefromgif($filename);
break;
case 'jpg': //jpg
$img = imagecreatefromjpeg($filename);
imagecopyresampled($newimg, $img, 0, 0, $x, $y, $suxiao_W, $suxiao_W, $width, $height); //画图
imageinterlace($newimg, 1); //隔行扫描
imagejpeg($newimg, $newname, 100);
break;
case 'jpeg': //jpg
$img = imagecreatefromjpeg($filename);
imagecopyresampled($newimg, $img, 0, 0, $x, $y, $suxiao_W, $suxiao_W, $width, $height); //画图
imageinterlace($newimg, 1); //隔行扫描
imagejpeg($newimg, $newname, 100);
break;
case 'png': //png
$img = imagecreatefrompng($filename);
imagesavealpha($img,true);//这里很重要;
imagealphablending($newimg,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
imagesavealpha($newimg,true);//这里很重要,意思是不要丢了$thumb图像的透明色;
imageCopyreSampled ($newimg,$img, 0, 0, $x, $y, $suxiao_W, $suxiao_W, $width, $height);
imagepng($newimg, $newname);
break;
default:
break;
}
imagedestroy($newimg);
imagedestroy($img);
return $newname;
//$img = imagecreatefrompng($filename); //把原本的图片读进来
/**
* $dst_image:目标图象连接资源。
* $src_image:源图象连接资源。
* $dst_x:目标 X 坐标点。
* $dst_y:目标 Y 坐标点。
* $src_x:源的 X 坐标点。
* $src_y:源的 Y 坐标点。
* $dst_w:目标宽度。
* $dst_h:目标高度。
* $src_w:源图象的宽度。
* $src_h:源图象的高度。
*/
imagecopyresampled($newimg, $img, 0, 0, $x, $y, $suxiao_W, $suxiao_W, $width, $height); //画图
//imagejpeg($newimg, $newname, 100); //生成图片
switch ($info['type']) {
case 'gif': //gif
imagegif($newimg, $newname);
break;
case 'jpg': //jpg
imageinterlace($newimg, 1); //隔行扫描
imagejpeg($newimg, $newname, 100);
break;
case 'jpeg': //jpeg
imageinterlace($newimg, 1); //隔行扫描
imagejpeg($newimg, $newname, 100);
break;
case 'png': //png
imagepng($newimg, $newname, 100);
break;
default:
break;
}
imagedestroy($newimg);
imagedestroy($img);
return $newname;
} catch (\Error $error) {
returnData(402, 0, ['msg' =>$error->getMessage() ], '系统繁忙,请稍后重试');
} catch (\Exception $exception) {
returnData(402, 0, ['msg' =>$exception->getMessage() ], '系统繁忙,请稍后重试');
}
}