标签:eal The save info lis ges lse name sam
/** * Function createThumb * 生成缩略图 * @param $srcImg string 源图地址 * @param $maxWidth int 缩略图最大宽度 * @param $maxHeight int 缩略图最大高度 */ function createThumb($srcImg,$maxWidth="200",$maxHeight="200",$is_list = true){ //$srcImg = $srcImg); if($is_list){ if(empty($srcImg))return ‘/Public/static/bootstrap/images/headpic.jpg‘ ; }else{ if(empty($srcImg))return false; } $pathinfo = pathinfo($srcImg); $extension = strtolower($pathinfo[‘extension‘])==‘jpg‘?‘jpeg‘:strtolower($pathinfo[‘extension‘]); $extName = ‘_‘.$maxWidth.‘x‘.$maxHeight.‘.‘.$extension; //$newName = $pathinfo[‘basename‘].$extName; //$srcImgFile = substr($srcImg,strpos($srcImg,‘uploadfile‘)); //获取图片位置 $srcImgFile = ROOT.$srcImg; //获取图片位置 $outSrc = $srcImgFile.$extName; //缩略图位置 //判断是否存在 if(!file_exists($srcImgFile)){ return $srcImg ; }elseif(file_exists($outSrc)){ $srcImgName = $srcImg.$extName; return $srcImgName ; }else{ // $arr = array(); list($souWidth,$souHeight,$type,$arr[]) = getimagesize($srcImgFile); //获取图像信息 $newImg = imagecreatetruecolor($maxWidth,$maxHeight); //缩略新建图像 $bgColor = imagecolorallocate($newImg,255,255,255); //背景色 imagefill($newImg,0,0,$bgColor); //填充背景色 $imagecreatefromN = $extension==‘bmp‘?‘imagecreatefromw‘.$extension:‘imagecreatefrom‘.$extension; $sourceImg = $imagecreatefromN($srcImgFile); //打开图像 $maxScale = $maxWidth / $maxHeight; //计算目标图像的宽高比 $souScale = $souWidth / $souHeight; //计算源图像的宽高比 $dstOffsetH = 0 ; //预设高度偏移量 $dstOffsetW = 0 ; //预设宽度偏移量 $souOffsetH = 0 ; //源图高度偏移量 $souOffsetW = 0 ; //源图宽度偏移量 if($souScale > $maxScale){ //源图比例较目标图比例宽 if($souWidth > $maxWidth && $souHeight > $maxHeight){ $srcWidth = $souHeight * ($maxWidth / $maxHeight); $srcHeight = $souHeight ; $dstWidth = $maxWidth ; $dstHeight = $maxHeight ; $souOffsetW = ($souWidth - $srcWidth)/2; //计算偏移量 }elseif($souWidth > $maxWidth && $souHeight < $maxHeight){ $dstOffsetH = ($maxHeight - $souHeight) / 2 ; //计算高度偏移量,以调整居中 $srcWidth = $maxWidth ; $srcHeight = $souHeight ; $dstWidth = $maxWidth ; $dstHeight = $souHeight ; $souOffsetW = ($souWidth - $srcWidth)/2; //计算偏移量 }elseif($souWidth < $maxWidth && $souHeight < $maxHeight){ $dstOffsetH = ($maxHeight - $souHeight) / 2 ; //计算高度偏移量,以调整居中 $dstOffsetW = ($maxWidth - $souWidth) / 2 ; //计算宽度偏移量,以调整居中 $srcWidth = $souWidth ; $srcHeight = $souHeight ; $dstWidth = $souWidth ; $dstHeight = $souHeight ; } }elseif($souScale < $maxScale){ //源图比例较目标图比例高 if($souWidth > $maxWidth && $souHeight > $maxHeight){ $srcWidth = $souWidth ; $srcHeight = $souWidth * ($maxHeight / $maxWidth); $dstWidth = $maxWidth ; $dstHeight = $maxHeight ; $souOffsetH = ($souHeight - $srcHeight)/2; //计算偏移量 }elseif($souWidth < $maxWidth && $souHeight > $maxHeight){ $dstOffsetW = ($maxWidth - $souWidth) / 2 ; //计算宽度偏移量,以调整居中 $srcWidth = $souWidth ; $srcHeight = $maxHeight ; $dstWidth = $souWidth ; $dstHeight = $maxHeight ; $souOffsetH = ($souHeight - $srcHeight)/2; //计算偏移量 }elseif($souWidth < $maxWidth && $souHeight < $maxHeight){ $dstOffsetH = ($maxHeight - $souHeight) / 2 ; //计算高度偏移量,以调整居中 $dstOffsetW = ($maxWidth - $souWidth) / 2 ; //计算宽度偏移量,以调整居中 $srcWidth = $souWidth ; $srcHeight = $souHeight ; $dstWidth = $souWidth ; $dstHeight = $souHeight ; } }else{ //源图比例和目标图比例一样 if($souWidth > $maxWidth && $souHeight > $maxHeight){ $srcWidth = $souWidth ; $srcHeight = $souHeight ; $dstWidth = $maxWidth ; $dstHeight = $maxHeight ; }else{ $dstOffsetH = ($maxHeight - $souHeight) / 2 ; //计算高度偏移量,以调整居中 $dstOffsetW = ($maxWidth - $souWidth) / 2 ; //计算宽度偏移量,以调整居中 $srcWidth = $souWidth ; $srcHeight = $souHeight ; $dstWidth = $souWidth ; $dstHeight = $souHeight ; } } if($extension == ‘png‘){ $newImg = imagecreatetruecolor($maxWidth,$maxHeight); //缩略新建图像 $bgColor = imagecolorallocatealpha($newImg,$color,$color,$color,127); //背景色 imagefill($newImg,0,0,$bgColor); //填充背景色 $sourceImg = imagecreatefrompng($srcImgFile); //打开图像 // imagecopyresampled($newImg,$sourceImg,0,0,0,0,$dstWidth,$dstHeight,$souWidth,$souHeight); //缩放图像 imagecopyresampled($newImg,$sourceImg,$dstOffsetW,$dstOffsetH,$souOffsetW,$souOffsetH,$dstWidth,$dstHeight,$srcWidth,$srcHeight); //缩放图像 imagesavealpha($newImg, true); imagepng($newImg,$outSrc); //输出图像 }else{ imagecopyresampled($newImg,$sourceImg,$dstOffsetW,$dstOffsetH,$souOffsetW,$souOffsetH,$dstWidth,$dstHeight,$srcWidth,$srcHeight); //缩放图像 $imageN = $extension==‘bmp‘?‘imagew‘.$extension:‘image‘.$extension; $imageN($newImg,$outSrc); //输出图像 imagedestroy($newImg); //释放图像 } $newSrcImg = $srcImg.$extName; return $newSrcImg ; } } /** * Function createThumbNoCut * 生成缩略图 * @param $srcImg string 源图地址 * @param $maxWidth int 缩略图最大宽度 * @param $maxHeight int 缩略图最大高度 */ function createThumbNoCut($srcImg,$maxWidth="200",$maxHeight="200",$color = 255){ $srcImgFile = ROOT.$srcImg; //获取图片位置 if(empty($srcImg)) { $srcImg = ‘/Public/static/bootstrap/images/headpic.jpg‘; $srcImgFile = ROOT.$srcImg; } if(!file_exists($srcImgFile)){ $srcImg = ‘/Public/static/bootstrap/images/headpic.jpg‘; $srcImgFile = ROOT.$srcImg; } $pathinfo = getimagesize($srcImgFile); $pathinfo[‘extension‘] = substr($pathinfo[‘mime‘],6); // var_dump($pathinfo); // var_dump($pathinfo[‘extension‘]); $extension = strtolower($pathinfo[‘extension‘])==‘jpg‘?‘jpeg‘:strtolower($pathinfo[‘extension‘]); // $extName = ‘_‘.$maxWidth.‘x‘.$maxHeight.‘.‘.$extension; // return $srcImgFile; $extName = ‘_‘.$maxWidth.‘x‘.$maxHeight.‘.‘.$extension ; $outSrc = $srcImgFile.$extName ; //缩略图位置 //$extension = ‘jpeg‘; // RETURN $pathinfo; // return $extension; //判断是否存在 if(file_exists($outSrc)){ return $srcImg.$extName; }else{ $arr = array(); list($souWidth,$souHeight,$type,$arr[]) = getimagesize($srcImgFile); //获取图像信息 $maxScale = $maxWidth / $maxHeight; //计算目标图像的宽高比 $souScale = $souWidth / $souHeight; //计算源图像的宽高比 $dstOffsetH = 0 ; //目标高度偏移量 $dstOffsetW = 0 ; //目标宽度偏移量 $souOffsetH = 0 ; //源图高度偏移量 $souOffsetW = 0 ; //源图宽度偏移量 if($souScale > $maxScale){ //源图比例较目标图比例宽 if($souWidth > $maxWidth){ $dstWidth = $maxWidth; $dstHeight = $dstWidth / $souScale ; }elseif($souWidth < $maxWidth){ $dstWidth = $souWidth ; $dstHeight = $souHeight ; $dstOffsetW = ($maxWidth - $dstWidth) / 2 ; } $dstOffsetH = ($maxHeight - $dstHeight) / 2 ; }elseif($souScale < $maxScale){ //源图比例较目标图比例高 if($souHeight > $maxHeight){ $dstHeight = $maxHeight ; $dstWidth = $dstHeight * $souScale ; }elseif($souHeight < $maxHeight){ $dstWidth = $souWidth ; $dstHeight = $souHeight ; $dstOffsetH = ($maxHeight - $dstHeight) / 2 ; } $dstOffsetW = ($maxWidth - $dstWidth) / 2 ; }else{ //源图比例和目标图比例一样 if($souWidth > $maxWidth && $souHeight > $maxHeight){ $dstWidth = $maxWidth ; $dstHeight = $maxHeight ; }elseif($souWidth = $maxWidth && $souHeight = $maxHeight){ return $srcImg ; }else{ $dstWidth = $souWidth ; $dstHeight = $souHeight ; $dstOffsetH = ($maxHeight - $dstHeight) / 2 ; $dstOffsetW = ($maxWidth - $dstWidth) / 2 ; } } if($extension == ‘png‘){ $newImg = imagecreatetruecolor($maxWidth,$maxHeight); //缩略新建图像 $bgColor = imagecolorallocatealpha($newImg,$color,$color,$color,127); //背景色 imagefill($newImg,0,0,$bgColor); //填充背景色 $sourceImg = imagecreatefrompng($srcImgFile); //打开图像 // imagecopyresampled($newImg,$sourceImg,0,0,0,0,$dstWidth,$dstHeight,$souWidth,$souHeight); //缩放图像 imagecopyresampled($newImg,$sourceImg,$dstOffsetW,$dstOffsetH,$souOffsetW,$souOffsetH,$dstWidth,$dstHeight,$souWidth,$souHeight); //缩放图像 imagesavealpha($newImg, true); imagepng($newImg,$outSrc); //输出图像 }else{ // return array($srcImgFile,$dstOffsetW,$dstOffsetH,0,0,$dstWidth,$dstHeight,$souWidth,$souHeight); $newImg = imagecreatetruecolor($maxWidth,$maxHeight); //缩略新建图像 $bgColor = imagecolorallocate($newImg,$color,$color,$color); //背景色 imagefill($newImg,0,0,$bgColor); //填充背景色 $imagecreatefromN = $extension==‘bmp‘?‘imagecreatefromw‘.$extension:‘imagecreatefrom‘.$extension; // var_dump($extension); // var_dump($imagecreatefromN);exit; $sourceImg = $imagecreatefromN($srcImgFile); //打开图像 imagecopyresampled($newImg,$sourceImg,$dstOffsetW,$dstOffsetH,$souOffsetW,$souOffsetH,$dstWidth,$dstHeight,$souWidth,$souHeight); //缩放图像 $imageN = $extension==‘bmp‘?‘imagew‘.$extension:‘image‘.$extension; $imageN($newImg,$outSrc); //输出图像 } imagedestroy($newImg); //释放图像 $newSrcImg = $srcImg.‘_‘.$maxWidth.‘x‘.$maxHeight.‘.‘.$extension; return $newSrcImg ; } }
标签:eal The save info lis ges lse name sam
原文地址:https://www.cnblogs.com/godehi/p/13087109.html