PHP中面向對象的圖片處理類

        我們對圖片的處理主要是添加水印和等比縮放,在PHP中,封裝一個類來實現兩個功能。

源代碼如下:

<?php

/**
 *圖片處理
 */
class Image
{
	//路徑
	private $path = './upload/';
	//隨機文件名
	private $isRandName;


	//初始化成員方法
	public function __construct($path = null , $r = true)
	{
		if (!is_null($path)) {
			$this->path = rtrim($path,'/').'/';
		}
		$this->isRandName = $r;
	}

	//water水印的方法
	//源(圖片 $dst)  目標(水印 $src)  位置(9宮格) 前綴($prefix) 透明度($tmd )
	public function water($dst,$src,$pos = 9,$prefix = 'wa_', $tmd = 100)
	{
		//判斷文件路徑是否存在
		$src = $this->path . $src;
		if (!file_exists($dst) || !file_exists($src)) {
			exit('圖片或者水印不存在');
		}

		//獲取圖像(圖片和水印)的相關信息
		$dstInfo = self::getImageInfo($dst);
		$srcInfo = self::getImageInfo($src);
		//var_dump($dstInfo);
		//判斷寬高是否超過了目標圖片的寬高
		if (!$this->_checkSize($dstInfo,$srcInfo)) {
			exit('水印圖片的寬、高不合法');
		}

		//擺放位置  1 2 3 4 5 6 7 8 9 九宮格(3行3列)
		$postion = self::getPostion($dstInfo,$srcInfo,$pos);

		//打開圖片
		$dstRes = self::openImage($dst,$dstInfo);
		$srcRes = self::openImage($src,$srcInfo);

		//將兩個圖片合併在一起  通過兩張圖片信息將圖片合併在一起  需要自定義一個方法
		$newRes = $this->_mergeImage($dstRes,$srcRes,$postion,$dstInfo,$srcInfo,$tmd);

		//判斷是否允許隨機命名【保存之前】
		if ($this->isRandName) {

			//路徑 前綴 產生id .  後綴
			//uniqid() 獲取一個帶前綴、基於當前時間微秒數的唯一ID
			$path = $this->path.$prefix . uniqid(). '.' .$dstInfo['subfix'];
		} else {

			//路徑 前綴 文件原名
			$path = $this->path.$prefix . $dstInfo['basename'];
		}
		//保存圖片
		self::saveImage($newRes,$path,$dstInfo);

		//銷燬資源
		p_w_picpathdestroy($dstRes);
		p_w_picpathdestroy($srcRes);
		//返回路徑

	}

	//等比縮放
	//源圖片 寬 高 前綴
	public function thump($dst,$width,$height,$prefix = 'thump_')
	{
		//判斷文件是否存在

		if (!file_exists($dst)) {
			exit('文件路徑不存在');
		}

		//獲取圖像的信息  沒有信息就退出
		$info = self::getImageInfo($dst);
		//得到一個新的尺寸
		$newSize = self::getNewSize($width,$height,$info);
		//打開資源
		$res = self::openImage($dst,$info);
		//等比縮放這個資源  處理gif背景變黑的問題
		$newRes = self::kidOfImage($res,$newSize,$info);
		//保存
		$path = $this->path.$prefix.$info['basename'];
		self::saveImage($newRes,$path,$info);
		//銷燬資源
		p_w_picpathdestroy($newRes);
		//返回路徑
		return $path;

	}

	//等比縮放處理
	private static function kidOfImage($srcImg, $size, $imgInfo)
	{
		$newImg = p_w_picpathcreatetruecolor($size["width"], $size["height"]);
		$otsc = p_w_picpathcolortransparent($srcImg);
		if ( $otsc >= 0 && $otsc < p_w_picpathcolorstotal($srcImg)) {
			 $transparentcolor = p_w_picpathcolorsforindex( $srcImg, $otsc );
				 $newtransparentcolor = p_w_picpathcolorallocate(
				 $newImg,
				 $transparentcolor['red'],
					 $transparentcolor['green'],
				 $transparentcolor['blue']
			 );

			 p_w_picpathfill( $newImg, 0, 0, $newtransparentcolor );
			 p_w_picpathcolortransparent( $newImg, $newtransparentcolor );
		}

		p_w_picpathcopyresized( $newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"] );
		p_w_picpathdestroy($srcImg);
		return $newImg;
	}

	//得到一個新的尺寸
	private static function getNewSize($width, $height, $imgInfo)
	{
		$size["width"] = $imgInfo["width"];   //將原圖片的寬度給數組中的$size["width"]
		$size["height"] = $imgInfo["height"];  //將原圖片的高度給數組中的$size["height"]

		if($width < $imgInfo["width"]) {
			$size["width"] = $width;             //縮放的寬度如果比原圖小才重新設置寬度
		}

		if ($width < $imgInfo["height"]) {
			$size["height"] = $height;            //縮放的高度如果比原圖小才重新設置高度
		}

		if($imgInfo["width"]*$size["width"] > $imgInfo["height"] * $size["height"]) {
			$size["height"] = round($imgInfo["height"] * $size["width"] / $imgInfo["width"]);
		} else {
			$size["width"] = round($imgInfo["width"] * $size["height"] / $imgInfo["height"]);
		}

		return $size;
	}


	//獲取圖片的相關信息
	public static function getImageInfo($path)
	{
		$data = [];
		//獲取圖片大小
		$info = getp_w_picpathsize($path);
		//var_dump($info);
		//根據打印出來的信息 將鍵所對應的值(文件的大小)賦值給data的數組中
		$data['width'] = $info[0];
		$data['height'] = $info[1];
		$data['mime'] = $info['mime'];
		//獲取路徑  後綴 文件名信息
		$path = pathinfo($path);
		//var_dump($path);die;
		//根據打印出來的信息 將將鍵所對應的值(路徑和文件名)賦值給data的數組中
		$data['basename'] = $path['basename'];
		$data['subfix'] = $path['extension'];

		return $data;

	}
	//檢查圖片和水印的寬高
	//將圖片的寬高和水印的寬高進行比較
	private function _checkSize($dstInfo,$srcInfo)
	{
		//水印的寬應該小於圖片的寬度或者水印的高度應該小於圖片的高度 ,只要其中一個不滿足就不能繼續
		if ($dstInfo['width'] < $srcInfo['width'] || $dstInfo['height'] < $srcInfo['height']) {
			return false;
		}
		return true;
	}

	//位置處理
	public static function getPostion($dstInfo,$srcInfo,$pos)
	{
		switch ($pos) {
			case 1:
				$x = 0;
				$y = 0;
				break;
			case 2:
				$x = ceil(($dstInfo['width'] - $srcInfo['width']) / 2 );
				$y = 0;
				break;
			case 3:
				$x = $dstInfo['width'] - $srcInfo['width'];
				$y = 0;
				break;
			case 4:
				$x = 0;
				$y = ceil(($dstInfo['height'] - $srcInfo['height']) / 2 );
				break;
			case 5:
				$x = ceil(($dstInfo['width'] - $srcInfo['width']) / 2 );
				$y = ceil(($dstInfo['height'] - $srcInfo['height']) / 2 );
				break;
			case 6:
				$x = $dstInfo['width'] - $srcInfo['width'];
				$y = ceil(($dstInfo['height'] - $srcInfo['height']) / 2 );
				break;
			case 7:
				$x = 0;
				$y = $dstInfo['height'] - $srcInfo['height'];
				break;
			case 8:
				$x = ceil(($dstInfo['width'] - $srcInfo['width']) / 2 );
				$y = $dstInfo['height'] - $srcInfo['height'];
				break;
			case 9:
				$x = $dstInfo['width'] - $srcInfo['width'];
				$y = $dstInfo['height'] - $srcInfo['height'];
				break;
		}

		return ['x' => $x ,'y' =>$y];

	}

	//打開圖片
	//根據圖片的類型打開相應的圖片資源
	private function openImage($path,$info)
	{
		switch ($info['mime']) {
			case 'p_w_picpath/png':
			case 'p_w_picpath/x-png':
					$res = p_w_picpathcreatefrompng($path);
					break;
			case 'p_w_picpath/jpeg':
			case 'p_w_picpath/jpg':
			case 'p_w_picpath/pjpeg':

					$res = p_w_picpathcreatefromjpeg($path);
					break;
			case 'p_w_picpath/gif':
					$res = p_w_picpathcreatefromgif($path);
					break;
			case 'p_w_picpath/wbmp':
			case 'p_w_picpath/bmp':
					$res = p_w_picpathcreatefromwbmp($path);
					break;
		}
		//var_dump($res);die;
		return $res;

	}

	//合併圖片 p_w_picpathcopymerge(圖片,水印,圖片座標x,圖片座標y,水印座標x,水印座標y,透明度)
	private function _mergeImage($dstRes,$srcRes,$postion,$dstInfo,$srcInfo,$tmd)
	{

		p_w_picpathcopymerge($dstRes,$srcRes,$postion['x'],$postion['y'],0,0,$srcInfo['width'],$srcInfo['height'],$tmd);
		return $dstRes;
	}

	//保存圖片處理方法
	//參數:需要保存的圖片資源,保存的路徑,保存的信息
	public static function saveImage($res,$path,$info)
	{
		//根據不同的圖片類型選擇不同的函數進行保存
		switch ($info['mime']) {
			case 'p_w_picpath/png':
			case 'p_w_picpath/x-png':
					p_w_picpathpng($res,$path);
					break;
			case 'p_w_picpath/jpeg':
			case 'p_w_picpath/jpg':
			case 'p_w_picpath/pjpeg':
					p_w_picpathjpeg($res,$path);
					break;
			case 'p_w_picpath/gif':
					p_w_picpathgif($res,$path);
					break;
			case 'p_w_picpath/wbmp':
			case 'p_w_picpath/bmp':
					p_w_picpathwbmp($res,$path);
					break;
		}

	}
}


測試代碼:

$img = new Image();
/*
$img->water('ly.png','logo.gif',3);
$img->water('ly.png','logo.gif',4);*/

$img->thump('ly.png',100,100,'l1_');


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