最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php 方便水印和縮略圖的圖形類

 更新時間:2009年05月21日 02:58:48   作者:  
這是個方便做水印和縮略圖的類,簡化一些操作,按照一些CSS的習(xí)慣寫參數(shù)
復(fù)制代碼 代碼如下:

<?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("沒有這個類方法或函數(shù)");
        }
        return call_user_func_array($funcstr,$arg);
    }

    /*
    *創(chuàng)建圖像
    *@param string/int 圖片文件路徑或者寬度
    *@param int 高度,可省略
    *@param string 6位16進(jìn)制整數(shù)
    */
    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("對不起,參數(shù)錯誤!");
            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("對不起,參數(shù)錯誤!");
        }
        //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 可選,是否拉伸,默認(rèn)不拉伸
    */
    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 字符串相關(guān)的參數(shù),為一個關(guān)聯(lián)數(shù)組,left 為距左邊距離,right為距右邊距離,left優(yōu)先,top為距頂部距離,bottom為距底部距離,top優(yōu)先;angle為角度,color為6位數(shù)16進(jìn)制顏色,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 圖片文件路徑或這圖片標(biāo)識符
    *@param array $arg 字符串相關(guān)的參數(shù),為一個關(guān)聯(lián)數(shù)組,left 為距左邊距離,right為距右邊距離,left優(yōu)先,top為距頂部距離,bottom為距底部距離,top優(yōu)先;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 要轉(zhuǎn)存的文件路徑
    *@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);
    }
}
*/
?>

這是簡單的用法實(shí)例
復(fù)制代碼 代碼如下:

$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");

相關(guān)文章

  • PHP設(shè)計模式之工廠模式詳解

    PHP設(shè)計模式之工廠模式詳解

    這篇文章主要為大家詳細(xì)介紹了PHP設(shè)計模式之工廠模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 記錄PHP錯誤日志 display_errors與log_errors的區(qū)別

    記錄PHP錯誤日志 display_errors與log_errors的區(qū)別

    錯誤回顯,一般常用語開發(fā)模式,但是很多應(yīng)用在正式環(huán)境中也忘記了關(guān)閉此選項(xiàng)。錯誤回顯可以暴露出非常多的敏感信息,為攻擊者下一步攻擊提供便利。推薦關(guān)閉此選項(xiàng)
    2012-10-10
  • 解析數(shù)組非數(shù)字鍵名引號的必要性

    解析數(shù)組非數(shù)字鍵名引號的必要性

    以下是對數(shù)組非數(shù)字鍵名引號的必要性進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下
    2013-08-08
  • 為PHP5.4開啟Zend OPCode緩存

    為PHP5.4開啟Zend OPCode緩存

    PHP5.5開始內(nèi)置了Zend OPcache的緩存系統(tǒng),這個系統(tǒng)在5.2-5.4下也是可以使用的。經(jīng)過測試,在php5.4下的加速性能明顯,效果比5.2+ea還要好,在此推薦安裝
    2014-12-12
  • php讀取數(shù)據(jù)庫信息的幾種方法

    php讀取數(shù)據(jù)庫信息的幾種方法

    連接到一個 url 地址為localhost 、 端口為 3306 的mysql服務(wù)器上。mysql服務(wù)器的帳號是"root",密碼是"9999"。mysql 服務(wù)器上有一個數(shù)據(jù)庫 ok , 數(shù)據(jù)庫里有一個表 abc。表 abc 一共為兩列,列名分別是 "id" 和 "name" ,將 abc 里的所有數(shù)據(jù)讀出來。
    2008-05-05
  • php多文件上傳功能實(shí)現(xiàn)原理及代碼

    php多文件上傳功能實(shí)現(xiàn)原理及代碼

    對多圖片上傳功能小小的研究了一下,把下面的代碼整理出來,方便以后使用,感興趣的各位可以參考下哈,希望對你有所幫助
    2013-04-04
  • PHP單元測試PHPUnit簡單用法示例

    PHP單元測試PHPUnit簡單用法示例

    這篇文章主要介紹了PHP單元測試PHPUnit簡單用法,結(jié)合實(shí)例形式分析了PHPUnit的安裝、單元測試簡單操作技巧,需要的朋友可以參考下
    2018-07-07
  • php出現(xiàn)Cannot modify header information問題的解決方法大全

    php出現(xiàn)Cannot modify header information問題的解決方法大全

    我做了一個統(tǒng)一的出錯提示函數(shù),在函數(shù)執(zhí)行里面,先處理出錯的地址寫入cookie以方便用戶登陸以后可以直接跳轉(zhuǎn)到要執(zhí)行的這個頁面,可是發(fā)現(xiàn)在服務(wù)器上測試時,竟然提示本地沒有出現(xiàn)的錯誤: Warning: Cannot modify header information - headers already sent by....
    2008-04-04
  • php自定義函數(shù)截取漢字長度

    php自定義函數(shù)截取漢字長度

    php中截取漢字長度的方法有很多,本例通過自定義函數(shù)來完成這個需求,感興趣的朋友可以參考下
    2014-05-05
  • 分享一則PHP定義函數(shù)代碼

    分享一則PHP定義函數(shù)代碼

    這篇文章主要介紹了分享一則PHP定義函數(shù)代碼,主要是讓大家熟悉下php的語法格式以及php中插入HTML代碼的方式,希望能夠給到大家一些幫助。
    2015-02-02

最新評論

柳林县| 东兴市| 江北区| 金川县| 和田市| 广昌县| 兴山县| 乐东| 城步| 来凤县| 岗巴县| 鞍山市| 卢氏县| 张家界市| 襄垣县| 西吉县| 永定县| 内江市| 贵德县| 冀州市| 衡阳市| 龙口市| 铜川市| 美姑县| 那坡县| 九江市| 五华县| 屏南县| 灵武市| 玛曲县| 儋州市| 枞阳县| 盐山县| 华蓥市| 洪泽县| 铁岭市| 古蔺县| 饶河县| 西青区| 萍乡市| 长宁县|