-
-
/** 縮圖產生類別,支援imagemagick及gd庫兩種處理
- * Date: 2013 -07-15
- * Author: fdipzone
- * Ver: 1.2
- * edit: bbs.it-home.org
- * Func:
- * public set_config: 設定參數
- * Func:
- * public set_config: 設定參數
- * public create_thumb: 產生縮圖
- * private fit: 縮寫圖片
- * private crop: 裁切圖片
- * private gd_fit: GD1p: GD
- >* private get_size: 獲取要轉換的size
- * private get_crop_offset: 獲取裁圖的偏移量
- * private add_watermark: 添加水印
- * private check_handler: 判斷處理程序是否已安裝
- * private create_dirs: 建立目錄
- * private exists: 判斷參數是否存在
- * private to_log: 記錄log
- * private hex2rgb: hex_vgb> 獲取圖片🎜>* private hex2rgb: hex_getsgb
- 獲取圖片🎜> 得到圖片*
- * ver: 1.1 增加GD庫處理
- * ver: 1.2 增加width,height錯誤參數處理
- * 增加當圖片colorspace不為RGB時作轉RGB處理* 修正使用crop保存為gif時出現透明無效區域問題,使用+repage參數,刪除透明無效區域即可
- *
- * tips:建議使用imagemagick
- * GD庫不支援透明度水印,如果必須使用透明水印,請將浮水印圖片做成有透明度。
- * GD庫輸出gif如加透明浮水印,會有問題。
- */
-
- class PicThumb{ // class start
-
- private $_log = null; // log file
- private $_handler = null; // 進行圖片處理的程式,imagemagick/gd函式庫
- private $_type = 'fit'; // fit or crop
- private $_source = null; // 原圖路徑
- private $_dest = null; // 縮圖路徑
- private $_watermark = null; // 水印圖片
- private $_opacity = 75; // 水印圖片透明度,gd庫不支援
- private $_gravity = 'SouthEast'; // 水印擺放位置NorthWest,, North, NorthEast, West, Center, East, SouthWest, South, SouthEast
- private $_geometry = '+10+10'; // 水印定位,gd庫不支援
- private $_croppos = 'TL'; /gd庫不支援
- private $_croppos = 'TL'; / / 截圖的位置TL TM TR ML MM MR BL BM BR
- private $_bgcolor = null; // 填入的背景色
- private $_quality = 90; // 產生的圖片品質
- private $_quality = 90; // 產生的圖片品質
- private $_width = null; // 指定區域寬度
- private $_height = null; // 指定區域高度
-
-
- // 初始化
- public function __construct($logfile=''){
- if($logfile!=''){
- $this->_log = $logfile;
- }
- }
-
-
- // 設定參數
- public function set_config ( $param=array()){
- $this->_handler = $this->exists($param, 'handler')? strtolower($param['handler']) : null;
- $this- >_type = $this->exists($param, 'type')? strtolower($param['type']) : 'fit';
- $this->_watermark = $this- >exists($param, 'watermark')? $param['watermark'] : null;
- $this->_opacity = $this->exists($param, 'opacity')? $param[ 'opacity'] : 75;
- $this->_gravity = $this->exists($param, 'gravity')? $param['gravity'] : 'SouthEast';
- $this- >_geometry = $this->exists($param , 'geometry')? $param['geometry'] : '+10+10';
- $this->_croppos = $this->exists ($param, 'croppos')? $param['croppos' ] : 'TL';
- $this->_bgcolor = $this->exists($param, 'bgcolor')? $param[' bgcolor'] : null;
- $this->_quality = $this ->exists($param, 'quality')? $param['quality'] : 90;
- $this->_width = $this->exists($param, 'width')? $param['width '] : null;
- $this->_height = $this->exists($param, 'height') ? $param['height'] : null;
- }
-
- /* *建立縮圖
- * @param String $source 原圖
- * @param String $dest 目標圖
- * @return boolean
- */
- public function create_thumb($source, $dest){
-
- / / 檢查所使用的handler是否已安裝
- if(!$this->check_handler()){
- $this->to_log('handler not installed');
- return false;
- }
-
- // 判斷區域寬高是否正確
- if(!is_numeric($this ->_width) || !is_numeric($this->_height) || $this->_width< ;=0 || $this->_height $this->to_log('width or height invalid');
- return false;
- }
-
- // 判斷來源檔案是否存在
- if(!file_exists($source)){
- $this->to_log($ source.' not exists');
- return false;
- }
-
- // 建立目標檔案路徑
- if(!$this->create_dirs($dest)){
- $this->to_log(dirname($dest).' create fail') ;
- return false;
- }
-
- $this->_source = $source; // 來源檔案
- $this->_dest = $dest; // 目標檔
-
- // 處理圖片
- switch($this->_type){
- case 'fit':
- if($this ->_handler=='imagemagick'){
- return $this->fit();
- }else{
- return $this->gd_fit();
- }
- break;
-
- case 'crop':
- if($this->_handler=='imagemagick'){
- return $this->crop();
- }else{
- return $ this->gd_crop();
- }
- break;
-
- default:
- $this->to_log($this->_type.' not fit and crop');
- return false;
- } }
-
-
- /**將圖片按比例壓縮或拉伸圖片
- * @return boolean
- */
- private function fit(){
-
- // 判斷是否填入背景
- $bgcolor = ($this->_bgcolor !=null)?
- sprintf(" -background '%s' -gravity center -extent '%sx%s' ", $this->_bgcolor, $this->_width, $this->_height) : " ";
-
- // 判斷是否要轉為RGB
- $source_info = getimagesize($this->_source);
- $colorspace = (!isset($source_info['channels']) | | $source_info['channels']!=3)? ' -colorspace RGB ' : '';
-
- // 命令列
- $cmd = sprintf("convert -resize '%sx%s' '%s' %s -quality %s %s '%s'", $this->_width, $this->_height, $this->_source, $bgcolor, $this->_quality, $colorspace, $this ->_dest);
-
- // 記錄執行的指令
- $this->to_log($cmd);
-
- // 執行指令
- exec($cmd);
-
- // 加水印
- $this->add_watermark($this->_dest);
-
- return is_file($this->_dest)? true : false;
-
-
- }
-
-
- /**裁切圖片
- * @return boolean
- */
- private function crop(){
-
- // 取得產生的圖片尺寸
- list($pic_w, $ pic_h) = $this->get_size();
-
- // 取得截圖的偏移
- list($offset_w, $offset_h) = $this->get_crop_offset($pic_w, $pic_h);
-
- // 判斷是否要轉為RGB
- $source_info = getimagesize($this->_source);
- $colorspace = (!isset($source_info['channels']) || $ source_info['channels']!=3)? ' -colorspace RGB ' : '';
-
- // 命令列
- $cmd = sprintf("convert -resize '%sx%s' '% s' -quality %s %s -crop %sx%s+%s+%s +repage '%s'", $pic_w, $pic_h, $this->_source, $this->_quality, $colorspace, $this- >_width, $this->_height, $offset_w, $offset_h, $this->_dest);
-
- // 記錄執行的命令
- $this->to_log($cmd);
-
- // 執行指令
- exec($cmd);
-
- // 新增浮水印
- $this->add_watermark($this->_dest);
-
- return is_file ($this->_dest)? true : false;
-
- }
-
-
- /**GD庫依比例壓縮或伸展圖片
- * @return boolean
- */
- private function gd_fit(){ */
- private function gd_fit(){
-
- > // 取得產生的圖片尺寸
- list($pic_w, $pic_h) = $this->get_size();
-
- list($owidth, $oheight, $otype) = getimagesize($this ->_source);
-
- switch($otype){
- case 1: $source_img = imagecreatefromgif($this->_source); break;
- case 2: $source_img = imagecreate ->_source); break;
- case 3: $source_img = imagecreatefrompng($this->_source); break;
- default: return false;
- }
-
- // 依比例縮略/拉伸圖片
- $new_img = imagecreatetruecolor($pic_w, $pic_h);
- $new_img = imagecreatetruecolor($pic_w, $pic_h);
- imagecopyresampled($new_img, $source_img, 0, 0, 0, 0, $pic_w, $pic_h, $picpic_ho );
-
- // 判斷是否填入背景
- if($this->_bgcolor!=null){
- $bg_img = imagecreatetruecolor($this->_width, $this->_height);
- $rgb = $this->hex2rgb($this->_bgcolor);
- $bgcolor =imagecolorallocate($bg_img, $rgb['r'], $rgb['g'], $rgb[' b']);
- imagefill($bg_img, 0, 0, $bgcolor);
- imagecopy($bg_img, $new_img, (int)(($this->_width-$pic_w)/2), (int)(($this->_height-$pic_h)/2), 0, 0, $pic_w, $pic_h);
- $new_img = $bg_img;
- }
-
- //取得目標圖片的類型
- $dest_ext = $this->get_file_ext($this->_dest);
-
- // 產生圖片
- switch($dest_ext){
- case 1: imagegif ($new_img, $this->_dest, $this->_quality); break;
- case 2: imagejpeg($new_img, $this->_dest, $this->_quality); break;
- case 3 : imagepng($new_img, $this->_dest, (int)(($this->_quality-1)/10)); break;
- }
-
- if(isset($source_img)) {
- imagedestroy($source_img);
- }
-
- if(isset($new_img)){
- imagedestroy($new_img);
- }
-
- //加水印
- $this->add_watermark($this->_dest);
- return is_file($this->_dest)? true : false; }
-
-
- /**GD庫裁切圖片
- * @return boolean
- */
- private function gd_crop(){
-
- // 取得產生的圖片尺寸
- list($pic_w, $pic_h) = $this->get_size();
-
- // 取得截圖的偏移
- list($offset_w, $offset_h) = $this->get_crop_offset($pic_w, $pic_h);
-
- list($owidth, $oheight, $otype) = getimagesize($this->_source);
-
- switch($otype){
- case 1: $source_img = imagecreatefrom(thisagecreatefrom($this ->_source); break;
- case 2: $source_img = imagecreatefromjpeg($this->_source); break;
- case 3: $source_img = imagecreatefrompng($this->_source); break;
- ; default: return false;
- }
-
- // 按比例縮寫/拉伸圖片
- $tmp_img = imagecreatetruecolor($pic_w, $pic_h);
- imagecopyresampled($tmpresampled($img, $ , 0, 0, 0, 0, $pic_w, $pic_h, $owidth, $oheight);
-
- // 裁切圖片
- $new_img = imagecreatetruecolor($this->_width, $this-> _height);
- imagecopyresampled($new_img, $tmp_img, 0, 0, $offset_w, $offset_h, $this->_width, $this->_height, $this->_width, $this->_height); 🎜>
- // 取得目標圖片的類型
- $dest_ext = $this->get_file_ext($this->_dest);
-
- // 產生圖片
- switch($dest_ext){
- case 1: imagegif($new_img, $this->_dest, $this->_quality); break;
- case 2: imagejpeg($new_img, $this->_dest, $this->_quality); break;
- case 3: imagepng($new_img, $this->_dest, (int)(($this->_quality-1)/10)); break;
- }
-
- if (isset($source_img)){
- imagedestroy($source_img);
- }
-
- if(isset($tmp_img)){
- imagedestroy($tmp_img); 🎜>
- if(isset($new_img)){
- imagedestroy($new_img);
- }
-
- // 加水印
- $this->add_watermark($this-> _dest);
-
- return is_file($this->_dest)? true : false;
-
- }
-
-
- /**取得目標圖產生的size
- * @return Array $width, $height
- / private function get_size(){
- list($owidth, $oheight) = getimagesize($this->_source);
- $width = (int)($this->_width);
- $height = (int)($this->_height);
-
- switch($this->_type){
- case 'fit':
- $pic_w = $width;
- $pic_h = ( int)($pic_w*$oheight/$owidth);
- if($pic_h>$height){
- $pic_h = $height;
- $pic_w = (int)($pic_h*$owidth/ $oheight);
- }
- break;
- case 'crop':
- if($owidth>$oheight){
- $pic_h = $height;
- $pic_w = (int )($pic_h*$owidth/$oheight);
- }else{
- $pic_w = $width;
- $pic_h = (int)($pic_w*$oheight/$owidth);
- }
- break;
- }
-
- return array($pic_w, $pic_h);
- }
-
-
- /**取得截圖的偏移
- * @param int $pic_w 圖寬度
- * @param int $pic_h 圖高度
- * @return Array $offset_w, $offset_h
- */
- 毛髮函數 get_crop_offset($pic_w, $pic_h){
- $offset_w = 0;
- $offset_h = 0;
-
- switch(strtoupper($this->_croppos)){
- case 'TL':
- $offset_w = 0;
- $offset_h = 0;
- 休息;
-
- case 'TM':
- $offset_w = (int)(($pic_w-$this->_width)/2);
- $offset_h = 0;
- 休息;
-
- case 'TR':
- $offset_w = (int)($pic_w-$this->_width);
- $offset_h = 0;
- 休息;
-
- 案例 'ML':
- $offset_w = 0;
- $offset_h = (int)(($pic_h-$this->_height)/2);
- 休息;
-
- case 'MM':
- $offset_w = (int)(($pic_w-$this->_width)/2);
- $offset_h = (int)(($pic_h-$this->_height)/2);
- 休息;
-
- case 'MR':
- $offset_w = (int)($pic_w-$this->_width);
- $offset_h = (int)(($pic_h-$this->_height)/2);
- 休息;
-
- 案例 'BL':
- $offset_w = 0;
- $offset_h = (int)($pic_h-$this->_height);
- 休息;
-
- case 'BM':
- $offset_w = (int)(($pic_w-$this->_width)/2);
- $offset_h = (int)($pic_h-$this->_height);
- 休息;
-
- case 'BR':
- $offset_w = (int)($pic_w-$this->_width);
- $offset_h = (int)($pic_h-$this->_height);
- 休息;
- }
-
- 回傳記憶體($offset_w, $offset_h);
- }
-
-
- /**新增浮水印
- * @param String $dest 圖片路徑
- */
- private function add_watermark($dest){
- if($this->_watermark!=null && file_exists($ 1 ->_watermark) && file_exists($dest)){
- list($owidth, $oheight, $otype) = getimagesize($dest);
- list($w, $h, $wtype) = getimagesize($this->_watermark);
-
- // 水印圖比原圖小才加浮水印
- if($w
- if($this-> ;_handler=='imagemagick'){ // imagemagick 加浮水印
-
- $cmd = sprintf("composite -gravity %s -geometry %s -dissolve %s '%s' %s %s", $這->_gravity,$這->_geometry,$這->_opacity,$這->_watermark,$dest,$dest);
-
- $this->to_log($cmd);
-
- exec($cmd);
-
- }else{ // gd 加水印
-
- switch($wtype){
- case 1: $water_img = imagecreatefromgif($this->_watermark);休息;
- 情況2:$water_img = imagecreatefromjpeg($this->_watermark); 休息;
- 案例3:$water_img = imagecreatefrompng($this->_watermark); 休息;
- 預設:回傳 false;
- }
-
- switch($otype){
- 情況 1: $dest_img = imagecreatefromgif($dest); 休息;
- 情況2:$dest_img = imagecreatefromjpeg($dest);休息;
- 案例 3:$dest_img = imagecreatefrompng($dest); 休息;
- 預設:回傳 false;
- }
-
- // 水印位置
- switch(strtolower($this->_gravity)){
- case '西北':
- $posX = 0;
- $posY = 0
- $posX = 0;
- $posY = 0
- 休息;
- case '北':
- $posX = ($owidth - $w) / 2;
- $posY = 0;
- 休息;
- case '東北':
- $posX = $owidth - $w;
- $posY = 0;
- 休息;
- case '西':
- $posX = 0;
- $posY = ($oheight - $h) / 2;
- 休息;
- case 'center':
- $posX = ($owidth - $w) / 2;
- $posY = ($oheight - $h) / 2 ;
- 休息;
- case 'east':
- $posX = $owidth - $w;
- $posY = ($oheight - $h) / 2;
- 休息;
- case '西南':
- $posX = 0;
- $posY = $oheight - $h;
- 休息;
- case '南':
- $posX = ($owidth - $w ) / 2;
- $posY = $oheight - $h;
- 休息;
- case '東南':
- $posX = $owidth - $w;
- $posY = $oheight -posY = $owidth - $w;
- $posY = $oheight - $h;
- 休息;
- }
-
- imagealphablending($dest_img, true);
- imagecopy($dest_img, $water_img, $posX, $posY, 0, 0, $w, $w $h);
-
- switch($otype){
- 情況1:imagegif($dest_img, $dest, $this->_quality);休息;
- 情況2:imagejpeg($dest_img, $dest, $this->_quality);休息;
- 情況3:imagepng($dest_img, $dest, (int)(($this->_quality-1)/10));休息;
- }
-
- if(isset($water_img)){
- imagedestroy($water_img);
- }
-
- if(isset($dest_img)){
-
- if(isset($dest_img)){
- imagedestro );
- }
-
- }
- }
- }
- }
-
-
- /**判斷處理程序是否已安裝
- * @return boolean
- */
- 私有函式>
- /**建立圖片目錄
- * @param String $path
- * @return boolean
- */
- 私有函數{
-
- $handler = $this->_handler;
-
- if(!in_array($handler, array('imagemagick', 'gd', null))){
- return false ;
- }
-
- // 檢查是否安裝imagemagick
- $imagemagick_installed = strstr(shell_exec('convert -version'),'Version: ImageMagick')!=''?:假;
-
- // 檢查是否有安裝gd函式庫
- $gd_installed = function_exists('gd_info')? 真:假;
-
- switch($handler){
- case 'imagemagick':
- return $imagemagick_installed;
- 休息;
-
- case 'gd':
- return $gd_installed;
- 休息;
-
- case null:
- if($ imagemagick_installed){
- $this->_handler = 'imagemagick';
- 回傳true;
- }
-
- if($gd_installed){
- $this->_handler = 'gd' ;
- 返回true;
- }
- 休息;
- }
-
- 返回false;
- }
-
-
- /**判斷參數是否存在
- * @param Array $obj 陣列物件
- * @param String $key 要找的key
- * @return boolean
- */
- private function create_dirs($dest){
- if(!is_dir(dirname($dest))){
- 回傳mkdir(dirname($dest), 0777, true);
- }
- > 回傳true;
- }
-
-
- /***/
- 私有函數存在($obj,$key=''){
- if($key= ='') { return isset($obj) && !empty($obj); }else{ $keys =explode('.',$key); for($ i=0,$max=count($keys); $i if(isset($obj[$keys[$i]])){ $obj = $ obj[$keys[$i]]; }else{
- 回傳false;
- }
- }
- return isset($obj) && !empty($obj);
- }
- }
-
-
- /**記錄log
- * @param String $msg 要記錄的log 訊息
- */
- 私有函數to_log($msg){
- if($this->_log){
- $msg = '['.date('Y-m-d H:i:s ').']'.$msg."rn";
- file_put_contents($this->_log, $msg, FILE_APPEND);
- }
- }
-
-
- /* *hex顏色轉rgb顏色
- * @param String $color hex顏色
- * @return Array
- */
- 私有函數hex2rgb($hexcolor){
- $color = str_replace('#', '' , $hexcolor);
- if (strlen($color) > 3) {
- $rgb = array(
- 'r' => hexdec(substr($color, 0, 2)),
- 'g ' => 十六進位(substr($color, 2, 2 )),
- 'b' => 十六進位(substr($color, 4, 2))
- );
- } else {
- $r = substr($color, 0, 1) . substr($顏色, 0, 1);
- $g = substr($color, 1, 1) 。 substr($顏色, 1, 1);
- $b = substr($color, 2, 1) 。 substr($顏色, 2, 1);
- $rgb = array(
- 'r' => 十六進位($r),
- 'g' => 十六進位($g ),
- 'b' => 十六進位($ b)
- );
- }
- 回傳$rgb;
- }
-
-
- /** 取得圖片類型
- * @param String $file 圖片路徑
- * @return int
- */
- private function get_file_ext($file){
- $filename = basename($file);
- list($name, $ext)=explode('.', $filename);
-
- $ext_type = 0;
-
- switch(strtolower($ext)){
- case 'jpg':
- case 'jpeg':
- $ext_type = 2;
- 休息;
- case 'gif':
- $ext_type = 1;
- 休息;
- 案例'png':
- $ext_type = 3;
- 休息;
- }
-
- 回傳$ext_type;
- }
-
- } // 課程結束
-
- ?>;
- 示範:
- [php] view plaincopydefine('ROOT', dirname(__FILE__));
-
- require(ROOT."/PicThumb.class.php");
-
- $logfile = ROOT. '/PicThumb.log';
- $source1 = ROOT.'/pic/source.jpg';
- $dest1 = ROOT.'/pic/1.jpg';
- $dest2 = ROOT.' /pic/2.gif';
- $dest3 = ROOT.'/pic/3.png';
-
- $source2 = ROOT.'/pic/source_cmyk.jpg';
- $dest4 = ROOT.'/pic/4.jpg';
- $dest5 = ROOT.'/pic/5.gif';
- $dest6 = ROOT.'/pic/6.png';
-
- $watermark = ROOT.'/pic/watermark.png';
-
- //按比例產生預算
- $param = array(
- 'type' => 'fit',
- 'width' => 100,
- 'height ' => 100,
- );
-
- $obj = new PicThumb($logfile);
- $obj->set_config($param );
- $flag = $obj->create_thumb($source1, $dest1);
-
- if($flag){ // bbs.it-home.org
- echo '';
- }else{
- echo '建立拇指失敗';
- }
-
- //按比例產生少許,缺失部分以#FF0000填滿
- $param = array(
- 'type' => 'fit',
- 'width ' => 100,
- '高度' => 100,
- ' bgcolor' => '#FFFF00'
- );
-
- $obj = new PicThumb($logfile);
- $obj->set_config($param);
- $flag = $objobjj->set_config($param);
- $flag = $objbjj-> ->create_thumb($source1, $dest2);
-
- if($flag){
- echo '';
- }else{
- echo '創建拇指失敗';
- }
-
- // 裁剪250x250的大概,裁剪位置是底部中間,水印位置西南,寬度50
- $param = array (
- 'type' => 'crop' ,
- 'croppos' => 'BM',
- '寬度' => 250,
- '高度' => 250,
- '水印' => $水印> '不透明度' => 50,
- '重力' => '西南'
- );
-
- $obj = new PicThumb($logfile);
- $ obj->set_config($param);
- $flag = $obj->create_thumb($source1, $dest3);
-
- if($flag){
- echo '';
- }else{
- echo '建立拇指失敗';
- }
-
- //按比例產生特定CMYK格式
- $param = array(
- 'type' => 'fit',
- 'width' => 100 ,
- '高度' => 100,
- );
-
- $obj = new PicThumb($logfile);
- $obj->set_config($param);
- $flag = $obj->create_thumb($source2, $dest4);
-
- if( $flag){
- echo '';
- }else{
- echo '建立拇指失敗';
- }
// 以比例產生縮圖,不足部分以#FF0000填滿CMYK格式
- $param = array(
- 'type' => 'fit',
- ' width' => 100,
- 'height' => 100,
- 'bgcolor' => '#FFFF00'
- );
-
- $obj = new PicThumb($logfile);
- $obj->set_config($param);
- $flag = $obj->create_thumb($source2, $dest5);
-
- if($flag){
- echo '';
- }else{
- echo 'create thumb fail';
- }
-
- // 裁剪250x250的縮圖圖,裁切位置是底部中間,浮水印位置西南,透明度50 CMYK格式
- $param = array(
- 'type' => 'crop',
- 'croppos' => 'BM',
- 'width' => 250,
- 'height' => 250,
- 'watermark' => $watermark,
- 'opacity' => 50,
- 'gravity' => 'SouthWest'
- );
-
- $obj = new PicThumb($logfile);
- $obj->set_config($param);
- $flag = $obj->create_thumb($source2, $🎜>$flag = $obj->create_thumb($source2, $🎜>$flag = $obj->create_thumb($source2, $🎜>$flag = $obj->create_thumb($source2, $🎜>$flag = $obj->create_thumb($source2, $ dest6);
-
- if($flag){
- echo '';
- }else{
- echo 'create thumb fail';
- }
- ?>
複製程式碼
>>>> php縮圖產生類別原始碼下載位址
|