php GD庫裁剪、加水印圖片處理

<?php
// 原始圖片
$img1 = 'www.jpg';
// 水印後的圖片
$img2 = 'bbbb.jpg';
// 水印
$water = 'nnn.jpg';
$img = new image ();
// 等比縮放
$res = $img->param ( $img1 )->thumb ( $img1, 40, 40, 0 );

// center center 裁剪
/* $img->param ( $img1 )->thumb ( './image/test_1.jpg', 200, 200, 1 );
// top left 裁剪
$img->param ( $img1 )->thumb ( './image/test_2.jpg', 200, 200, 2 );
// 右下角添加水印
$img->param ( $img1 )->water ( $img2, $water, 9 ); */
class image {
	// 當前圖片
	protected $img;
	// 圖像types 對應表
	protected $types = array (
			1 => 'gif',
			2 => 'jpg',
			3 => 'png',
			6 => 'bmp' 
	);
	public function __construct($img = '') {
		! $img && $this->param ( $img );
	}
	public function param($img) {
		$this->img = $img;
		return $this;
	}
	public function getImageInfo($img) {
		$info = @getimagesize ( $img );
		if (isset ( $this->types [$info [2]] )) {
			$info ['ext'] = $info ['type'] = $this->types [$info [2]];
		} else {
			$info ['ext'] = $info ['type'] = 'jpg';
		}
		$info ['type'] == 'jpg' && $info ['type'] = 'jpeg';
		$info ['size'] = @filesize ( $img );
		return $info;
	}
	// thumb(新圖地址, 寬, 高, 裁剪, 允許放大)
	public function thumb($filename, $new_w = 160, $new_h = 120, $cut = 0, $big = 0) {
		// 獲取原圖信息
		$info = $this->getImageInfo ( $this->img );
		if (! empty ( $info [0] )) {
			$old_w = $info [0];
			$old_h = $info [1];
			$type = $info ['type'];
			$ext = $info ['ext'];
			unset ( $info );
			// 如果原圖比縮略圖小 並且不允許放大
			if ($old_w < $new_h && $old_h < $new_w && ! $big) {
				return false;
			}
			// 裁剪圖片
			if ($cut == 0) { // 等比列
				$scale = min ( $new_w / $old_w, $new_h / $old_h ); // 計算縮放比例
				$width = ( int ) ($old_w * $scale); // 縮略圖尺寸
				$height = ( int ) ($old_h * $scale);
				$start_w = $start_h = 0;
				$end_w = $old_w;
				$end_h = $old_h;
			} elseif ($cut == 1) { // center center 裁剪
				$scale1 = round ( $new_w / $new_h, 2 );
				$scale2 = round ( $old_w / $old_h, 2 );
				if ($scale1 > $scale2) {
					$end_h = round ( $old_w / $scale1, 2 );
					$start_h = ($old_h - $end_h) / 2;
					$start_w = 0;
					$end_w = $old_w;
				} else {
					$end_w = round ( $old_h * $scale1, 2 );
					$start_w = ($old_w - $end_w) / 2;
					$start_h = 0;
					$end_h = $old_h;
				}
				$width = $new_w;
				$height = $new_h;
			} elseif ($cut == 2) { // left top 裁剪
				$scale1 = round ( $new_w / $new_h, 2 );
				$scale2 = round ( $old_w / $old_h, 2 );
				if ($scale1 > $scale2) {
					$end_h = round ( $old_w / $scale1, 2 );
					$end_w = $old_w;
				} else {
					$end_w = round ( $old_h * $scale1, 2 );
					$end_h = $old_h;
				}
				$start_w = 0;
				$start_h = 0;
				$width = $new_w;
				$height = $new_h;
			}
			// 載入原圖
			$createFun = 'ImageCreateFrom' . $type;
			$oldimg = $createFun ( $this->img );
			// 創建縮略圖
			if ($type != 'gif' && function_exists ( 'imagecreatetruecolor' )) {
				$newimg = imagecreatetruecolor ( $width, $height );
			} else {
				$newimg = imagecreate ( $width, $height );
			}
			// 複製圖片
			if (function_exists ( "ImageCopyResampled" )) {
				ImageCopyResampled ( $newimg, $oldimg, 0, 0, $start_w, $start_h, $width, $height, $end_w, $end_h );
			} else {
				ImageCopyResized ( $newimg, $oldimg, 0, 0, $start_w, $start_h, $width, $height, $end_w, $end_h );
			}
			// 對jpeg圖形設置隔行掃描
			$type == 'jpeg' && imageinterlace ( $newimg, 1 );
			// 生成圖片
			$imageFun = 'image' . $type;
			echo $imageFun;
			! @$imageFun ( $newimg, $filename ) && die ( '保存失敗!檢查目錄是否存在並且可寫?' );
			ImageDestroy ( $newimg );
			ImageDestroy ( $oldimg );
			return $filename;
		}
		return false;
	}
	// water(保存地址,水印圖片,水印位置,透明度)
	public function water($filename, $water, $pos = 0, $pct = 80) {
		// 加載水印圖片
		$info = $this->getImageInfo ( $water );
		if (! empty ( $info [0] )) {
			$water_w = $info [0];
			$water_h = $info [1];
			$type = $info ['type'];
			$fun = 'imagecreatefrom' . $type;
			$waterimg = $fun ( $water );
		} else {
			return false;
		}
		// 加載背景圖片
		$info = $this->getImageInfo ( $this->img );
		if (! empty ( $info [0] )) {
			$old_w = $info [0];
			$old_h = $info [1];
			$type = $info ['type'];
			$fun = 'imagecreatefrom' . $type;
			$oldimg = $fun ( $this->img );
		} else {
			return false;
		}
		// 剪切水印
		$water_w > $old_w && $water_w = $old_w;
		$water_h > $old_h && $water_h = $old_h;
		// 水印位置
		switch ($pos) {
			case 0 : // 隨機
				$posX = rand ( 0, ($old_w - $water_w) );
				$posY = rand ( 0, ($old_h - $water_h) );
				break;
			case 1 : // 1爲頂端居左
				$posX = 0;
				$posY = 0;
				break;
			case 2 : // 2爲頂端居中
				$posX = ($old_w - $water_w) / 2;
				$posY = 0;
				break;
			case 3 : // 3爲頂端居右
				$posX = $old_w - $water_w;
				$posY = 0;
				break;
			case 4 : // 4爲中部居左
				$posX = 0;
				$posY = ($old_h - $water_h) / 2;
				break;
			case 5 : // 5爲中部居中
				$posX = ($old_w - $water_w) / 2;
				$posY = ($old_h - $water_h) / 2;
				break;
			case 6 : // 6爲中部居右
				$posX = $old_w - $water_w;
				$posY = ($old_h - $water_h) / 2;
				break;
			case 7 : // 7爲底端居左
				$posX = 0;
				$posY = $old_h - $water_h;
				break;
			case 8 : // 8爲底端居中
				$posX = ($old_w - $water_w) / 2;
				$posY = $old_h - $water_h;
				break;
			case 9 : // 9爲底端居右
				$posX = $old_w - $water_w;
				$posY = $old_h - $water_h;
				break;
			default : // 隨機
				$posX = rand ( 0, ($old_w - $water_w) );
				$posY = rand ( 0, ($old_h - $water_h) );
				break;
		}
		// 設定圖像的混色模式
		imagealphablending ( $oldimg, true );
		// 添加水印
		imagecopymerge ( $oldimg, $waterimg, $posX, $posY, 0, 0, $water_w, $water_h, $pct );
		$fun = 'image' . $type;
		! @$fun ( $oldimg, $filename ) && die ( '保存失敗!檢查目錄是否存在並且可寫?' );
		imagedestroy ( $oldimg );
		imagedestroy ( $waterimg );
		return $filename;
	}
}
?>


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