php > 改变图片大小

function resizejpg($imgsrc,$imgdst,$imgwidth,$imgheight)
	{ 
		//$imgsrc jpg格式图像路径 $imgdst jpg格式图像保存文件名 $imgwidth要改变的宽度 $imgheight要改变的高度
		//取得图片的宽度,高度值
		$arr = getimagesize($imgsrc);  
		//判断图片类型
		$file = fopen($imgsrc, "rb"); 
		$bin = fread($file, 2);
		fclose($file); 
		$strInfo = @unpack("C2chars", $bin); 
		$typeCode = intval($strInfo['chars1'].$strInfo['chars2']); 
		$fileType = ''; 
		switch ($typeCode) 
		{ 
		case 255216: 
		header("Content-type: image/jpg");
		$imgsrc = imagecreatefromjpeg($imgsrc);
		break; 
		case 7173: 
		header("Content-type: image/gif");
		$imgsrc = imagecreatefromgif($imgsrc);
		break; 
		case 13780: 
		header("Content-type: image/png");
		$imgsrc = imagecreatefrompng($imgsrc);
		break; 
		default: 
		header("Content-type: image/jpg");
		$imgsrc = imagecreatefromjpeg($imgsrc);
		}                    
		
		$imgWidth = $imgwidth;
		$imgHeight = $imgheight;
		// Create image and define colors
		$image = imagecreatetruecolor($imgWidth, $imgHeight);  //创建一个彩色的底图
		imagecopyresampled($image, $imgsrc, 0, 0, 0, 0,$imgWidth,$imgHeight,$arr[0], $arr[1]);
		imagepng($image);
		imagedestroy($image);
	}
 

猜你喜欢

转载自mft.iteye.com/blog/1626038