php 方便水印和缩略图的图形类


Posted in PHP onMay 21, 2009
<?php /* 
*@author    夜无眠    27262681@qq.com 
*copyright    http://www.gowake.cn 
*/ 
class img { 
    function __construct($arg = null) { 
        $args = func_get_args(); 
        if($arg == null) { 
            return null; 
        } 
        $this->im = call_user_func_array(array($this,'create'),$args); 
    } 
    function __call($func,$arg) { 
        if(function_exists('image'.$func)) { 
            $funcstr = 'image'.$func; 
        }elseif (function_exists($func)){ 
            $funcstr = $func; 
        }else { 
            error("没有这个类方法或函数"); 
        } 
        return call_user_func_array($funcstr,$arg); 
    } 
    /* 
    *创建图像 
    *@param string/int 图片文件路径或者宽度 
    *@param int 高度,可省略 
    *@param string 6位16进制整数 
    */ 
    function create($arg = null) { 
        $args = func_get_args(); 
        if(is_file($args[0])) { 
            $this->file = $args[0]; 
            $size = getimagesize($this->file) or error('图片类型错误'); 
            $this->size = $this->size ? $this->size : $size; 
            $type = image_type_to_extension($size[2],false); 
            $this->type = $this->type ? $this->type : $type; 
            $createfunc = 'imagecreatefrom'.$type; 
            $im = $createfunc($this->file); 
        }elseif((int)$args[0]>0 and (int)$args[1]>0) { 
            $im = imagecreatetruecolor((int)$args[0],(int)$args[1]) or error("对不起,参数错误!"); 
            if(!$args[2]) { 
                $color = hexdec('7fffffff'); 
                imagecolortransparent($im,$color); 
            }else { 
                $color = hexdec(str_replace('#','',$args[2])); 
            } 
            $this->size = $this->size ? $this->size : array((int)$args[0] ,(int)$args[1]); 
            imagefill($im, 1, 1, $color); 
        }else { 
            error("对不起,参数错误!"); 
        } 
        //imagealphablending($im,false);//这两行用来记录透明通道 
        imagesavealpha($im,true); 
        imageinterlace($im,true);//开启隔行扫描 
        return $im; 
    } 
    /* 
    *生成缩略图 
    *@param int $w 新图片的宽度 
    *@param int $h 新图片的宽度 
    *@param string/bool $color 可选,新图片的背景色,false或空为透明 
    *@param bool $lashen 可选,是否拉伸,默认不拉伸 
    */ 
    function suolue($w = null,$h = null,$color = false,$lashen = false) { 
        $w_o = imagesx($this->im); 
        $h_o = imagesy($this->im); 
        if($w == null and $h != null) { 
            $w = $h * $w_o/$h_o; 
        }elseif ($w != null and $h == null){ 
            $h = $w * $h_o/$w_o; 
        } 
        $this->size = null; 
        $im = $this->create($w,$h,$color); 
        $w_n = $w; 
        $h_n = $h; 
        if($w_o/$h_o > $w/$h) { 
            $h_n = $w*$h_o/$w_o; 
            $y = ($h-$h_n)/2; 
        }elseif ($w_o/$h_o < $w/$h){ 
            $w_n = $h*$w_o/$h_o; 
            $x = ($w-$w_n)/2; 
        } 
        if($lashen) { 
            $w_n = $w; 
            $h_n = $h; 
            $x = 0; 
            $y = 0; 
        } 
        imagecopyresampled($im,$this->im,$x,$y,0,0,$w_n,$h_n,$w_o,$h_o); 
        //imagedestroy($this->im); 
        $this->im = $im; 
        return $im; 
    } 
    /* 
    *在图片上写字 
    *@param string $str 要写的字符串 
    *@param array $arg 字符串相关的参数,为一个关联数组,left 为距左边距离,right为距右边距离,left优先,top为距顶部距离,bottom为距底部距离,top优先;angle为角度,color为6位数16进制颜色,touming为文字透明度,font为字体文件 
    */ 
    function write($str = '' , $arg = array()) { 
        $size = $arg['size'] ? $arg['size'] : 20; 
        $angle = $arg['angle'] ? $arg['angle'] : 0 ; 
        $color = $arg['color'] ? $arg['color'] : '000000'; 
        $touming = $arg['touming'] ? $arg['touming'] : 100; 
            $touming = dechex((100-$touming)*127/100); 
            $color = hexdec($touming.str_replace("#","",$color)); 
        $font = $arg['font'] ? $arg['font'] : 'arial.ttf'; 
        $boxarr = imagettfbbox($size,$angle,$font,$str); 
        $w = imagesx($this->im); 
        $h = imagesy($this->im); 
        $x_l = $x_r = $boxarr[0]; 
        $y_t = $y_b = $boxarr[1]; 
        for($i=0;$i<7;$i = $i+2) { 
            $x_l = $boxarr[$i] < $x_l ? $boxarr[$i] : $x_l; 
            $x_r = $boxarr[$i] > $x_r ? $boxarr[$i] : $x_r; 
            $y_t = $boxarr[$i+1] < $y_t ? $boxarr[$i+1] : $y_t; 
            $y_b = $boxarr[$i+1] > $y_b ? $boxarr[$i+1] : $y_b; 
        } 
        $width = $x_r - $x_l; 
        $height = $y_b - $y_t; 
        /*获取精确偏移量*/ 
        $im = $this->create($width*4,$height*4); 
        $tm = hexdec('7fffffff'); 
        imagettftext($im,$size,$angle,$width*2,$height*2,$color,$font,$str); 
        for($i=0;$i<$width*4;$i++) { 
            for($ii=0;$ii<$height*4;$ii++) { 
                if(imagecolorat($im,$i,$ii) != $tm) { 
                    $x_l = $i; 
                    break(2); 
                } 
            } 
        } 
        for($i=0;$i<$height*4;$i++) { 
            for($ii=$x_l;$ii<$width*4;$ii++) { 
                if(imagecolorat($im,$ii,$i) != $tm) { 
                    $y_t = $i; 
                    break(2); 
                } 
            } 
        } 
        for($i=$width*4-1;$i>0;$i--) { 
            for($ii=$y_t;$ii<$height*4;$ii++) { 
                if(imagecolorat($im,$i,$ii) != $tm) { 
                    $x_r = $i; 
                    break(2); 
                } 
            } 
        } 
        for($i=$height*4-1;$i>0;$i--) { 
            for($ii=$x_l;$ii<=$x_r;$ii++) { 
                if(imagecolorat($im,$ii,$i) != $tm) { 
                    $y_b = $i; 
                    break(2); 
                } 
            } 
        } 
        $x_off = $x_l - $width*2; 
        $y_off = $y_b - $height*2; 
        $width = $x_r - $x_l; //精确宽度 
        $height = $y_b - $y_t; //精确高度 
        imagedestroy($im); 
        if(isset($arg['left'])) { 
            $x = (int)$arg['left'] - $x_off; 
        }elseif (isset($arg['right'])){ 
            $x = $w - (int)$arg['right'] - $width - $x_off; 
        }else { 
            $x = ($w - $width)/2 - $x_off; 
        } 
        if(isset($arg['top'])) { 
            $y = (int)$arg['top'] - $y_off + $height; 
        }elseif (isset($arg['bottom'])){ 
            $y = $h - (int)$arg['bottom'] - $y_off; 
        }else { 
            $y = ($h + $height)/2 - $y_off; 
        } 
        imagettftext($this->im,$size,$angle,$x,$y,$color,$font,$str); 
        return $this->im; 
    } 
    /* 
    *合并图片(如图片水影) 
    *@param string/resource $file 图片文件路径或这图片标识符 
    *@param array $arg 字符串相关的参数,为一个关联数组,left 为距左边距离,right为距右边距离,left优先,top为距顶部距离,bottom为距底部距离,top优先;touming为文字透明度 
    */ 
    function merge($file,$arg = array()) { 
        if(is_file($file)) { 
            $imc = $this->create($file); 
        }elseif(gettype($file)=='resource') { 
            $imc = $file; 
        }else { 
            error("没有图片"); 
        } 
        $touming = $arg['touming'] ? (int)$arg['touming'] : 100 ; 
        $w = imagesx($this->im); 
        $h = imagesy($this->im); 
        $width = imagesx($imc); 
        $height = imagesy($imc); 
        if(isset($arg['left'])) { 
            $x = (int)$arg['left']; 
        }elseif (isset($arg['right'])){ 
            $x = $w - (int)$arg['right'] - $width; 
        }else { 
            $x = ($w - $width)/2; 
        } 
        if(isset($arg['top'])) { 
            $y = (int)$arg['top']; 
        }elseif (isset($arg['bottom'])){ 
            $y = $h - $height - $arg['bottom']; 
        }else { 
            $y = ($h - $height)/2; 
        } 
        imagecopymergegray($this->im,$imc,$x,$y,0,0,$width,$height,$touming); 
    } 
    /* 
    *输出图片 
    *@param string $type 
    *@param string $filename 要转存的文件路径 
    *@param int $zhiliang jpeg图片特有的,图像清晰度 
    */ 
    function display($type = null,$filename = null,$zhiliang = null) { 
        if($type == null) { 
            $type = $this->type ? $this->type : 'jpg'; 
        } 
        if(($type == 'jpeg' or $type == 'jpg') and $zhiliang == null) { 
            $type = 'jpeg'; 
            $zhiliang = 100; 
        } 
        if($filename == null) { 
            header('Content-type: image/'.$type); 
        } 
        $displayfunc = 'image'.$type; 
        $displayfunc($this->im,$filename,$zhiliang); 
        imagedestroy($this->im); 
    } 
    function randcolor($a,$b) { 
        $a = $a>255 ? 255 : (int)$a; 
        $a = $a<0 ? 0 : (int)$a; 
        $b = $b>255 ? 255 : (int)$b; 
        $b = $b<0 ? 0 : (int)$b; 
        for($i=0;$i<3;$i++) { 
            $color .= str_pad(dechex(mt_rand($a,$b)), 2, "0", STR_PAD_LEFT); 
        } 
        return $color; 
    } 
} 
/* 
function error($msg,$debug = false) { 
    $err = new Exception($msg); 
    $str = "<pre>\n<span style="color:red" style="color:red">错误:</span>\n".print_r($err->getTrace(),1)."\n</pre>"; 
    if($debug == true) { 
        file_put_contents(date('Y-m-d').".log",$str); 
        return $str; 
    }else{ 
        die($str); 
    } 
} 
*/ 
?>

这是简单的用法实例
$img = new img('a.png'); 
$m = $img->im; 
$im = $img->suolue(100); 
$img->im = $m; 
$img->suolue(300); 
$img->merge($m,array('left'=>0,'top'=>0,'touming'=>60)); 
$img->merge($im,array('right'=>0,'top'=>0,'touming'=>60)); 
$img->merge($im,array('left'=>0,'bottom'=>0,'touming'=>60)); 
$img->merge($im,array('right'=>0,'bottom'=>0,'touming'=>60)); $img->write("春天来了",array('left'=>0,'top'=>0,'size'=>30,'color'=>$img->randcolor(0,180),'angle'=>-45,'font'=>'simfang.ttf','touming'=>80)); 
$img->write("春天来了",array('left'=>0,'bottom'=>0,'size'=>30,'color'=>$img->randcolor(0,180),'angle'=>45,'font'=>'simfang.ttf','touming'=>80)); 
$img->write("春天来了",array('right'=>0,'bottom'=>0,'size'=>30,'color'=>$img->randcolor(0,180),'angle'=>-45,'font'=>'simfang.ttf','touming'=>80)); 
$img->write("春天来了",array('right'=>0,'top'=>0,'size'=>30,'color'=>$img->randcolor(0,180),'angle'=>45,'font'=>'simfang.ttf','touming'=>80)); 
$img->display("gif");
PHP 相关文章推荐
PHP pathinfo()获得文件的路径、名称等信息说明
Sep 13 PHP
比较详细PHP生成静态页面教程
Jan 10 PHP
PHP将整个网站生成HTML纯静态网页的方法总结
Feb 05 PHP
解析PHP中如何将数组变量写入文件
Jun 06 PHP
常用的php图片处理类(水印、等比缩放、固定高宽)分享
Jun 19 PHP
使用PHP和JavaScript判断请求是否来自微信内浏览器
Aug 18 PHP
CI框架常用方法小结
May 17 PHP
老生常谈php中传统验证与thinkphp框架(必看篇)
Jun 10 PHP
PHP多线程模拟实现秒杀抢单
Feb 07 PHP
使用PHPExcel导出Excel表
Sep 08 PHP
如何在centos8自定义目录安装php7.3
Nov 28 PHP
php计数排序算法的实现代码(附四个实例代码)
Mar 31 PHP
简单的php 验证图片生成函数
May 21 #PHP
PHP 数组入门教程小结
May 20 #PHP
php 无限级 SelectTree 类
May 19 #PHP
PHP日期时间函数的高级应用技巧
May 16 #PHP
PHP 模拟登陆MSN并获得用户信息
May 16 #PHP
抓取YAHOO股票报价的类
May 15 #PHP
PHP 采集心得技巧
May 15 #PHP
You might like
eaglephp使用微信api接口开发微信框架
2014/01/09 PHP
PHPStrom中实用的功能和快捷键大全
2015/09/23 PHP
深入浅析PHP7.0新特征(五大新特征)
2015/10/29 PHP
PHP7 list() 函数修改
2021/03/09 PHP
javascript陷阱 一不小心你就中招了(字符运算)
2013/11/10 Javascript
js中replace的用法总结
2013/12/27 Javascript
基于JQuery实现的图片自动进行缩放和裁剪处理
2014/01/31 Javascript
js获得页面的高度和宽度的方法
2014/02/23 Javascript
javascript感应鼠标图片透明度显示的方法
2015/02/24 Javascript
JavaScript获得当前网页来源页面(即上一页)的方法
2015/04/03 Javascript
jquery移动端TAB触屏切换实现效果
2020/12/22 Javascript
JavaScript中0和&quot;&quot;比较引发的问题
2016/05/26 Javascript
JavaScript解八皇后问题的方法总结
2016/06/12 Javascript
nodeJS删除文件方法示例
2016/12/25 NodeJs
svg动画之动态描边效果
2017/02/22 Javascript
JS实现简单短信验证码界面
2017/08/07 Javascript
Node.js如何使用Diffie-Hellman密钥交换算法详解
2017/09/05 Javascript
微信小程序非跳转式组件授权登录的方法示例
2019/05/22 Javascript
Vue实现回到顶部和底部动画效果
2019/07/31 Javascript
小程序实现图片预览裁剪插件
2019/11/22 Javascript
vue中defineProperty和Proxy的区别详解
2020/11/30 Vue.js
[01:10]DOTA2 Supermajor:英雄,由我们见证
2018/05/14 DOTA
Python标准库defaultdict模块使用示例
2015/04/28 Python
在Linux系统上通过uWSGI配置Nginx+Python环境的教程
2015/12/25 Python
Python解析最简单的验证码
2016/01/07 Python
Python简单生成8位随机密码的方法
2017/05/24 Python
python 列表删除所有指定元素的方法
2018/04/19 Python
Python实现基于POS算法的区块链
2018/08/07 Python
浅析Python数字类型和字符串类型的内置方法
2019/12/22 Python
5分钟快速掌握Python定时任务框架的实现
2021/01/26 Python
适合各种场合的美食礼品:Harry & David
2016/08/03 全球购物
介绍一下Linux内核的排队自旋锁
2014/01/04 面试题
大学秋游活动方案
2014/02/11 职场文书
经典禁毒标语
2014/06/16 职场文书
2015年重阳节慰问信
2015/03/23 职场文书
JavaScript实现一键复制内容剪贴板
2022/07/23 Javascript