php封裝的圖片(縮略圖)處理類完整實(shí)例
本文實(shí)例講述了php封裝的圖片(縮略圖)處理類。分享給大家供大家參考,具體如下:
<?php //圖片處理工具類 class Image{ //屬性 private $thumb_width; //縮略圖的寬 private $thumb_height; //錯(cuò)誤屬性 public $thumb_error; //構(gòu)造方法 public function __construct($width = 0,$height = 0){ $this->thumb_width = ($width == 0) ? $GLOBALS['config']['admin_goods_thumb']['width'] : $width; $this->thumb_height = ($height == 0) ? $GLOBALS['config']['admin_goods_thumb']['height'] : $height; } /* * 制作縮略圖 * @param1 string $src,原圖路徑,/uploads/20150122101010abcdef.gif * @param2 string $path,縮略圖保存路徑/uploads/thumb_20150122101010abcdef.gif * @return 縮略圖的名字 */ public function makeThumb($src,$path){ //判斷原圖是否存在 if(!file_exists($src)){ $this->thumb_error = '原圖不存在!'; return false; } //打開原圖資源 //獲取能夠使用的后綴 $ext = $this->getFunctionName($src); //gif //拼湊函數(shù)名 $open = 'imagecreatefrom' . $ext; //imagecreatefromgif $save = 'image' . $ext; //imagegif //如果不清楚;echo $open,$save;exit; //可變函數(shù)打開原圖資源 $src_img = $open($src); //利用可變函數(shù)打開圖片資源 //imagecreatefromgif($src) //縮略圖資源 $dst_img = imagecreatetruecolor($this->thumb_width,$this->thumb_height); //背景色填充白色 $dst_bg_color = imagecolorallocate($dst_img,255,255,255); imagefill($dst_img,0,0,$dst_bg_color); //寬高比確定寬高 $dst_size = $this->thumb_width / $this->thumb_height; //獲取原圖數(shù)據(jù) $file_info = getimagesize($src); $src_size = $file_info[0]/$file_info[1]; //求出縮略圖寬和高 if($src_size > $dst_size){ //原圖寬高比大于縮略圖 $width = $this->thumb_width; $height = round($width / $src_size); }else{ $height = $this->thumb_height; $width = round($height * $src_size); } //求出縮略圖起始位置 $dst_x = round($this->thumb_width - $width)/2; $dst_y = round($this->thumb_height - $height)/2; //制作縮略圖 if(imagecopyresampled($dst_img,$src_img,$dst_x,$dst_y,0,0,$width,$height,$file_info[0],$file_info[1])){ //采樣成功:保存,將文件保存到對應(yīng)的路徑下 $thumb_name = 'thumb_' . basename($src); $save($dst_img,$path . '/' . $thumb_name); //保存成功 return $thumb_name; }else{ //采樣失敗 $this->thumb_error = '縮略圖采樣失??!'; return false; } } /* * 獲取文件要調(diào)用的函數(shù)名 * @param1 string $file,文件名字 * @return 通過文件后綴名得到的函數(shù)字符串 */ private function getFunctionName($file){ //得到文件的后綴 $file_info = pathinfo($file); $ext = $file_info['extension']; //后綴:gif,png,jpg,jpeg,pjpeg //imagecreatefromgif,imagecreatefromjpeg,imagecreatefrompng //定義一個(gè)數(shù)組保存函數(shù)名 $func = array( 'gif' => 'gif', 'png' => 'png', 'jpg' => 'jpeg', 'jpeg' => 'jpeg', 'pjpeg' => 'jpeg' ); //返回值 return $func[$ext]; } }
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php文件操作總結(jié)》、《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP函數(shù)實(shí)現(xiàn)從一個(gè)文本字符串中提取關(guān)鍵字的方法
這篇文章主要介紹了PHP函數(shù)實(shí)現(xiàn)從一個(gè)文本字符串中提取關(guān)鍵字的方法,涉及php針對字符串的遍歷與查找等操作技巧,需要的朋友可以參考下2015-07-07PHP實(shí)現(xiàn)讀取文件夾及批量重命名文件操作示例
這篇文章主要介紹了PHP實(shí)現(xiàn)讀取文件夾及批量重命名文件操作,涉及php目錄讀取、遍歷、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04php 攻擊方法之談php+mysql注射語句構(gòu)造
由于PHP和MYSQL本身得原因,PHP+MYSQL的注射要比asp困難,尤其是注射時(shí)語句的構(gòu)造方面更是個(gè)難點(diǎn),本文主要是借對Okphp BBS v1.3一些文件得簡單分析,來談?wù)刾hp+mysql注射語句構(gòu)造方式,希望本文對你有點(diǎn)幫助。2009-10-10php使用curl通過代理獲取數(shù)據(jù)的實(shí)現(xiàn)方法
這篇文章主要介紹了php使用curl通過代理獲取數(shù)據(jù)的實(shí)現(xiàn)方法,主要涉及php curl中CURLOPT_PROXYUSERPWD參數(shù)的使用技巧,需要的朋友可以參考下2016-05-05PHP中CURL的CURLOPT_POSTFIELDS參數(shù)使用細(xì)節(jié)
CURL確實(shí)是一個(gè)不錯(cuò)的好工具,不僅在PHP中還是其他的操作系統(tǒng)中,都是一個(gè)非常好用的。但是如果你有些參數(shù)沒有用好的話,那可能會(huì)得不到自己理想中的結(jié)果2014-03-03