水印 縮略圖 驗證碼類

 

  1. class ImageTool { 
  2.     // imageInfo 分析圖片的信息 
  3.     // return array() 
  4.     public static function imageInfo($image) { 
  5.         // 判斷圖片是否存在 
  6.         if(!file_exists($image)) { 
  7.             return false; 
  8.         } 
  9.  
  10.         $info = getimagesize($image); 
  11.          
  12.         if($info == false) { 
  13.             return false; 
  14.         } 
  15.  
  16.         // 此時info分析出來,是一個數組 
  17.         $img['width'] = $info[0]; 
  18.         $img['height'] = $info[1]; 
  19.         $img['ext'] = substr($info['mime'],strpos($info['mime'],'/')+1); 
  20.  
  21.         return $img
  22.     } 
  23.  
  24.  
  25.  
  26.     /* 
  27.         加水印功能 
  28.         parm String $dst 等操作圖片 
  29.         parm String $water 水印小圖 
  30.         parm String $save,不填則默認替換原始圖 
  31.     */ 
  32.     public static function water($dst,$water,$save=NULL,$pos=2,$alpha=50) { 
  33.         // 先保證2個圖片存在 
  34.         if(!file_exists($dst) || !file_exists($water)) { 
  35.             return false; 
  36.         } 
  37.          
  38.          
  39.         // 首先保證水印不能比待操作圖片還大 
  40.         $dinfo = self::imageInfo($dst); 
  41.         $winfo = self::imageInfo($water); 
  42.  
  43.         if($winfo['height'] > $dinfo['height'] || $winfo['width'] > $dinfo['width']) { 
  44.             return false; 
  45.         } 
  46.  
  47.         // 兩張圖,讀到畫布上! 但是圖片可能是png,可能是jpeg,用什麼函數讀? 
  48.         $dfunc = 'imagecreatefrom' . $dinfo['ext']; 
  49.         $wfunc = 'imagecreatefrom' . $winfo['ext']; 
  50.  
  51.         if(!function_exists($dfunc) || !function_exists($wfunc)) { 
  52.             return false; 
  53.         } 
  54.  
  55.  
  56.         // 動態加載函數來創建畫布 
  57.         $dim = $dfunc($dst);  // 創建待操作的畫布 
  58.         $wim = $wfunc($water);  // 創建水印畫布 
  59.  
  60.  
  61.         // 根據水印的位置 計算粘貼的座標 
  62.         switch($pos) { 
  63.             case 0: // 左上角 
  64.             $posx = 0; 
  65.             $posy = 0; 
  66.             break
  67.  
  68.             case 1: // 右上角 
  69.             $posx = $dinfo['width'] - $winfo['width']; 
  70.             $posy = 0; 
  71.             break
  72.  
  73.             case 3: // 左下角 
  74.             $posx = 0; 
  75.             $posy = $dinfo['height'] - $winfo['height']; 
  76.             break
  77.          
  78.             default
  79.             $posx = $dinfo['width'] - $winfo['width']; 
  80.             $posy = $dinfo['height'] - $winfo['height']; 
  81.         } 
  82.  
  83.  
  84.         // 加水印 
  85.         imagecopymerge ($dim,$wim$posx , $posy , 0 , 0 , $winfo['width'] , $winfo['height'] , $alpha); 
  86.  
  87.         // 保存 
  88.         if(!$save) { 
  89.             $save = $dst
  90.             unlink($dst); // 刪除原圖 
  91.         } 
  92.  
  93.         $createfunc = 'image' . $dinfo['ext']; 
  94.         $createfunc($dim,$save); 
  95.  
  96.         imagedestroy($dim); 
  97.         imagedestroy($wim); 
  98.  
  99.         return true; 
  100.     } 
  101.  
  102.  
  103.     /** 
  104.         thumb 生成縮略圖 
  105.         等比例縮放,兩邊留白 
  106.     **/ 
  107.     public static function thumb($dst,$save=NULL,$width=200,$height=200) { 
  108.         // 首先判斷待處理的圖片存不存在 
  109.         $dinfo = self::imageInfo($dst); 
  110.         if($dinfo == false) { 
  111.             return false; 
  112.         } 
  113.  
  114.         // 計算縮放比例 
  115.         $calc = min($width/$dinfo['width'], $height/$dinfo['height']); 
  116.  
  117.         // 創建原始圖的畫布 
  118.         $dfunc = 'imagecreatefrom' . $dinfo['ext']; 
  119.         $dim = $dfunc($dst); 
  120.  
  121.         // 創建縮略畫布 
  122.         $tim = imagecreatetruecolor($width,$height); 
  123.  
  124.         // 創建白色填充縮略畫布 
  125.         $white = imagecolorallocate($tim,255,255,255); 
  126.  
  127.         // 填充縮略畫布 
  128.         imagefill($tim,0,0,$white); 
  129.  
  130.         // 複製並縮略 
  131.         $dwidth = (int)$dinfo['width']*$calc
  132.         $dheight = (int)$dinfo['height']*$calc
  133.  
  134.         $paddingx = (int)($width - $dwidth) / 2; 
  135.         $paddingy = (int)($height - $dheight) / 2; 
  136.  
  137.  
  138.         imagecopyresampled($tim,$dim,$paddingx,$paddingy,0,0,$dwidth,$dheight,$dinfo['width'],$dinfo['height']); 
  139.  
  140.         // 保存圖片 
  141.         if(!$save) { 
  142.             $save = $dst
  143.             unlink($dst); 
  144.         } 
  145.  
  146.         $createfunc = 'image' . $dinfo['ext']; 
  147.         $createfunc($tim,$save); 
  148.  
  149.         imagedestroy($dim); 
  150.         imagedestroy($tim); 
  151.  
  152.         return true; 
  153.  
  154.     } 
  155.  
  156.  
  157.     //寫驗證碼 
  158.     /* 
  159.         author: dabao 
  160.     */ 
  161.     public static function captcha($width=50,$height=25) { 
  162.             //造畫布 
  163.             $image = imagecreatetruecolor($width,$height) ; 
  164.             
  165.             //造背影色 
  166.             $gray = imagecolorallocate($image, 200, 200, 200); 
  167.             
  168.             //填充背景 
  169.             imagefill($image, 0, 0, $gray); 
  170.             
  171.             //造隨機字體顏色 
  172.             $color = imagecolorallocate($image, mt_rand(0, 125), mt_rand(0, 125), mt_rand(0, 125)) ; 
  173.             //造隨機線條顏色 
  174.             $color1 =imagecolorallocate($image, mt_rand(100, 125), mt_rand(100, 125), mt_rand(100, 125)); 
  175.             $color2 =imagecolorallocate($image, mt_rand(100, 125), mt_rand(100, 125), mt_rand(100, 125)); 
  176.             $color3 =imagecolorallocate($image, mt_rand(100, 125), mt_rand(100, 125), mt_rand(100, 125)); 
  177.             
  178.             //在畫布上畫線 
  179.             imageline($image, mt_rand(0, 50), mt_rand(0, 25), mt_rand(0, 50), mt_rand(0, 25), $color1) ; 
  180.             imageline($image, mt_rand(0, 50), mt_rand(0, 20), mt_rand(0, 50), mt_rand(0, 20), $color2) ; 
  181.             imageline($image, mt_rand(0, 50), mt_rand(0, 20), mt_rand(0, 50), mt_rand(0, 20), $color3) ; 
  182.             
  183.             //在畫布上寫字 
  184.             $text = substr(str_shuffle('ABCDEFGHIJKMNPRSTUVWXYZabcdefghijkmnprstuvwxyz23456789'), 0,4) ; 
  185.             imagestring($image, 5, 7, 5, $text$color) ; 
  186.             
  187.             //顯示、銷燬 
  188.             header('content-type: image/jpeg'); 
  189.             imagejpeg($image); 
  190.             imagedestroy($image); 
  191.     } 
  192.  
  193.  

 

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