php GD库类

<?php 
//    header("Content-type:text/html;charset=utf-8");
    /**
     * GD库类
     * 功能:水印 缩略图 验证码
     */
    class ImageTool{
        protected static $erroTxt;
        /**
         * getImgInfo
         * 获取图片信息
         * param filename
         * return Array/False
         */
        protected static function getImgInfo($filename){
            if (!file_exists($filename)) {
                # code...
                ImageTool::$erroTxt = $filename.'图片不存在';
                return false;
            }
            $imgInfo = getimagesize($filename);
            if(!$imgInfo){
                ImageTool::$erroTxt = $filename.'未知图片类型';
                return false;
            }
            $info['width'] = $imgInfo[0];
            $info['height'] = $imgInfo[1];
            $info['ext'] = strtolower( str_replace('/', '', strrchr($imgInfo['mime'],'/')) );
            return $info;
        }
        /**
         * water()
         * 功能:图片加水印
         * param : $dst_img 目标图片
         * $water_img 水印图片
         * $save 目标函数保存路径
         * $pos 水印位置 上右下左 0 1 2 3 默认为2右下角
         * $alpha透明度
         * return 
         */
        public static function water($dst_img,$water_img,$save='',$pos='2',$alpha='50'){
            $dst_info = ImageTool::getImgInfo($dst_img);
            $water_info = ImageTool::getImgInfo($water_img);
            if(!$dst_info || !$water_info){
                return false;
            }
            //判断水印图片是否大于目标图片
            if ($dst_info['width']<=$water_info['width'] || $dst_info['height']<=$water_info['height']) {
                # code...
                ImageTool::$erroTxt='水印图片大于待操作图片';
                return false;
            }
            //生产对应画布函数
            $createDstFun = 'imagecreatefrom'.$dst_info['ext']; 
            $createWaterFun = 'imagecreatefrom'.$water_info['ext'];
            // return $createWaterFun;
            if (!function_exists($createDstFun)) {
                # code...
                ImageTool::$erroTxt=$createDstFun.'函数异常';
                return false;
            }
            if (!function_exists($createWaterFun)) {
                # code...
                ImageTool::$erroTxt=$createWaterFun.'函数异常';
                return false;
            }
            //创建画布
            // ob_clean();
            $dst_im = $createDstFun($dst_img);
            $water_im = $createWaterFun($water_img);
            //画布函数操作
            $src_x = 0;
            $src_y = 0;
            $src_w = $water_info['width'];
            $src_h = $water_info['height'];

            switch ($pos) {
                case '0':
                    # code...左上角
                    $dst_x = 0;
                    $dst_y = 0;
                    break;
                case '1':
                    # code...右上角
                    $dst_x = $dst_info['width']-$water_info['width'];
                    $dst_y = 0;
                    break;
                case '3':
                    # code...左下角
                    $dst_x = 0;
                    $dst_y = $dst_info['height']-$water_info['height'];
                    break;
                
                default:
                    # code...右下角
                    $dst_x = $dst_info['width']-$water_info['width'];
                    $dst_y = $dst_info['height']-$water_info['height'];
                    break;
            }
            // return $pos;
            $rs = imagecopymerge ( $dst_im , $water_im , $dst_x , $dst_y , $src_x , $src_y , $src_w , $src_h , $alpha );
            if( !$rs ){
                ImageTool::$erroTxt='水印添加失败';
                return false;
            }
            $imgInputFun='image'.$dst_info['ext'];
            if(!$save){
                $save = $dst_img;
                unlink($dst_img);
            }
            $imgInputFun($dst_im,$save);
            imagedestroy($dst_im);
            imagedestroy($water_im);
            return true;
        }
        /**
         * thumb()
         * 缩略图
         * param :
         * $src_img 原图片路径
         * $thumb_w,$thumb_h  缩略图宽高
         * $save 缩略图保存路径
         */
        public static function thumb($src_img,$thumb_w='200',$thumb_h='200',$save=''){
            $thumbInfo = ImageTool::getImgInfo($src_img);
            if(!$thumbInfo){
                return false;
            }
            //创建画布
            $dst_im = imagecreatetruecolor($thumb_w,$thumb_h);
            //缩略图函数
            $thumbFun = 'imagecreatefrom'.$thumbInfo['ext'];
            $thumb_im = $thumbFun($src_img);
            //缩略图比例
            $percent = min( $thumb_w/$thumbInfo['width'],$thumb_h/$thumbInfo['height'] );  //小数
            $bg = imagecolorallocate($dst_im, 255, 255, 255);
            imagefill($dst_im, 0, 0, $bg);  //填充背景为白色
            
            $dst_image = $dst_im;
            $src_image = $thumb_im;
            $src_x = 0; 
            $src_y = 0;
            $dst_w = $thumbInfo['width']*$percent; //图片粘贴到缩略图的宽
            $dst_h = $thumbInfo['height']*$percent; //图片粘贴到缩略图的高
            $src_w = $thumbInfo['width']; 
            $src_h = $thumbInfo['height'];
            $dst_x = ($thumb_w - $dst_w)/2;
            $dst_y = ($thumb_w - $dst_h)/2;
            
            $rs = imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
            if(!$rs){
                ImageTool::$erroTxt='生成缩略图失败';
                return false;
            }
            $imgInputFun='image'.$thumbInfo['ext'];
            if(!$save){
                // $path = str_replace('D:/WWW/study/food/', '../', dirname($src_img)).'/thumb_'.basename($src_img);
                $path = dirname($src_img).'/thumb_'.basename($src_img);
            }else{
                // $path = str_replace('D:/WWW/study/food/', '../', dirname($src_img)).'/'.$save.'_'.basename($src_img);
                $path = dirname($src_img).'/'.$save.'_'.basename($src_img);
            }
            $save = $path;
            $imgInputFun($dst_im,$save);
            imagedestroy($dst_im);
            return $path;            
        }
        /**
         * captcha
         * 生成验证码
         * param : $width 验证码宽  $heigth 验证码高
         * $lineNum 线条干扰码数目
         * $alpha 线条干扰码透明度
         * $length 验证码字符长度
         * return String
         */
        public static function captchat($width='70',$height='25',$lineNum='10',$length='4',$alpha='80'){
            $capt_im = imagecreatetruecolor($width, $height);
            $bak_im = imagecreatetruecolor($width, $height);
            $bg = imagecolorallocate($capt_im, 100, 100, 100);
            $color = imagecolorallocate($capt_im, rand(0,255), rand(0,255), rand(0,255));
            imagefill($capt_im, 0, 0, $bg);
            imagefill($bak_im, 0, 0, $bg);
            //干扰线
            for($i=0;$i<$lineNum;$i++){
                $lincolor = imagecolorallocatealpha($capt_im, rand(0,255), rand(0,255), rand(0,255),$alpha);
                imageline($capt_im, rand(0,$width), rand(0,$height), rand(0,$width), rand(0,$height), $lincolor);
            }
            //干扰点
            for($i=0;$i<200;$i++){
                $dotcolor = imagecolorallocatealpha($capt_im, rand(0,255), rand(0,255), rand(0,255),$alpha);
                imagesetpixel($capt_im,rand(0,$width), rand(0,$height), $dotcolor);
            }
            $size =18;
            $angle = 0;
            $x = 2;
            $y = 22;
            $fontfile = 'verdana.ttf';
            $text = ImageTool::randName($length);
            
            imagettftext($capt_im, $size, $angle, $x, $y, $color, $fontfile, $text);
//            验证码扭曲
            $range = 4; //扭曲程度
//            imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
            for($i=0;$i<$width;$i++){
                $y = sin(deg2rad( (720/$width)*$i )) * $range;
                imagecopyresampled($bak_im, $capt_im, $i, $y, $i, 0, $i, $height, $i, $height);
            }
            header("Content-type:image/png");
            imagepng($bak_im);
            imagedestroy($capt_im);
            imagedestroy($bak_im);
            return true;
        }
        /**
         * 生成随机字符
         * arg $n
         * return string
         */
        protected static function randName($n=6){  //$n 代表截取长度
            $str = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ0123456789';
            $str = str_shuffle($str);    //随机打乱一个字符串
            $str = substr($str,0,$n);     //截取子字符串
            return $str;
        }

        /**
         * getError()
         * 获取错误信息
         * param String
         * return String
         */
        public static function getError(){
            return ImageTool::$erroTxt;
        }
    }

//    ImageTool::captchat();

 ?>

 

posted @ 2015-11-05 16:18  人间最美二月天  阅读(275)  评论(0编辑  收藏  举报