PHP壓縮圖片函數

<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">/** 圖片壓縮函數</span>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"> * @param $orgin_file 原始圖片文件</span>
 * @param $maxwidth 最大寬度
 * @param $maxheight 最大高度
 * @param $name 壓縮圖片名
 * @param $filetype 圖片類型
 */
function resizeImage($orgin_file,$maxwidth,$maxheight,$name,$filetype)
{
	$im = '';
	switch ($filetype){
		case '.jpg':
			$im=imagecreatefromjpeg($orgin_file);
			break;
		case '.png':
			$im=imagecreatefrompng($orgin_file);
			break;
		case '.gif':
			$im=imagecreatefromgif($orgin_file);
			break;
		default:
			$im=imagecreatefromstring(file_get_contents($orgin_file));
	}
	$pic_width = imagesx($im);
	$pic_height = imagesy($im);

	if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
	{
		if($maxwidth && $pic_width>$maxwidth)
		{
			$widthratio = $maxwidth/$pic_width;
			$resizewidth_tag = true;
		}

		if($maxheight && $pic_height>$maxheight)
		{
			$heightratio = $maxheight/$pic_height;
			$resizeheight_tag = true;
		}

		if($resizewidth_tag && $resizeheight_tag)
		{
			if($widthratio<$heightratio)
				$ratio = $widthratio;
			else
				$ratio = $heightratio;
		}

		if($resizewidth_tag && !$resizeheight_tag)
			$ratio = $widthratio;
		if($resizeheight_tag && !$resizewidth_tag)
			$ratio = $heightratio;

		$newwidth = $pic_width * $ratio;
		$newheight = $pic_height * $ratio;

		if(function_exists("imagecopyresampled"))
		{
			$newim = imagecreatetruecolor($newwidth,$newheight);//PHP系統函數
			imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//PHP系統函數
		}
		else
		{
			$newim = imagecreate($newwidth,$newheight);
			imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
		}

		$name = $name.$filetype;
		switch ($filetype){
			case '.jpg':
				imagejpeg($newim,$name);
				break;
			case '.png':
				imagepng($newim,$name);
				break;
			case '.gif':
				imagegif($newim,$name);
				break;
			default:
				imagegd($newim,$name);
		}

		imagedestroy($newim);
	}
	else
	{
		$name = $name.$filetype;
		switch ($filetype){
			case '.jpg':
				imagejpeg($im,$name);
				break;
			case '.png':
				imagepng($im,$name);
				break;
			case '.gif':
				imagegif($im,$name);
				break;
			default:
				imagegd($im,$name);
		}
	}
}

調用方法

<span style="white-space:pre">	</span>    $dot_pos = strripos($_FILES['image']['name'],'.');
            $file_extend = substr($_FILES['image']['name'],$dot_pos);
            if(in_array($file_extend,$extend_limit) && in_array($_FILES['image']['type'],$type_limit) &&  ($_FILES["image"]["size"] < 10000000)){
                $real_file_name = getRandChar(5).time().$file_extend;
                $real_path = dirname(__FILE__);
                $sys_path = $real_path.'/myuploads/';
                $real_file_path = $sys_path.$real_file_name;
                if (move_uploaded_file($_FILES['image']['tmp_name'], $real_file_path)) {
                    $result['status'] = 1;
                    $maxwidth="600";
                    $maxheight="400";
                    $name=$real_file_path.'.thump';
                    $filetype=$file_extend;
                    resizeImage($real_file_path,$maxwidth,$maxheight,$name,$filetype);
                    $result['imgurl'] = 'myuploads/'.$real_file_name.'.thump'.$file_extend;
                } else {
                    $result['status'] = 4;
                    $result['desc'] = '圖片上傳失敗';
                }
            }else{
                $result['status'] = 3;
                $result['desc']   = '請選擇圖片文件上傳';
            }



圖片添加水印可以參考“imagecopymerge”函數,手冊很詳細。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章