PHP中物件導向的圖片處理類

來源:互聯網
上載者:User

標籤:圖片處理   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);//銷毀資源imagedestroy($dstRes);imagedestroy($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);//銷毀資源imagedestroy($newRes);//返迴路徑return $path;}//等比縮放處理private static function kidOfImage($srcImg, $size, $imgInfo){$newImg = imagecreatetruecolor($size["width"], $size["height"]);$otsc = imagecolortransparent($srcImg);if ( $otsc >= 0 && $otsc < imagecolorstotal($srcImg)) { $transparentcolor = imagecolorsforindex( $srcImg, $otsc ); $newtransparentcolor = imagecolorallocate( $newImg, $transparentcolor[‘red‘], $transparentcolor[‘green‘], $transparentcolor[‘blue‘] ); imagefill( $newImg, 0, 0, $newtransparentcolor ); imagecolortransparent( $newImg, $newtransparentcolor );}imagecopyresized( $newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"] );imagedestroy($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 = getimagesize($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 ‘image/png‘:case ‘image/x-png‘:$res = imagecreatefrompng($path);break;case ‘image/jpeg‘:case ‘image/jpg‘:case ‘image/pjpeg‘:$res = imagecreatefromjpeg($path);break;case ‘image/gif‘:$res = imagecreatefromgif($path);break;case ‘image/wbmp‘:case ‘image/bmp‘:$res = imagecreatefromwbmp($path);break;}//var_dump($res);die;return $res;}//合并圖片 imagecopymerge(圖片,浮水印,圖片座標x,圖片座標y,浮水印座標x,浮水印座標y,透明度)private function _mergeImage($dstRes,$srcRes,$postion,$dstInfo,$srcInfo,$tmd){imagecopymerge($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 ‘image/png‘:case ‘image/x-png‘:imagepng($res,$path);break;case ‘image/jpeg‘:case ‘image/jpg‘:case ‘image/pjpeg‘:imagejpeg($res,$path);break;case ‘image/gif‘:imagegif($res,$path);break;case ‘image/wbmp‘:case ‘image/bmp‘:imagewbmp($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_‘);


本文出自 “你好我是森林” 部落格,請務必保留此出處http://chensenlin.blog.51cto.com/10559465/1855589

PHP中物件導向的圖片處理類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.