压缩图片类库

原创
2016/11/18 17:29
阅读数 43
<?php 
class RarImg{


/**
 * king 2016/11/18
 * [getNewSize 获取最终压缩尺寸]
 * @param  [type] $maxWidth  [目标宽度]
 * @param  [type] $maxHeight [目标高度]
 * @param  [type] $srcWith   [原始图片长度]
 * @param  [type] $srcHeight [原始图片宽度]
 * @return [array]            [返回目标高度和宽度]
 */
	public function getNewSize($maxWidth,$maxHeight,$srcWith,$srcHeight){


		if($maxWidth>$srcWith){

			$maxWidth=$maxWidth;

			if($srcHeight>$maxHeight){

				$maxHeight=($srcHeight/$maxHeight)*$maxWidth;
			}else{

				$maxHeight=$srcHeight;
			}

			return array('width'=>$maxWidth,'height'=>$maxHeight);
		}

		if($maxHeight>$srcHeight){


			$maxHeight=$maxHeight;

			if($srcWith>$maxWidth){

				$maxWidth=($srcWith/$maxWidth)*$maxHeight;
			}else{

				$maxWidth=$srcWith;
			}

			return array('width'=>$maxWidth,'height'=>$maxHeight);
		}


			return array('width'=>$maxWidth,'height'=>$maxHeight);

}

/**
 * [makeThumb 压缩图片函数]
 * @param  [type] $srcFile   [原始图片路径]
 * @param  [type] $dstFile   [目标图片路径]
 * @param  [type] $maxWidth  [压缩后的宽度]
 * @param  [type] $maxHeight [压缩后的高度]
 * @return [type]            [description]
 */

public function makeThumb($srcFile,$dstFile,$maxWidth,$maxHeight){


 //原始图片尺寸和类型
			if($size=getimagesize($srcFile)){


				$srcWith=$size[0];
				$srcHeight=$size[1];
				$mime=$size['mime'];
				switch ($mime) {
					  case 'image/jpeg':
						$isJpeg=true;
						break;
						case 'image/gif':
						$isGif=true;
						break;
						case 'image/png':
						$isPng=true;
						break;
				    	default:
						return false;
						break;
				}





			$arr=$this->getNewSize($maxWidth,$maxHeight,$srcWith,$srcHeight);
			$thumbWidth=$arr['width'];
			$thumbHeight=$arr['height'];

		

			if(isset($isJpeg)&&$isJpeg){


				$dstThumPic=imagecreatetruecolor($thumbWidth,$thumbHeight);
				$srcPic=imagecreatefromjpeg($srcFile);
				imagecopyresampled($dstThumPic, $srcPic, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $srcHeight, $srcHeight);

				imagejpeg($dstThumPic,$dstFile,100);

				imagedestroy($dstThumPic);
				imagedestroy($srcPic);

				return true;

			}elseif(isset($isGif)&&$isGif){
				$dstThumPic=imagecreatetruecolor($thumbWidth,$thumbHeight);
				$srcPic=imagecreatefromgif($srcFile);

				//创建透明画布
				imagealphablending($dstThumPic, true);
				imagesavealpha($dstThumPic, true);
				$trans_color=imagecolorallocatealpha($dstThumPic, 0, 0, 0, 127);
				imagefill($dstThumPic, 0, 0, $trans_color);
				imagecopyresampled($dstThumPic, $srcPic, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $srcHeight, $srcHeight);

				imagegif($dstThumPic,$dstFile);

				imagedestroy($dstThumPic);
				imagedestroy($srcPic);

				return true;




			}elseif(isset($isPng)&&$isPng){

				$dstThumPic=imagecreatetruecolor($thumbWidth,$thumbHeight);
				$srcPic=imagecreatefrompng($srcFile);


				//创建透明画布
				imagealphablending($dstThumPic, true);
				imagesavealpha($dstThumPic, true);
				$trans_color=imagecolorallocatealpha($dstThumPic, 0, 0, 0, 127);
				imagefill($dstThumPic, 0, 0, $trans_color);
				imagecopyresampled($dstThumPic, $srcPic, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $srcHeight, $srcHeight);

				imagepng($dstThumPic,$dstFile);

				imagedestroy($dstThumPic);
				imagedestroy($srcPic);

				return true;



			}else{

				return false;
			}



		}else{


			return  false;
		}



	}




}


$obj=new RarImg();

$obj->makeThumb('2.png','4.png',100,100);


 ?>
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部