亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 46222 | 回復(fù): 0
打印 上一主題 下一主題

對圖片進行水印添加以及生成縮率圖 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2015-08-12 13:50 |只看該作者 |倒序瀏覽
2個方法
1 給圖片進行水印添加
2生成一個新的縮率圖

代碼
  1. <?php
  2.     class Image{
  3.         //水印配置項
  4.         private $waterOn;
  5.         private $waterImg;
  6.         private $waterPos;
  7.         private $waterPct;
  8.         private $waterText;
  9.         private $waterFont;
  10.         private $waterTextSize;
  11.         private $waterTextColor;
  12.         private $qua;
  13.         //縮略圖配置項
  14.         private $thumbWidth;
  15.         private $thumbHeight;
  16.         private $thumbType;
  17.         private $thumbEndfix;
  18.         //構(gòu)造函數(shù)
  19.         public function __construct(){
  20.             $this->waterOn=C("WATER_ON");
  21.             $this->waterImg=C("WATER_IMG");
  22.             $this->waterPos=C("WATER_POS");
  23.             $this->waterPct=C("WATER_PCT");
  24.             $this->waterText=C("WATER_TEXT");
  25.             $this->waterFont=C("WATER_FONT");
  26.             $this->waterTextSize=C("WATER_TEXT_SIZE");
  27.             $this->waterTextColor=C("WATER_TEXT_COLOR");
  28.             $this->qua=C("WATER_QUA");
  29.             //縮率圖
  30.             $this->thumbWidth=C("THUMB_WIDTH");
  31.             $this->thumbHeight=C("THUMB_HEIGHT");
  32.             $this->thumbType=C("THUMB_TYPE");
  33.             $this->thumbEndFix=C("THUMB_ENDFIX");
  34.             }
  35.         /*
  36.         *驗證圖片是否合法
  37.         */
  38.         private function check($img){
  39.             return is_file($img)&&getimagesize($img)&&extension_loaded("gd");
  40.             }
  41.         /*
  42.         *縮率圖
  43.         *@param string  $img         原圖
  44.         *@param string  $outFile     縮率之后存儲的圖片
  45.         *@param int     $thumbWidth  縮率圖寬度
  46.         *@param int     $thumbHeight 縮率圖高度
  47.         *@param int     $thumbType   那種方式進行縮略處理
  48.         */
  49.         public function thumb($img,$outFile="",$thumbWidth="",$thumbHeight="",$thumbType=""){
  50.             if(!$this->check($img)){
  51.                 return false;
  52.                 }
  53.             //縮率圖處理方式   
  54.             $thumbType=$thumbType?$thumbType:$this->thumbType;
  55.             //縮率圖寬度
  56.             $thumbWidth=$thumbWidth?$thumbWidth:$this->thumbWidth;   
  57.             //縮率圖高度
  58.             $thumbHeight=$thumbHeight?$thumbHeight:$this->thumbHeight;
  59.             //獲取原圖信息
  60.             $imgInfo=getimagesize($img);
  61.             //原圖寬度
  62.             $imgWidth=$imgInfo[0];
  63.             //原圖高度
  64.             $imgHeight=$imgInfo[1];
  65.             //獲得原圖類型
  66.             $imgtype=image_type_to_extension($imgInfo[2]);
  67.             //根據(jù)不同的縮略處理方式,獲得尺寸(原圖和縮略圖相應(yīng)的尺寸)
  68.             $thumb_size=$this->thumbsize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType);
  69.             //創(chuàng)建原圖
  70.             $func="imagecreatefrom".substr($imgtype,1);//變量函數(shù)
  71.             $resImg=$func($img);
  72.             //創(chuàng)建縮率圖畫布
  73.             if($imgtype==".gif"){
  74.                 $res_thumb=imagecreate($thumb_size[2],$thumb_size[3]);
  75.                 }else{
  76.                     $res_thumb=imagecreatetruecolor($thumb_size[2],$thumb_size[3]);
  77.                     }
  78.             imagecopyresized($res_thumb,$resImg,0,0,0,0,$thumb_size[2],$thumb_size[3],$thumb_size[0],$thumb_size[1]);
  79.             $fileInfo=pathinfo($img);//文件信息
  80.             $outFile=$outFile?$outFile:$fileInfo['filename'].$this->thumbEndFix.$fileInfo['extension'];//文件名稱
  81.             $outFile=$fileInfo["dirname"]."/".$outFile;//加上目錄
  82.             $func="image".substr($imgtype,1);
  83.             $func($res_thumb,$outFile);
  84.             return $outFile;
  85.             }   
  86.         private function thumbSize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType){
  87.             //縮率圖尺寸
  88.             $w=$thumbWidth;
  89.             $h=$thumbHeight;
  90.             //原圖尺寸
  91.             $img_w=$imgWidth;
  92.             $img_h=$imgHeight;
  93.             switch($thumbType){
  94.                 case 1:
  95.                     //寬度固定,高度自增
  96.                     $h=$w/$imgWidth*$imgHeight;
  97.                     break;
  98.                 case 2://高度固定,寬度自   
  99.                     $w=$h/$imgHeight*$imgWidth;
  100.                     break;
  101.                 case 3:
  102.                     if($imgHeight/$thumbHeight>$imgWidth/$thumbWidth){
  103.                         $img_h=$imgWidth/$thumbWidth*$thumbHeight;
  104.                         }else{
  105.                             $img_w=$imgHeight/$thumbHeight*$thumbWidth;
  106.                             }
  107.                 }
  108.                 return array($img_w,$img_h,$w,$h);
  109.             }
  110.         /*
  111.         *@param string  $img     原圖
  112.         *@param string  $outImg  加完水印后生成的圖
  113.         *@param int     $pos     水印位置
  114.         *@param int     $pct     透明度
  115.         *@param text    $text    水印文字
  116.         *@param string  $waterImg水印圖片      
  117.         */
  118.         public function water($img,$outImg=null,$pos="",$pct="",$text="",$waterImg="",$textColor=""){
  119.             if(!$this->check($img)){
  120.                 return false;
  121.                 }
  122.             //加完水印后生成的圖
  123.             $outImg=$outImg?$outImg:$img;
  124.             //水印位置
  125.             $pos=$pos?$pos:$this->waterPos;
  126.             //透明度
  127.             $pct=$pct?$pct:$this->waterPct;
  128.             //水印文字
  129.             $text=$text?$text:$this->waterText;
  130.             //水印圖片
  131.             $waterImg=$waterImg?$waterImg:$this->waterImg;
  132.             //驗證水印圖片
  133.             $waterImgOn=$this->check($waterImg);
  134.             //水印文字顏色
  135.             $textColor=$textColor?$textColor:$this->waterTextColor;
  136.             //原圖信息
  137.             $imgInfo=getimagesize($img);
  138.             //原圖寬度
  139.             $imgWidth=$imgInfo[0];
  140.             //原圖高度
  141.             $imgHeight=$imgInfo[1];
  142.             switch($imgInfo[2]){
  143.                 case 1:
  144.                     $resImg=imagecreatefromgif($img);
  145.                     break;
  146.                 case 2:
  147.                     $resImg=imagecreatefromjpeg($img);
  148.                     break;
  149.                 case 3:
  150.                     $resImg=imagecreatefrompng($img);
  151.                     break;      
  152.                 }
  153.             if($waterImgOn){//水印圖片有效
  154.                 //水印信息
  155.                 $waterInfo=getimagesize($waterImg);
  156.                 //水印寬度
  157.                 $waterWidth=$waterInfo[0];
  158.                 //水印高度
  159.                 $waterHeight=$waterInfo[1];
  160.                 //根據(jù)不同的情況創(chuàng)建不同的類型 gif jpeg png
  161.                 $w_img=null;
  162.                 switch($waterInfo[2]){
  163.                     case 1:
  164.                         $w_img=imagecreatefromgif($waterImg);
  165.                         break;
  166.                     case 2:
  167.                         $w_img=imagecreatefromjpeg($waterImg);
  168.                         break;
  169.                     case 3:
  170.                         $w_img=imagecreatefrompng($waterImg);      
  171.                     }
  172.                 }else{//水印圖片失效,使用文字水印
  173.                     if(empty($text)||strlen($textColor)!==7){
  174.                         return false;
  175.                         }
  176.                     //獲得文字水印盒子信息
  177.                     $textInfo=imagettfbbox($this->waterTextSize,0,$this->waterFont,$text);
  178.                     //文字信息寬度
  179.                     $textWidth=$textInfo[2]-$textInfo[6];
  180.                     //文字信息高度   
  181.                     $textHeight=$textInfo[3]-$textInfo[7];
  182.                     }
  183.                 //水印位置
  184.                 $x=$y=20;
  185.                 switch($pos){
  186.                     case 1:
  187.                         break;
  188.                     case 2:
  189.                         $x=($imgWidth-$waterWidth)/2;
  190.                         break;
  191.                     case 3:
  192.                         $y=$imgWidth-$waterWidth-10;
  193.                         break;
  194.                     case 4:
  195.                         $x=($imgHeight-$waterHeight)/2;
  196.                         break;
  197.                     case 5:
  198.                         $x=($imgWidth-$waterWidth)/2;
  199.                         $y=($imgHeight-$waterHeight)/2;
  200.                         break;
  201.                     case 6:
  202.                         $x=$imgWidth-$waterWidth-10;
  203.                         $y=($imgHeight-$waterHeight)/2;
  204.                         break;
  205.                     case 7:
  206.                         $x=$imgHeight-$waterHeight-10;
  207.                         break;
  208.                     case 8:
  209.                         $x=($imgWidth-$waterWidth)/2;
  210.                         $y=$imgHeight-$waterHeight-10;
  211.                         break;
  212.                     case 9:
  213.                         $x=$imgWidth-$waterWidth-10;
  214.                         $y=$imgHeight-$waterHeight-10;      
  215.                         break;
  216.                     default:
  217.                         $x=mt_rand(20,$imgWidth-$waterWidth);
  218.                         $y=mt_rand(20,$imgHeight-$waterHeight);         
  219.                     }   
  220.                 if($waterImgOn){//當(dāng)水印圖片有效時,以圖片形式加水印
  221.                     if($waterInfo[2]==3){
  222.                         imagecopy($resImg,$w_img,$x,$y,0,0,$waterWidth,$waterHeight);
  223.                         }else{
  224.                             imagecopymerge($resImg,$w_img,$x,$y,0,0,$waterInfo,$waterHeight,$pct);
  225.                             }
  226.                     }else{//水印圖片失效,以文字水印加
  227.                         $red=hexdec(substr($this->waterTextColor,1,2));
  228.                         $greem=hexdec(substr($this->waterTextColor,3,2));
  229.                         $blue=hexdec(substr($this->waterTextColor,5,2));
  230.                         $color=imagecolorallocate($resImg,$red,$greem,$blue);
  231.                         imagettftext($resImg,$this->waterTextSize,0,$x,$y,$color,$this->waterFont,$text);
  232.                         }
  233.                 //輸出圖片      
  234.                 switch($imgInfo[2]){
  235.                         case 1:
  236.                             imagegif($resImg,$outImg);
  237.                             break;
  238.                         case 2:
  239.                             imagejpeg($resImg,$outImg);
  240.                             break;
  241.                         case 3:
  242.                             imagepng($resImg,$outImg);
  243.                             break;      
  244.                     }   
  245.                 //垃圾回收
  246.                 if(isset($resImg)){
  247.                     imagedestroy($resImg);
  248.                     }
  249.                 if(isset($w_img)){
  250.                     imagedestroy($w_img);
  251.                     }   
  252.                 return true;               
  253.             }   
  254.         }
  255. ?>
復(fù)制代碼
代碼
  1. <?php
  2. return array(
  3.     //水印處理
  4.     "WATER_ON"=>1,//水印開關(guān)
  5.     "WATER_IMG"=>"./data/logo.png",//水印圖片
  6.     "WATER_POS"=>9,//水印位置
  7.     "WATER_PCT"=>80,//水印透明度
  8.     "WATER_TEXT"=>"http://www.caoxiaobin.cn",
  9.     "WATER_FONT"=>"./data/simsunb.ttf",//水印字體
  10.     "WATER_TEXT_COLOR"=>"#333333",//文字顏色 16進制表示
  11.     "WATER_TEXT_SIZE"=>16,//文字大小
  12.     "WATER_QUA"=>80,//圖片壓縮比
  13.     //縮略圖
  14.     "THUMB_WIDTH"=>150,//縮率圖寬度
  15.     "THUMB_HEIGHT"=>150,//縮略圖高度
  16.     "THUMB_TYPE"=>1,//縮略圖處理  1寬度固定,高度自增 2高度固定,寬度自增 //縮略圖尺寸不變,對原圖進行裁切
  17.     "THUMB_ENDFIX"=>"_thmub"//縮略圖后綴
  18.      
  19. );
  20. ?>
復(fù)制代碼
代碼
  1. /*
  2. * 不區(qū)分大小寫的數(shù)據(jù)鍵檢測
  3. */
  4. function array_key_exists_d($key,$arr){
  5.     $_key=strtolower($key);
  6.     foreach ($arr as $k=>$v){
  7.         if($_key==strtolower($k)){
  8.             return true;
  9.         }
  10.     }
  11. }
  12. /*
  13. * 遞歸更改數(shù)組的KEY(鍵名)
  14. * @param array;
  15. * @stat int 0小寫 1大寫
  16. */
  17. function array_change_key_case_d($arr,$stat=0){
  18.     $func=$stat?"strtoupper":"strtolower";
  19.     $_newArr=array();
  20.     if(!is_array($arr)||empty($arr)){
  21.         return $_newArr;
  22.     }
  23.     foreach($arr as $k=>$v){
  24.         $_k=$func($k);//通過變量函數(shù)轉(zhuǎn)換KEY大小寫
  25.         $_newArr[$_k]= is_array($v)?array_change_key_case_d($v):$v;
  26.     }
  27.     return $_newArr;
  28. }
  29. /*
  30. * 讀取與設(shè)置配置項
  31. * @param $name void 配置項名稱,如果不填寫返回所有配置項
  32. * @param $value void 配置項的值
  33. * @param $value 值 false null 只取$name值
  34. */
  35. function C($name=null,$value=null){
  36.     static $config=array();//靜態(tài)變量$config存儲所有配置項
  37.     if(is_null($name)){
  38.         return $config;
  39.     }
  40.     //如果$name為數(shù)組
  41.     if(is_array($name)){
  42.         return $config=array_merge($config,array_change_key_case_d($name,1));
  43.     }
  44.     //$name為字符串 2種情況 $value無值表示獲得配置項的值,有值表示更改配置項
  45.     if(is_string($name)){
  46.         $name=  strtoupper($name);
  47.         //獲得配置項的值
  48.         if(is_null($value)){
  49.           return  array_key_exists_d($name,$config)?$config[$name]:null;
  50.         }else{
  51.             //設(shè)置值
  52.             $config[$name]=$value;
  53.             return true;
  54.         }
  55.     }
  56. }
復(fù)制代碼
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP