您的位置:首页 > 编程语言 > PHP开发

PHP图像处理之画图

2012-04-21 22:24 429 查看
一、PHP图像处理的过程

1、画图,一般画验证码, 统计图。前提条件:安装GD库--- LAMP环境安装

(1)创建画布 --- 创建资源类型 --- 高度 宽度
resource imagecreate ( int x_size, int y_size ); //新建一个基于调色板的图像
resource imagecreatetruecolor ( int x_size, int y_size ) //新建一个真彩色图像,一般用这个

(2)绘制图像
制定各种颜色 $red=imagecolorallocate($img, 255, 0, 0); //为一幅图像分配颜色

矩形, 圆, 点, 线段, 扇形, 画字(字符, 字符串, freetype)
每一个图像对应一个函数

(3)输出图像/保存处理好的图像
1. 输出各种类型(gif, png, jpeg)
imagegif();
imagejpeg();
imagepng();

(4)释放资源
imagedestroy($img); //释放资源

二、PHP处理图像常用到的函数介绍

<?php

//step 1创建图片资源

$img=imagecreatetruecolor(200, 200);

// $img=imagecreate(200, 200);

$red=imagecolorallocate($img, 255, 0, 0); //为一幅图像分配颜色
$yellow=imagecolorallocate($img, 255, 255, 0);
$green=imagecolorallocate($img, 0, 255, 0);
$blue=imagecolorallocate($img, 0, 0, 255);

imagefill($img, 0, 0, $yellow); //为图像区域填充颜色,一幅图的左上角的坐标是:0,0 向左移是整数移,向下移是整数移
//step 2画各种图形

//画一个矩形并填充
imagefilledrectangle($img, 10, 10, 80, 80, $green); //第一个参数为图像资源参数,其左上角坐标为10,10 、右下角坐标为80,80,后面的参数是为图像分配颜色的参数
//画一个矩形
imagerectangle($img, 90, 10, 190, 80, $green);

//线段
imageline($img,0, 0, 200, 200 ,$blue); //线段开始点的坐标是:0,0、结束点的坐标是:200,200
imageline($img,200, 0, 0, 200, $blue);

//点
imagesetpixel($img,50, 50 ,$red);
imagesetpixel($img,55, 50 ,$red);
imagesetpixel($img,59, 50 ,$red);
imagesetpixel($img,64, 50 ,$red);
imagesetpixel($img,72, 50 ,$red);

//圆
// imageellipse ( resource $image , int $cx , int $cy , int $w , int $h , int $color )
//imageellipse() 在 image 所代表的图像中画一个中心为 cx,cy(图像左上角为 0, 0)的椭圆。w 和 h 分别指定了椭圆的宽度和高度,椭圆的颜色由 color 指定。
imageellipse($img, 100, 100, 100, 100,$green);
//圆
imagefilledellipse($img, 100, 100, 10, 10,$blue);
//step3 输出或保存图像

header("Content-Type:image/gif");
imagegif($img);

//释放资源
imagedestroy($img)

?>

三、创建一个 3D 效果的饼状图

<?php

//step 1创建图片资源
$img=imagecreatetruecolor(200, 200);

$white=imagecolorallocate($img, 255, 255, 255);
$gray=imagecolorallocate($img, 0xC0, 0xC0, 0xC0);
$darkgray=imagecolorallocate($img, 0x90, 0x90, 0x90);
$navy=imagecolorallocate($img, 0, 0, 0x80);
$darknavy=imagecolorallocate($img, 0, 0, 0x50);
$red= imagecolorallocate($img, 0xFF, 0, 0);
$darkred=imagecolorallocate($img, 0x90, 0, 0);

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

//3D效果
for($i=60; $i>50; $i--){
//imagefilledarc()画一椭圆弧且填充
//bool imagefilledarc ( resource $image , int $cx , int $cy , int $w , int $h , int $s , int $e , int $color , int $style )
//imagefilledarc() 在 image 所代表的图像中以 cx,cy(图像左上角为 0, 0)画一椭圆弧。成功时返回 TRUE, 或者在失败时返回 FALSE.w 和 h 分别指定了椭圆的宽和高,s 和 e 参数以角度指定了起始和结束点。style 可以是下列值按位或(OR)后的值:
//1.IMG_ARC_PIE 2.IMG_ARC_CHORD 3.IMG_ARC_NOFILL 4.IMG_ARC_EDGED
//IMG_ARC_PIE 和 IMG_ARC_CHORD 是互斥的;IMG_ARC_CHORD 只是用直线连接了起始和结束点,IMG_ARC_PIE 则产生圆形边界(如果两个都用,IMG_ARC_CHORD 生效)。IMG_ARC_NOFILL 指明弧或弦只有轮廓,不填充。IMG_ARC_EDGED 指明用直线将起始和结束点与中心点相连,和 IMG_ARC_NOFILL 一起使用是画饼状图轮廓的好方法(而不用填充)。

imagefilledarc($img, 50, $i,100, 50, -160, 40, $darkgray, IMG_ARC_PIE);
imagefilledarc($img, 50, $i,100, 50, 40, 75, $darknavy, IMG_ARC_PIE);
imagefilledarc($img, 50, $i,100, 50, 75, 200, $darkred, IMG_ARC_PIE);
}
imagefilledarc($img, 50, $i,100, 50, -160, 40, $gray, IMG_ARC_PIE);
imagefilledarc($img, 50, $i,100, 50, 40, 75, $navy, IMG_ARC_PIE);
imagefilledarc($img, 50, $i,100, 50, 75, 200, $red, IMG_ARC_PIE);

header("Content-Type:image/gif");
imagegif($img);

//释放资源
imagedestroy($img);
?>

四、向图像中写入字符、字符串、中文文字

<?php

//step 1创建图片资源
$img=imagecreatetruecolor(200, 200);

$white=imagecolorallocate($img, 255, 255, 255);
$gray=imagecolorallocate($img, 0xC0, 0xC0, 0xC0);
$darkgray=imagecolorallocate($img, 0x90, 0x90, 0x90);
$navy=imagecolorallocate($img, 0, 0, 0x80);
$darknavy=imagecolorallocate($img, 0, 0, 0x50);
$red= imagecolorallocate($img, 0xFF, 0, 0);
$darkred=imagecolorallocate($img, 0x90, 0, 0);

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

imagechar($img, 5, 100, 100, "A", $red); //水平地画一个字符,第二个参数是int $font 是 1,2,3,4 或 5使用的是内置的字体大小,100,100是其坐标(即左上角坐标)
imagechar($img, 5, 120, 120, "B", $red);
imagecharup($img, 5, 60, 60, "C", $red); //垂直地画一个字符(只画一个)
imagecharup($img, 5, 80, 80, "D", $red);

imagestring($img, 3, 10, 10, "Hello", $navy); //水平地画一行字符串,第二个参数表示字体的大小,后面的参数是画上去的图像颜色
imagestringup($img, 3, 10, 80, "Hello", $navy); //垂直地画一行字符串,第二个参数表示字体的大小,后面的参数是画上去的图像颜色
//array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
//float $angle 角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。
imagettftext($img, 25, 60, 150, 150, $red, "simkai.ttf", "高洛峰"); //用 simkai.ttf(TrueType) 字体向图像写入文本,simkai.ttf是中文楷体的字体库,在c:\windows\font\文件夹下有各种字体库
imagettftext($img, 12, -60, 50, 150, $red, "simli.ttf", "中国字");

header("Content-Type:image/gif");
imagegif($img);

//释放资源
imagedestroy($img)
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: