zoukankan      html  css  js  c++  java
  • PHP

    第14章 处理图像

    学习要点:

    1.创建图像

    2.简单小案例

    PHP5中,动态图象的处理要比以前容易得多。PHP5php.ini文件中包含了GD扩展包,只需去掉GD扩展包的相应注释就可以正常使用了。PHP5包含的GD库正是升级的GD2库,其中包含支持真彩图像处理的一些有用的JPG功能。

    一般生成的图形,通过PHP的文档格式存放,但可以通过HTML的图片插入方式SRC来直接获取动态图形。比如,验证码、水印、微缩图等。

    一.创建图像

    创建图像的一般流程:

    1).设定标头,告诉浏览器你要生成的MIME类型。

    2).创建一个图像区域,以后的操作都将基于此图像区域。

    3).在空白图像区域绘制填充背景。

    4).在背景上绘制图形轮廓输入文本。

    5).输出最终图形。

    6).清除所有资源。

    7).其他页面调用图像。

    设定标头指定MIME输出类型

    <?php

     header('Content-Type: image/png');

    ?>

    创建一个空白的图像区域

    <?php

     $im = imagecreatetruecolor(200,200);

    ?>

    在空白图像区域绘制填充背景

    <?php

     $blue = imagecolorallocate($im,0,102,255);

     imagefill($im,0,0,$blue);

    ?>

    在背景上绘制图形轮廓输入文本

    <?php

     $white = imagecolorallocate($im,255,255,255);

     imageline($im,0,0,200,200,$white);

     imageline($im,200,0,0,200,$white);

     imagestring($im, 5, 80, 20, "Mr.Lee", $white);

    ?>

    输出最终图形

    <?php

     imagepng($im);

    ?>

    清除所有资源

    <?php

     imagedestroy($im);

    ?>

    其他页面调用创建的图形

    <img src="Demo4.php" alt="PHP创建的图片" />

    二.简单小案例

    简单验证码

    <?php

    header('Content-type: image/png');

    for($Tmpa=0;$Tmpa<4;$Tmpa++){

    $nmsg.=dechex(rand(0,15));

    }

    $im = imagecreatetruecolor(75,25);

    $blue = imagecolorallocate($im,0,102,255);

    $white = imagecolorallocate($im,255,255,255);

    imagefill($im,0,0,$blue);

    imagestring($im,5,20,4,$nmsg,$white);

    imagepng($im);

    imagedestroy($im);

    ?>

    加载已有的图像

    <?php

     header('Content-Type:image/png');

     define('__DIR__',dirname(__FILE__).'\');

     $im = imagecreatefrompng(__DIR__.'222.png');

     $white = imagecolorallocate($im,255,255,255);

     imagestring($im,3,5,5,'http://www.yc60.com',$white);

     imagepng($im);

     imagedestroy($im);

    ?>

    加载已有的系统字体

    <?php

    $text = iconv("gbk","utf-8","李炎恢");

    $font = 'C:WINDOWSFontsSIMHEI.TTF';

    imagettftext($im,20,0,30,30,$white,$font,$text);

    ?>

    图像微缩

    <?php

    header('Content-type: image/png');

    define('__DIR__',dirname(__FILE__).'\');

    list($width, $height) = getimagesize(__DIR__.'222.png');

    $new_width = $width * 0.7;

    $new_height = $height * 0.7;

    $im2 = imagecreatetruecolor($new_width, $new_height);

    $im = imagecreatefrompng(__DIR__.'222.png');

    imagecopyresampled($im2, $im, 0, 0, 0, 0, 

    $new_width, $new_height, $width, $height);

    imagepng($im2);

    imagedestroy($im);

    Imagedestroy($im2);

    ?>

    PS:扫一遍图像函数手册

    ===============================================================

    <?php
    /*     // * 创建图像 *
         //第一步,设置输出图片的类型
        header('Content-Type:image/png');
        
        //第二步,创建一个空白的画布,并返回画布的句柄
        $img = imagecreatetruecolor(200, 200);
        
        //第三步,在空白的画布上面填充背景颜色,并返回此时的句柄
        //创建背景颜色
        $background_color = imagecolorallocate($img,135,206,250 );
        //填充颜色
        imagefill($img, 0, 0, $background_color);
        
        //第四步,在背景上面,绘制文字,线条等其他图形。
        //创建一个白颜色
        $white = imagecolorallocate($img, 255, 255, 255);
        //画一条线
        imageline($img, 0, 0, 200, 200, $white);
        //也可以输出字符串
        imagestring($img, 5, 40, 90, "HF_Ultrastrong", $white);
        
        //第五步,输出创建的图像,可以输出png,jpeg,wbmp,gif
        imagepng($img);
        
        //第六步,销毁所使用的资源
        imagedestroy($img); 
    */
    
    /*
         // * 创建验证码 *
        //文件头...
        header("Content-type: image/png");
        //创建真彩色白纸
        $im = @imagecreatetruecolor(50, 20) or die("建立图像失败");
        //获取背景颜色
        $background_color = imagecolorallocate($im, 255, 255, 255);
        //填充背景颜色(这个东西类似油桶)
        imagefill($im,0,0,$background_color);
        //获取边框颜色
        $border_color = imagecolorallocate($im,200,200,200);
        //画矩形,边框颜色200,200,200
        imagerectangle($im,0,0,49,19,$border_color);
        
        //逐行炫耀背景,全屏用1或0
        for($i=2;$i<18;$i++){
            //获取随机淡色
            $line_color = imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255));
            //画线
            imageline($im,2,$i,47,$i,$line_color);
        }
        
        //设置字体大小
        $font_size=12;
        
        //设置印上去的文字
        $Str[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $Str[1] = "abcdefghijklmnopqrstuvwxyz";
        $Str[2] = "01234567891234567890123456";
        
        //获取第1个随机文字
        $imstr[0]["s"] = $Str[rand(0,2)][rand(0,25)];
        $imstr[0]["x"] = rand(2,5);
        $imstr[0]["y"] = rand(1,4);
        
        //获取第2个随机文字
        $imstr[1]["s"] = $Str[rand(0,2)][rand(0,25)];
        $imstr[1]["x"] = $imstr[0]["x"]+$font_size-1+rand(0,1);
        $imstr[1]["y"] = rand(1,3);
        
        //获取第3个随机文字
        $imstr[2]["s"] = $Str[rand(0,2)][rand(0,25)];
        $imstr[2]["x"] = $imstr[1]["x"]+$font_size-1+rand(0,1);
        $imstr[2]["y"] = rand(1,4);
        
        //获取第4个随机文字
        $imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)];
        $imstr[3]["x"] = $imstr[2]["x"]+$font_size-1+rand(0,1);
        $imstr[3]["y"] = rand(1,3);
        
        //写入随机字串
        for($i=0;$i<4;$i++){
            //获取随机较深颜色
            $text_color = imagecolorallocate($im,rand(50,180),rand(50,180),rand(50,180));
            //画文字
            imagechar($im,$font_size,$imstr[$i]["x"],$imstr[$i]["y"],$imstr[$i]["s"],$text_color);
        }
        
        //显示图片
        imagepng($im);
        //销毁图片
        imagedestroy($im);
    */ 
    
    ?>
  • 相关阅读:
    面向对象的静态属性和静态方法
    面向对象魔术方法及类的自动加载
    面向对象
    mysql cmd 创表查表练习
    创建表 查询表以及添加数据
    windows cmd命令
    4.20 mysq数据库 创建表
    cmd控制数据库初步了解
    Jquery初步了解
    故宫博物院项目 JS功能整理
  • 原文地址:https://www.cnblogs.com/KTblog/p/4956403.html
Copyright © 2011-2022 走看看