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();

 ?>
时间: 2024-07-31 01:56:15

php GD库类的相关文章

php使用GD库实现图片水印和缩略图——封装成类

学完了如何使用GD库来实现对图片的各种处理,那么我们可以发现,不管哪种方法,都有相似之处,如果我们把这些相似的地方和不相似的地方都封装成类,这样就可以提升代码的速度,而且节省了很多时间,废话不多说,来人,上代码! 首先,先创建一个PHP文件:class.php(自定义) 我们知道,在 在原始图片中添加文字水印:http://www.cnblogs.com/finalanddistance/p/7243346.html 在原始图片中添加图片水印:http://www.cnblogs.com/fin

GD库的基本信息,图像的旋转、水印、缩略图、验证码,以及图像类的封装

GD库检测 <?php phpinfo(); ?> GD库安装• Windows 使用phpstudy • Linux 编译安装 –with-gd• Linux 编译安装扩展 GD库支持的图像格式 使用 gd_info() 函数 检测服务器支持的图像格式 图像信息处理 <?php //获取图像详细信息 $image = '../image/b.png'; $info = getimagesize($image); var_dump($info); $string = file_get_c

PHP学习笔记-GD库与Jpgraph的使用

转载请标明出处: http://blog.csdn.net/hai_qing_xu_kong/article/details/52281196 本文出自:[顾林海的博客] 前言 学习PHP从第一篇笔记到现在这篇,已经十多篇了,每天花时间去学习是需要毅力的,好在自己对IT这行也是比较感兴趣,算是每天自娱自乐吧,下周一就去考科目三了,想想也是醉了,拖这么长时间. GD库 GD库是一个开放的动态创建图像.源代码公开的函数库,可以从官方网站http://www.boutell.com/gd处下载.目前,

GD库使用小结---1

因为一开始,“大家”都说一般任务中,用php操作图片不常见,像我们这种基本业务型的,就更用不到了,所以先别看,偶就没有看.现在有机会了自然要来玩一把. 以前学过C#的GDI+,交了课程设计后忘得一干二净.又被迫学了点MFC的画图,觉得这是最蛋疼的画图过程.去年做了个小任务时用到了js图表控件,用的是封装好的js库直接调方法,然后发现这是用HTML5的canvas元素结合js打造而成,这些chart控件很多很漂亮:jsChart.HighChart.EChart.aChart.Chart.js等等

Linux下通过源码编译GD库

因为之前都通过源码直接编译安装的lamp环境,所以好多扩展库都是没有安装的,突然现在要用到一个验证码类,imagecreate函数显示未定义,所以就来安装编译下GD库, 首先需要先安装 gd 前置库 : freetype ,jpegsrc,libpng. freetype wget "http://download.savannah.gnu.org/releases/freetype/freetype-2.4.0.tar.bz2" tar jxvf freetype-2.4.0.tar

PHP环境安装配置(含IIS+PHP+MySQL+Zend Optimizer+GD库+phpMyAdmin)

IIS+PHP+MySQL+Zend Optimizer+GD库+phpMyAdmin安装配置一.软件准备 1.windos20032.IIS6.03.php-5.2.11-Win324.mysql-5.0.27-win325.ZendOptimizer-3.3.3-Windows6.phpMyAdmin-3.2.2.1(重庆萤火虫专用版) 二.开始安装 请确认已经安装好了windos2003+IIS6.0 第一步:安装PHP 1.将下载得到的php-5.2.11-win32解压到自己需要放置的

PHP系列(十)GD库

GD库 1.Php中gd库的使用 Gd库是一个画图或处理有图片的函数库 2.使用gd库画图 GD库图像绘制的步骤 在PHP中创建一个图像应该完成如下所示的4个步骤: 1.创建一个背景图像(也叫画布),以后的操作都基于此背景图像. 2.在背景上绘制图像轮廓或输入文本. 3.输出最终图形 4.释放资源 代码: <?php //1. 创建画布 $im = imageCreateTrueColor(200, 200); //建立空白画布背景 $white = imageColorAllocate($im

解决PHP开启gd库无效的问题

最近需要重新安装PHP,以前一直使用的都是XAMPP,基本上都不需要自己配置,现在准备直接下载官方原版的Apache和PHP,自己来慢慢摸索如何继承配置. 我下载的Apache版本为2.2.25,PHP版本为5.4.19,将Apache与PHP集成配置好后(PHP安装目录为:F:\php5.4.19),记得PHP默认没有开启GD库的支持,需要自行开启.于是就打开PHP安装目录/php.ini配置文件,找到如下内容: ;extension=php_gd2.dll 按照网上搜索得来的方法,去掉前面表

PHP利用GD库画图和生成验证码图片

首先得确定php.ini设置有没有打开GD扩展功能,测试如下 print_r(gd_info()); 如果有打印出内容如下,则说明GD功能有打开: Array ( [GD Version] => bundled (2.0.34 compatible) [FreeType Support] => 1 [FreeType Linkage] => with freetype [T1Lib Support] => 1 [GIF Read Support] => 1 [GIF Crea