PHP中GD库

5934 admin
世界杯历届冠军名单

PHP中GD库

一、GD库的介绍

1、GD库是什么?

Graphic Device,图像工具库,gd库是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片。 在网站上 GD库通常用来生成缩略图或者用来对图片加水印或者对网站数据生成报表。

PHP 不仅限于只产生 HTML 的输出,还可以创建及操作多种不同格式的图像文件。PHP提供了一些内置的图像信息函数,也可以使用GD函数库创建新图像或处理已有的图像。目前GD2库支持GIF、JPEG、PNG和WBMP等格式。此外还支持一些FreeType(加载操作系统中的字体)、Type1等字体库。

2、引入和检测扩展

a. 在windows下,php.ini里,去掉php_gd2.dll前的;,引入gd2扩展

b. 在Linux下,需要编译时加上gd支持:extension = php_gd2.so

# 检测扩展是否开启

# phpinfo(); -》gd

var_dump(extension_loaded('gd'));

echo '


';

var_dump(gd_info());

echo '


';

var_dump(function_exists('imagecreatetruecolor'));

二、图片处理典型流程

imagetypes()获取当前PHP所支持图像类型

1、创建画布

//创建图片资源

$image = imagecreatetruecolor(500, 500);

2、准备颜料

red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF

#$color = imagecolorallocate($image, $red, $green, $blue );

$background_color = imagecolorallocate($image, 255, 255, 255); //白色作为背景色

$back = imagecolorallocate($image, 255, 255, 255);

$border = imagecolorallocate($image, 0, 0, 0);

$red = imagecolorallocate($image, 255, 0, 0);

$green = imagecolorallocate($image, 0, 255, 0);

3、利用颜料在画布上写字或填充颜色

#imagefilledrectangle ( resource $图片资源 , int $点1x轴, int $点1y轴 , int $点2x轴 , int $点2y轴 , int $color )

- x坐标和y坐标组成一个点

- 另个点可以整成一个直线

- 直线如果不是水平或垂直的就可以组成一个矩形

imagefilledrectangle ( $image , 0, 0 , 500 , 500 , $background_color );

如果要填充整个画布的话: 点1 为x轴为画布的0位置,点1的y轴也为画布的0位置。 点2 为x轴为画布的500位置,点2的y轴也为画布的500位置。

4、画图像

a、圆形(椭圆)

imagefilledellipse($img, 250, 250, 200, 200, $yellow);

//椭圆

imagefilledellipse($image, 200, 150, 300, 200, $yellow);

b、画矩形

imagefilledrectangle($img, 200, 200, 300, 300, $blue);

5、保存图片

#仅显示出来看

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

// imagejpeg($image);

imagegif($image,'./hello.gif'); //保存起来

6、销毁图片资源

imagedestroy($image);

三、GD常用的函数介绍

这里面的函数会用就好,不用都背

1、新建一个真彩色图像

resource imagecreatetruecolor ( int `$width` , int `$height` )

2、为一幅图像分配颜色.失败返回-1

int imagecolorallocate ( resource `$image` , int `$red` , int `$green` , int `$blue` )

3、为一幅图像分配颜色和透明度

透明度参数 alpha,其值从 0 到 127。0 表示完全不透明,127 表示-完全透明。

如果分配失败则返回 FALSE。

int imagecolorallocatealpha ( resource `$image` , int `$red` , int `$green` , int `$blue` , int `$alpha` )

4、画一条线

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

imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段

5、写字

imagechar($image, 5, 50, 50, 'J', $red);//水平地画一个字符

imagecharup($image, 5, 100, 100, 'A', $green); //垂直地画一个字符

imagestring($image, 5, 200, 200, 'Jack', $blue); // 水平地画一行字符串

// 中文用 TrueType 字体向图像写入文本

imagettftext($image, 18, 0, 20, 20, $red, './font/simkai.ttf', '来啊,流浪啊,反正有...');

6、画圆弧

imagearc() 以 cx,cy(图像左上角为 0, 0)为中心在 image 所代表的图像中画一个椭圆弧。w和 h 分别指定了椭圆的宽度和高度,起始和结束点以 s 和 e 参数以角度指定。0°位于三点钟位置,以顺时针方向绘画。

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

// 画一个白色的圆

imagearc($img, 100, 100, 150, 150, 0, 360, $white);

//蓝色圆弧

imagearc($img, 150, 150, 150, 150, 45, 135, $blue;

7、画椭圆

在指定的坐标上画一个椭圆。

bool imageellipse ( resource `$image` , int `$cx` , int `$cy` , int `$width` , int `$height` , int `$color` )

8、画矩形

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

magerectangle() 用 col 颜色在 image 图像中画一个矩形,其左上角坐标为 x1, y1,右下角坐标为 x2, y2。图像的左上角坐标为 0, 0。

代码示例参考-(划线)

$width = $height = 300;

$image = imagecreatetruecolor($width, $height);

# $image = imagecreatefromjpeg('./ren.jpg');

$back = imagecolorallocate($image, 255, 255, 255);

$border = imagecolorallocate($image, 0, 0, 0);

$red = imagecolorallocate($image, 255, 0, 0);

$green = imagecolorallocate($image, 0, 255, 0);

imageline($image, 0, 0, 200, 200, $red);

imageline($image, 20, 30, 220, 280, $green);

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

imagejpeg($image);

imagedestroy($image);

代码示例(三圈交叠)

$size = 300;

$image=imagecreatetruecolor($size, $size);

// 用白色背景加黑色边框画个方框

$back = imagecolorallocate($image, 255, 255, 255);

$border = imagecolorallocate($image, 0, 0, 0);

imagefilledrectangle($image, 0, 0, $size - 1, $size - 1, $back);

imagerectangle($image, 0, 0, $size - 1, $size - 1, $border);

$yellow_x = 100;

$yellow_y = 75;

$red_x = 120;

$red_y = 165;

$blue_x = 187;

$blue_y = 125;

$radius = 150;

// 用 alpha 值分配一些颜色

$yellow = imagecolorallocatealpha($image, 255, 255, 0, 75);

$red = imagecolorallocatealpha($image, 255, 0, 0, 75);

$blue = imagecolorallocatealpha($image, 0, 0, 255, 75);

// 画三个交迭的圆

imagefilledellipse($image, $yellow_x, $yellow_y, $radius, $radius, $yellow);

imagefilledellipse($image, $red_x, $red_y, $radius, $radius, $red);

imagefilledellipse($image, $blue_x, $blue_y, $radius, $radius, $blue);

// 不要忘记输出正确的 header!

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

// 最后输出结果

imagepng($image);

imagedestroy($image);来自为知笔记(Wiz)

最好的CAD看图软件有哪些?这三款软件值得一试! 解读手机飞线:是做工简陋还是偷换概念