- class ImageTool {
- // imageInfo 分析圖片的信息
- // return array()
- public static function imageInfo($image) {
- // 判斷圖片是否存在
- if(!file_exists($image)) {
- return false;
- }
- $info = getimagesize($image);
- if($info == false) {
- return false;
- }
- // 此時info分析出來,是一個數組
- $img['width'] = $info[0];
- $img['height'] = $info[1];
- $img['ext'] = substr($info['mime'],strpos($info['mime'],'/')+1);
- return $img;
- }
- /*
- 加水印功能
- parm String $dst 等操作圖片
- parm String $water 水印小圖
- parm String $save,不填則默認替換原始圖
- */
- public static function water($dst,$water,$save=NULL,$pos=2,$alpha=50) {
- // 先保證2個圖片存在
- if(!file_exists($dst) || !file_exists($water)) {
- return false;
- }
- // 首先保證水印不能比待操作圖片還大
- $dinfo = self::imageInfo($dst);
- $winfo = self::imageInfo($water);
- if($winfo['height'] > $dinfo['height'] || $winfo['width'] > $dinfo['width']) {
- return false;
- }
- // 兩張圖,讀到畫布上! 但是圖片可能是png,可能是jpeg,用什麼函數讀?
- $dfunc = 'imagecreatefrom' . $dinfo['ext'];
- $wfunc = 'imagecreatefrom' . $winfo['ext'];
- if(!function_exists($dfunc) || !function_exists($wfunc)) {
- return false;
- }
- // 動態加載函數來創建畫布
- $dim = $dfunc($dst); // 創建待操作的畫布
- $wim = $wfunc($water); // 創建水印畫布
- // 根據水印的位置 計算粘貼的座標
- switch($pos) {
- case 0: // 左上角
- $posx = 0;
- $posy = 0;
- break;
- case 1: // 右上角
- $posx = $dinfo['width'] - $winfo['width'];
- $posy = 0;
- break;
- case 3: // 左下角
- $posx = 0;
- $posy = $dinfo['height'] - $winfo['height'];
- break;
- default:
- $posx = $dinfo['width'] - $winfo['width'];
- $posy = $dinfo['height'] - $winfo['height'];
- }
- // 加水印
- imagecopymerge ($dim,$wim, $posx , $posy , 0 , 0 , $winfo['width'] , $winfo['height'] , $alpha);
- // 保存
- if(!$save) {
- $save = $dst;
- unlink($dst); // 刪除原圖
- }
- $createfunc = 'image' . $dinfo['ext'];
- $createfunc($dim,$save);
- imagedestroy($dim);
- imagedestroy($wim);
- return true;
- }
- /**
- thumb 生成縮略圖
- 等比例縮放,兩邊留白
- **/
- public static function thumb($dst,$save=NULL,$width=200,$height=200) {
- // 首先判斷待處理的圖片存不存在
- $dinfo = self::imageInfo($dst);
- if($dinfo == false) {
- return false;
- }
- // 計算縮放比例
- $calc = min($width/$dinfo['width'], $height/$dinfo['height']);
- // 創建原始圖的畫布
- $dfunc = 'imagecreatefrom' . $dinfo['ext'];
- $dim = $dfunc($dst);
- // 創建縮略畫布
- $tim = imagecreatetruecolor($width,$height);
- // 創建白色填充縮略畫布
- $white = imagecolorallocate($tim,255,255,255);
- // 填充縮略畫布
- imagefill($tim,0,0,$white);
- // 複製並縮略
- $dwidth = (int)$dinfo['width']*$calc;
- $dheight = (int)$dinfo['height']*$calc;
- $paddingx = (int)($width - $dwidth) / 2;
- $paddingy = (int)($height - $dheight) / 2;
- imagecopyresampled($tim,$dim,$paddingx,$paddingy,0,0,$dwidth,$dheight,$dinfo['width'],$dinfo['height']);
- // 保存圖片
- if(!$save) {
- $save = $dst;
- unlink($dst);
- }
- $createfunc = 'image' . $dinfo['ext'];
- $createfunc($tim,$save);
- imagedestroy($dim);
- imagedestroy($tim);
- return true;
- }
- //寫驗證碼
- /*
- author: dabao
- */
- public static function captcha($width=50,$height=25) {
- //造畫布
- $image = imagecreatetruecolor($width,$height) ;
- //造背影色
- $gray = imagecolorallocate($image, 200, 200, 200);
- //填充背景
- imagefill($image, 0, 0, $gray);
- //造隨機字體顏色
- $color = imagecolorallocate($image, mt_rand(0, 125), mt_rand(0, 125), mt_rand(0, 125)) ;
- //造隨機線條顏色
- $color1 =imagecolorallocate($image, mt_rand(100, 125), mt_rand(100, 125), mt_rand(100, 125));
- $color2 =imagecolorallocate($image, mt_rand(100, 125), mt_rand(100, 125), mt_rand(100, 125));
- $color3 =imagecolorallocate($image, mt_rand(100, 125), mt_rand(100, 125), mt_rand(100, 125));
- //在畫布上畫線
- imageline($image, mt_rand(0, 50), mt_rand(0, 25), mt_rand(0, 50), mt_rand(0, 25), $color1) ;
- imageline($image, mt_rand(0, 50), mt_rand(0, 20), mt_rand(0, 50), mt_rand(0, 20), $color2) ;
- imageline($image, mt_rand(0, 50), mt_rand(0, 20), mt_rand(0, 50), mt_rand(0, 20), $color3) ;
- //在畫布上寫字
- $text = substr(str_shuffle('ABCDEFGHIJKMNPRSTUVWXYZabcdefghijkmnprstuvwxyz23456789'), 0,4) ;
- imagestring($image, 5, 7, 5, $text, $color) ;
- //顯示、銷燬
- header('content-type: image/jpeg');
- imagejpeg($image);
- imagedestroy($image);
- }
- }