首页 > 技术文章 > gd库

fujunjie 2018-09-10 15:19 原文

//检查GD模块文件是否存在

//D:\wamp\bin\php\php7.0.4\ext

//检查GD模块是否开启

phpinfo();

 

//创建画布
//准备颜色
//填充背景
//作画
//保存,输出
//关闭资源/销毁(施放内存)

 

//1.创建画布
imagecreatetruecolor(x,y)

//2.准备颜色

imagecolorallocate($img, 255, 255, 255)

$img 返回资源  rgb颜色

//3.填充背景

imagefill($img, x,y, color)

$img 返回资源  x坐标 y坐标 color要填充的颜色

//4.画图

imagesetpixel($img, x,y, color) 点

imageline($img, x1,y1, x2,y2, color) 线

//写字
imagettftext($img, font-size, 角度, x1,y1, color, '.ttf', '写啥')

5.输出图片
imagejpeg()
imagepng()
imagegif()

6.销毁图片资源
imagedestroy()

 

 

//画点

<?php
header("content-type:text/html;charset=utf-8");

//1.创建画布
$img = imagecreatetruecolor(500,500);
//参1,2 画布宽和高
// var_dump($img);
//成功后返回图象资源,失败后返回 FALSE
//2.准备颜色
//imagecolorallocate(image, red, green, blue)
//参1 资源
//参2,3,4 RGB : 0-255 0x00~0xff
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0,0,0);
$red = imagecolorallocate($img, 255, 0,0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 0,255);
$yellow = imagecolorallocate($img, 255,255,0);

//3.填充背景
// imagefill($img, x,y, $color)
imagefill($img, 0,0, $black);
//4.作画
//画点
// imagesetpixel();
//参1 资源
//参2,3 为点的坐标
//参4 点的颜色
imagesetpixel($img, 250,250, $white);
for ($i=0; $i < 1000; $i++) {
imagesetpixel($img, mt_rand(0,500),mt_rand(0,500), $white);
}
//5.保存,输出
header("content-type:image/jpeg");
imagejpeg($img);
//imagejpeg($img) imagegif($img) imagepng($img)
//6.关闭资源/销毁(施放内存)
imagedestroy($img);

 

 

 

//划线

<?php
header("content-type:text/html;charset=utf-8");

//1.创建画布
$img = imagecreatetruecolor(500,500);
//参1,2 画布宽和高

$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0,0,0);
$red = imagecolorallocate($img, 255, 0,0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 0,255);
$yellow = imagecolorallocate($img, 255,255,0);

//3.填充背景
// imagefill($img, x,y, $color)
imagefill($img, 0,0, $black);
//4.作画
//画线
// imageline($img, x1,y1, x2,y2, $color)
// 参1 资源
// 参2,3 起始点的坐标
// 参4,5 结束点的坐标
// 参6 颜色
imageline($img, 500,0, 0,500, $red);
for ($i=0; $i < 200; $i+=10) {
// imageline($img, 100,100+$i, 300,100+$i, $red);
imageline($img,0+$i,0-$i, 500-$i,500+$i, $yellow);
}
//5.保存,输出
header("content-type:image/jpeg");
imagejpeg($img);
//imagejpeg($img) imagegif($img) imagepng($img)
//6.关闭资源/销毁(施放内存)
imagedestroy($img);

 

//封装验证码

code(100,40,4,2);
function code($width=100,$height=40,$type=4,$sty=2)
{
//准备画布
$img = imagecreatetruecolor($width, $height);
//准备颜色
$red = imagecolorallocate($img,255,0,0);
$green = imagecolorallocate($img,mt_rand(100,180),mt_rand(100,180),mt_rand(100,180));
$blue = imagecolorallocate($img,0,0,255);
$black = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
$whate = imagecolorallocate($img,255,255,255);
$gray = imagecolorallocate($img,200,200,200);
//填充背景
imagefill($img,0,0,$gray);

//干扰点 和干扰线
for($i = 0;$i < $width;$i++){
imagesetpixel($img, mt_rand(0,$width-1), mt_rand(0,$height-1), $green);
}

for($i = 0;$i < $width/10;$i++){
imageline($img,mt_rand(0,$width-1), mt_rand(0,$height-1), mt_rand(0,$width-1), mt_rand(0,$height-1), $green);
}

//准备源字符
$str = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';

switch ($sty) {
case '0':
$str = substr($str,0,10);
break;
case '1':
$str = substr($str,10,26);
break;
}
// echo strlen($str);
$str = str_shuffle($str);
$list = substr($str,0,$type);
// echo $list;

//写字
// imagettftext(image, size, angle, x, y, color, fontfile, text)
for ($i=0; $i < $type; $i++) {
$x = $width/10 + ($width/$type)*$i;
imagettftext($img,mt_rand(14,19),mt_rand(-10,10),$x,mt_rand($height/2,$height/1.1),$black,'./font/'.mt_rand(1,5).'.ttf',$list[$i]);
}


header('content-type:image/jpeg');

imagejpeg($img);

imagedestroy($img);
}

推荐阅读