【细说php】16章 关于php动态图像处理

来源:互联网 发布:linux系统怎么用java 编辑:程序博客网 时间:2024/06/03 22:58
细说php
  16.php动态图像处理
    GD库
      1,创建画布
      2,绘制图像
      3,输出图像
      4,释放资源
    画布管理

      resource imagecreate ( int $x_size, int $y_size ) //新建一个基于调色板的图像 

      resource imagecreatetruecolor ( int $x_size, int $y_size )//新建一个真彩色图像

      bool imagedestroy ( resource $image )//销毁一图像
    设置颜色
      int imagecolorallocate ( resource $image, int $red, int $green, int $blue ) //为一副图像分配颜色
    生成图像

      bool imagegif (resource $image [, string $filename] ) //以 GIF 格式将图像输出 

bool imagejpeg(resource $image [, string $filename [, int $quality]]) //以 JPEG 格式将图像输出 

bool imagepng ( resource $image [, string $filename] ) // 以PNG 格式将图像输出 

bool imagewbmp ( resource $image [, string $filename [, int $foreground]] )以 WBMP 格式将图像输出

    绘制图像
      1,图形区域填充bool imagefill ( resource $image, int $x, int $y, int $color )
      2,绘制点和线 
        bool imagesetpixel ( resource $image, int $x, int $y, int $color )(画一单一像素)
        bool imageline ( resource $image, int $x1, int $y1, int $x2, int $y2, int $color )//画一条线
      3,绘制矩形

        bool imagerectangle ( resource $image, int $x1, int $y1, int $x2, int $y2, int $color ) //画一个矩形

 bool imagefilledrectangle ( resource image, int $x1, int $y1, int $x2, int $y2, int $color ) //画一个矩形并填充

      4,绘制多边形

        bool imagepolygon (resource $image, array $points, int $num_points, int $color ) //画一个多边形

 bool imagefilledpolygon (resource $image, $array $points, int $num_points, int $color) //画一个多边形并且填充

      5.绘制椭圆

        bool imageellipse ( resource $image, int $cx, int $cy, int $w, int $h, int $color ) //绘制一个椭圆 

bool imagefilledellipse ( resource $image, int $cx, int $cy, int $w, int $h, int $color ) //绘制一个椭圆并填充

      6,绘制弧线
        bool imagearc ( resource $image, int $cx, int $cy, int $w, int $h, int $s, int $e, int $color ) //绘制弧线
    在图像上绘制文字

      bool imagestring ( resource $image, int $font, int $x, int y, string $s, int $color ) //水平地画一行字符串 

bool imagestringup ( resource $image, int $font, int $x, int y, string $s, int $color ) //垂直地画一行字符串

 bool imagechar ( resource $image, int $font, int $x, int $y, char $c, int $color ) //水平地画一个字符 

bool imagecharup ( resource $image, int $font, int $x, int $y, char $c, int $color ) //垂直地画一个字符

另一原型

      array imagettftext(resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text )

附件 验证码设计源码:

<?php/** file: vcode.class.php 验证码类,类名Vcode*/class  Vcode {private $width;                               //验证码图片的宽度private $height;                              //验证码图片的高度private $codeNum;                             //验证码字符的个数private $disturbColorNum;                     //干扰元素数量private $checkCode;                           //验证码字符private $image;                               //验证码资源/** * 构造方法用来实例化验证码对象,并为一些成员属性初使化        * @paramint$width设置验证码图片的宽度,默认宽度值为80像素         * @paramint$height设置验证码图片的高度,默认高度值为20像素        * @paramint$codeNum设置验证码中字母和数字的个数,默认个数为4个   */ function __construct($width=80, $height=20, $codeNum=4) {$this->width = $width;                    $this->height = $height;                   $this->codeNum = $codeNum;   //验证码字符数          $number = floor($height*$width/15);if($number > 240-$codeNum)$this->disturbColorNum = 240-$codeNum;else$this->disturbColorNum = $number; //干扰元素$this->checkCode = $this->createCheckCode(); //验证码字符}/** * 用于输出验证码图片,也向服务器的SESSION中保存了验证码,使用echo 输出对象即可 */function __toString(){/* 加到session中, 存储下标为code */$_SESSION["code"] = strtoupper($this->checkCode);  //随机产生的字符内容存在session里面$this->outImg();             return '';}/* 内部使用的私有方法,用于输出图像 */private function outImg(){                      $this->getCreateImage();     //创建画布资源            $this->setDisturbColor();    //干扰元素             $this->outputText();         //输出文本           $this->outputImage();        //输出            }/* 内部使用的私有方法,用来创建图像资源,并初使化背影 */private function getCreateImage(){              $this->image = imagecreatetruecolor($this->width,$this->height); //创建画布资源      $backColor = imagecolorallocate($this->image, rand(225,255),rand(225,255),rand(225,255));  //设置图像的颜色@imagefill($this->image, 0, 0, $backColor);$border = imageColorAllocate($this->image, 0, 0, 0);imageRectangle($this->image,0,0,$this->width-1,$this->height-1,$border);}/* 内部使用的私有方法,随机生成用户指定个数的字符串,去掉了容易混淆的字符oOLlz和数字012 */private function createCheckCode(){           $code="3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY";for($i=0; $i<$this->codeNum; $i++) {$char = $code{rand(0,strlen($code)-1)};$ascii.= $char;}return $ascii;//字符串 随机}/* 内部使用的私有方法,设置干扰像素,向图像中输出不同颜色的点 */private function setDisturbColor() {    for($i=0; $i <= $this->disturbColorNum; $i++) {$color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));   imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);}for($i=0; $i<10; $i++){$color=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));imagearc($this->image,rand(-10,$this->width),rand(-10,$this->height),rand(30,300),rand(20,200),55,44,$color);}  }/* 内部使用的私有方法,随机颜色、随机摆放、随机字符串向图像中输出 */private function outputText() {       for ($i=0; $i<=$this->codeNum; $i++) {$fontcolor = imagecolorallocate($this->image, rand(0,128), rand(0,128), rand(0,128));$fontSize = rand(3,5);$x = floor($this->width/$this->codeNum)*$i+3;   $y = rand(0,$this->height-imagefontheight($fontSize));imagechar($this->image, $fontSize, $x, $y, $this->checkCode{$i}, $fontcolor);    }}/* 内部使用的私有方法,自动检测GD支持的图像类型,并输出图像 */private function outputImage(){              if(imagetypes() & IMG_GIF){         header("Content-type: image/gif");  imagegif($this->image);          }elseif(imagetypes() & IMG_JPG){   header("Content-type: image/jpeg"); imagejpeg($this->image, "", 0.5);  }elseif(imagetypes() & IMG_PNG){     header("Content-type: image/png");  imagepng($this->image);          }elseif(imagetypes() & IMG_WBMP){    header("Content-type: image/vnd.wap.wbmp");   imagewbmp($this->image);       }else{                              die("PHP不支持图像创建!"); }}/* 析构方法,在对象结束之前自动销毁图像资源释放内存 */function __destruct(){                     imagedestroy($this->image);           }}

zendFramework -Verify控制器-verify行为控制器:

   public function verifyAction()    {       //初始化一下       session_start();       $Verify= new Vcode(); //产生了一张图片       echo $Verify;       exit();       //导航一下      // $this->view->render('Verify/index.phtml');      // exit();//使用exit 是为了显示数据 防止页面进行 不会再走前端页面     }

html页面:

<img id='img1' src="http://zf.my.com/Verify/verify" alt="验证码"> 



0 0
原创粉丝点击