PHP 图像处理

1、window平台下GD库的安装

phpinfo() 查看库是否可用

var_dump (extension_loaded('GD')); 查看是否已经载入GD库


2、读取文件输出到页面中

int readfile ( string $filename [, bool $use_include_path = false [, resource $context ]] )

读取文件并写入到输出缓冲。

header('Content-Type:image/gif');
readfile('1.jpg');

3、绘制简单图形

你需要一块画布才能挥斥方遒。

在页面中显示图片

设置HTTP头信息

  • header('Content-Type:image/jpg');

  • header('Content-Type:image/jpeg');

  • header('Content-Type:image/gif');

需要注意的是浏览器有一定的容错性,即使对应文件格式不太正确仍然可以正常显示

readfile() 函数:readfile('1.jpg'); 将图片读取到页面中

创建画布

$img = imagecreatetruecolor(500,500);

返回资源类型,参数为图片的宽和高

设置颜色

imagecolorallocate($resource,R,G,B);

  • $blue = imagecolorallocate($img, 0, 0,255); $img 为资源类型即创建画布时的返回值
  • 关于RGB
    • R:red G:green B:blue
    • 函数中的RGB为int类型
    • 当R=255,G=0,B=0时为红色

美术中的三原色,这三种色彩不同比例的混合可以表示所有的颜色。

填充颜色

imagefill($resource,int x,int y,color)

imagefill()image 图像的坐标 xy(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。

imagefill($img,0,0,$green);

绘制图形
绘制线条

imageline($img,0,0,490,490,$blue);

中间四个数字为 x1,y1,x2,y2 中学的坐标表示一条线段,没有毛病

空心矩形

bool imagerectangle( resource $image, int $x1, int $y1, int $x2, int $y2, int $col)

  1. x1,x2,y1,y2 为坐标,以图像的左上方为 0 0,最后一个参数为颜色

  2. imagerectangle($img,100,100,100,100,$blue); 这尼玛是一个点

    imagerectangle($img,100,100,400,400,$blue); 这个没有毛病

实心矩形

bool imagefilledrectangle( resource $image, int $x1, int $y1, int $x2, int $y2, int $color)

与空心矩形类似

绘制圆形

imageellipse($img,250,250,200,200,$blue);

与上面的函数类似,两个250为圆心坐标,后面两个参数依次为宽和高

以上函数返回值皆为布尔值。

输出画布

imagepng($img);

imagejpg($img);

imagegif($img);


4、绘制干扰点和干扰线

干扰点

imagesetpixel($img,mt_rand(1,500),mt_rand(1,500),$green);

for($a=0;$a!=5000;$a++){
    imagesetpixel($img,mt_rand(1,500),mt_rand(1,500),$green);
}
干扰线
for($a=0;$a!=500;$a++){
    imageline(
        $img,
        mt_rand(1,500),
        mt_rand(1,500),
        mt_rand(1,500),
        mt_rand(1,500),
        $green
    );
}

5、输出保存图片和释放内存

// 保存图片
imagegif($img,'imgs.jpg');
// 输出画布
imagegif($img);

imagedestroy($img);

  • 用于图像处理所占用的文件内存一般会比较大,这个函数可以手动释放因图片处理所占用的内存

  • 脚本运行完毕之后会自动清理,但手动清理也是必要的


6、画布中添加文本

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

  • 这个返回值是个什么鬼可以不关心。
  • 函数可以用来写入中文
  • 字体文件需要自行下载 就是参数中的 $fontfile 直接传入字体文件的路径即可
添加英文内容
// 创建画布
$img = imagecreatetruecolor(500,500);

// 分配颜色
$white = imagecolorallocate($img,255,255,255);
$red = imagecolorallocate($img,255,0,0);
$green  = imagecolorallocate($img,0,255,0);
$text = 'a little love';
$font = 'siyuan.otf';

imagettftext($img,18,40,200,200,$red,$font,$text);
for($i=0;$i<strlen($text);$i++){
    imagettftext($img,18,mt_rand(0,45),20*$i+50,200,$red,$font,$text[$i]);
}

imagepng($img);
添加中文内容

添加中文内容需要设置utf-8

中文字符串函数

  • mb_strlen()
  • mb_substr()
for($i=0;$i<mb_strlen($text);$i++){
  imagettftext($img,25,mt_rand(0,90),30*$i+50,200,$red,$font,mb_substr($text,$i,1,'utf-8'));
    
}

7、文本盒子

array imagettfbbox ( float $size , float $angle , string $fontfile , string $text )

  • 文本范围的盒子大小,可以方便控制文本输出位置
  • 返回一个含有 8 个单元的数组表示了文本外框的四个角:
变量位置
0左下角 X 位置
1左下角 Y 位置
2右下角 X 位置
3右下角 Y 位置
4右上角 X 位置
5右上角 Y 位置
6左上角 X 位置
7左上角 Y 位置
// 文本盒子:可以根据字体,角度,字体大小可以计算出字体所占用的空间
$box = imagettfbbox($size,0,$font,$text);
// print_r($box);

// 文本的宽和高
$width = $box[2] - $box[0];
$height = $box[3] - $box[1];
// echo $width,$height;

imagettftext($img,$size,0,490-$width,490-$height,$red,$font,$text);

8、封装验证码类

<?php

// 开发验证码类
class Captcha{
    protected $img;
    protected $width;
    protected $height;
    protected $len;
    protected $code;
    public function __construct($width = 100,$height = 40,$len =5)
    {
        $this->width = $width;
        $this->height = $height;
        $this->len = $len;
    }
    // 生成画布,输出画布
    public function render(){
        // 设置 HTTP 头
        header('Content-Type:image/png');
        $this->img = imagecreatetruecolor($this->width,$this->height);
        $color = imagecolorallocate($this->img,230,230,230);
        $this->code();
        $this->line();
        $this->pix();
        imagefill($this->img,0,0,$color);
        $this->show();
        return $this->code;
    }
    // 展示
    protected function show(){
        imagepng($this->img );

    }
    // 干扰点
    protected function pix(){

        for($i=0;$i!=200;$i++){
            imagesetpixel(
                $this->img,
                mt_rand(0,$this->width),
                mt_rand(0,$this->height),
                $this->color()   
            );
        }
        
    }
    // 干扰线
    protected function line(){
        for($i=0;$i!=6;$i++){
            imageline(
                $this->img,
                mt_rand(0,100),
                mt_rand(0,40),
                mt_rand(0,100),
                mt_rand(0,40),
                $this->color()
            );
        }
    }
    // 随机颜色
    protected function color(){
        $color = imagecolorallocate($this->img,
            mt_rand(0,255),
            mt_rand(0,255),
            mt_rand(0,255)
        );
        return $color;
    }
    // 验证码
    protected function code(){
        $font = realpath('siyuan.otf');
        $text = '1234567890qwertyuiopsdfghjklzxcvbnm';
        for($i=0;$i!=$this->len;$i++){
            $angle = mt_rand(-20,40);
            $this->code .= $pix = $text[mt_rand(0,strlen($text) - 1)];
            $box = imagettfbbox(20,$angle,$font,$pix);
            imagettftext(
                $this->img,
                20,
                $angle,
                $this->width/$this->len*$i + 10,
                ($this->height - $box[3] - $box[5])/2,
                $this->textColor(),
                $font,
                $pix
            );
        }
        return $this->code;
    }
    // 文字颜色
    protected function textColor(){
        return imagecolorallocate(
            $this->img,
            mt_rand(100,255),
            mt_rand(100,255),
            mt_rand(100,255)
        );
    }


}

1、一个函数实现一个小功能,即单一功能原则

2、自定义验证码宽度,高度,位数

3、通过img直接引用再保存在session中,与用户的输入做验证

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值