php 画图详解,PHP画图基础

Title: PHP画图基础 Author: MoreWindows Blog: http://blog.csdn.net/MoreWindows KeyWord: PHP绘图 画点、线、弧线 绘制和填充区域 图片特效 彩色圣诞节大雪花图 本篇对PHP常用的绘图函数进行总结。内容有建立图像,为图像分配颜色,画点,画线,画弧线,

Title: PHP画图基础

Author: MoreWindows

Blog: http://blog.csdn.net/MoreWindows

KeyWord: PHP绘图 画点、线、弧线 绘制和填充区域 图片特效 彩色圣诞节大雪花图

本篇对PHP常用的绘图函数进行总结。内容有建立图像,为图像分配颜色,画点,画线,画弧线,绘制和填充区域,输出字符和汉字及一些常见的图片特效如反色和浮雕。此外还给出一些有趣的实例,如绘制彩色的圣诞节大雪花图。

一.新建图像

resource imagecreate( int $x_size , int $y_size )

imagecreate()返回一个图像标识符,代表了一幅大小为 x_size 和y_size 的空白图像。

resource imagecreatetruecolor( int $x_size , int $y_size )

imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和y_size 的黑色图像。PHP手册上推荐尽量使用imagecreatetruecolor()函数。

还有根据.gif、.png、.jpg等文件来创建图像的函数。

resource imagecreatefromgif( string $filename )

resource imagecreatefrompng ( string $filename )

resource imagecreatefromjpeg( string $filename )

二.为图像分配颜色

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

imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。第一次图像调用 imagecolorallocate()表示设置图像背景色。

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

imagecolorallocatealpha() 的行为和imagecolorallocate()相同,但多了一个额外的透明度参数alpha,其值从 0 到127。0表示完全不透明,127 表示完全透明。

三.画点

bool imagesetpixel( resource $image , int $x , int $y , int $color )

注:图像左上角为(0,0)

四.画线

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

从(x1, y1)到(x2,y2)。线的风格可以由bool imagesetstyle( resource $image , array $style )来控制。宽度由bool imagesetthickness ( resource $image , int $thickness )控制,注意这个宽度在画矩形、弧线时也生效。

五.画椭圆弧

bool imagearc(resource $image , int $cx , int $cy , int $w , int $h , int $s , int $e , int $color)

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

$black = imagecolorallocate($img, 0, 0, 0);

imagearc($img, 100, 100, 150, 180, 0, 90,$black);

将在(100,100)处画一段宽150高180的从0到90度的弧,如下图所示(作为参照,右边是全图):

test.jsp?url=http%3A%2F%2Fhi.csdn.net%2Fattachment%2F201202%2F20%2F0_1329704740KUu7.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fmorewindows%2Farticle%2Fdetails%2F7274870

六.绘制区域

矩形

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

椭圆

bool imageellipse ( resource $image , int $cx , int $cy , int $w , int $h , int $color )

多边形

bool imagepolygon ( resource $image , array $points , int $num_points , int $color )

七.填充区域

填充区域

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

imagefill()在image图像的(x,y)处用 color颜色执行区域填充(即与 (x, y) 点颜色相同且相邻的点都会被填充)。如以下代码片段会先画出蓝色的椭圆,然后用红色填充椭圆内部。$blue_color = imagecolorallocate($img, 0, 0, 255);

$red_color = imagecolorallocate($img, 255, 0, 0);

imageellipse($img, 300, 200, 300, 200, $blue_color);

imagefill($img, 300, 200, $red_color);

运行效果如下:

test.jsp?url=http%3A%2F%2Fhi.csdn.net%2Fattachment%2F201202%2F20%2F0_1329704772p99X.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fmorewindows%2Farticle%2Fdetails%2F7274870

画一椭圆并填充

bool imagefilledellipse( resource $image , int $cx , int $cy , int $w , int $h , int $color )

这种画法椭圆是没有边框的,当然也可以如下实现:$lucency_color = imagecolorallocatealpha($img, 0, 0, 0, 126);//127为全透明 0全不透明

$red_color = imagecolorallocate($img, 255, 0, 0);

imageellipse($img, 300, 200, 300, 200, $lucency_color);

imagefill($img, 300, 200, $red_color);

//imagefilledellipse($img, 300, 200, 300, 200, $red_color);

画一矩形并填充

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

类似于画一椭圆并填充。

画一椭圆弧且填充

bool imagefilledarc( resource $image , int $cx , int $cy , int $w , int $h , int $s , int $e , int $color , int $style )

对最后一个参数说明下,有4种值:

IMG_ARC_PIE产生圆形边界(如果两个都用,IMG_ARC_CHORD生效)。

IMG_ARC_CHORD用直线连接了起始和结束点。

IMG_ARC_NOFILL画弧,只有轮廓,不填充。

IMG_ARC_EDGED指明用直线将起始和结束点与中心点相连

看下实际的效果(圆弧角度从0到210度):

test.jsp?url=http%3A%2F%2Fhi.csdn.net%2Fattachment%2F201202%2F20%2F0_1329704796A9P9.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fmorewindows%2Farticle%2Fdetails%2F7274870

将用这个函数来画饼状图。

八.字符

水平地画一个字符

bool imagechar(resource $image , int $font , int $x , int $y , string $c , int $color)

垂直地画一个字符

bool imagecharup(resource $image , int $font , int $x , int $y , string $c , int $color)

水平地画一行字符串

bool imagestring(resource $image , int $font , int $x , int $y , string $s , int $col)

垂直地画一行字符串

bool imagestringup(resource $image , int $font , int $x , int $y , string $s , int $col)

$font参数要注意下,要么使用内置的字体(从1到5),要么用int imageloadfont ( string $file )加载字体后再设置。

可以用输出*来得到彩色的圣诞节雪花图,代码如下:

运行效果如下:

test.jsp?url=http%3A%2F%2Fhi.csdn.net%2Fattachment%2F201202%2F20%2F0_1329704818JJjj.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fmorewindows%2Farticle%2Fdetails%2F7274870

九.文本

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

解释几个参数:

第二参数$size为字体大小。

第三参数$angle为文本旋转角度,0度为从左向右读的文本,更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。

第七个参数$fontfile表示字体文件,如"c:\\WINDOWS\\Fonts\\simhei.ttf"。

注意!使用这个函数应当配合imagecreatetruecolor(),而不是imagecreate()。下面用这个imagettftext()来代替上面的imagechar从而生成彩色的圣诞节大雪花图,代码如下:

运行效果如下:

test.jsp?url=http%3A%2F%2Fhi.csdn.net%2Fattachment%2F201202%2F20%2F0_13297048332XwQ.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fmorewindows%2Farticle%2Fdetails%2F7274870

十.图像特效

bool imagefilter ( resource $src_im , int $filtertype [,int $arg1 [, int $arg2 [, int $arg3 ]]] )

这里提供了很多特效,如浮雕,反色(底片色),调节灰度、亮度,对比度,模糊化等等。这只展示几种常用的特效,更多的请访问http://www.php.net/manual/zh/function.imagefilter.php。

原图:

test.jsp?url=http%3A%2F%2Fhi.csdn.net%2Fattachment%2F201202%2F20%2F0_13297048618O8D.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fmorewindows%2Farticle%2Fdetails%2F7274870

将图处保存到D:\\1234.png,就可以执行下面的代码了。

IMG_FILTER_NEGATE:将图像中所有颜色反转(底片色)。

test.jsp?url=http%3A%2F%2Fhi.csdn.net%2Fattachment%2F201202%2F20%2F0_13297048820jeS.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fmorewindows%2Farticle%2Fdetails%2F7274870

代码:

IMG_FILTER_EMBOSS:使图像浮雕化。

test.jsp?url=http%3A%2F%2Fhi.csdn.net%2Fattachment%2F201202%2F20%2F0_1329704897bz9O.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fmorewindows%2Farticle%2Fdetails%2F7274870

代码:

本篇就介绍到此,《PHP 画图应用 验证码 柱状图》将用本篇介绍的函数来绘制验证码和柱状图。

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Matlab中的animatedline函数可以用于实时绘制动态图像。在使用多个animatedline时,需要分别创建多个animatedline对象,并对每个对象进行单独的设置和更新。 以下是一个简单的示例代码,展示了如何使用两个animatedline对象同时绘制两条曲线: ```matlab % 创建两个animatedline对象 h1 = animatedline('Color','r'); h2 = animatedline('Color','b'); % 设置坐标轴范围 axis([0,10,-1,1]); % 开始绘制 for x = 0:0.1:10 % 计算两条曲线的y值 y1 = sin(x); y2 = cos(x); % 更新animatedline对象 addpoints(h1,x,y1); addpoints(h2,x,y2); % 刷新图像 drawnow; end ``` 在上面的代码中,我们首先创建了两个animatedline对象h1和h2,并设置它们的颜色为红色和蓝色。然后,我们设置了坐标轴的范围,并开始循环绘制两条曲线。在每次循环中,我们通过计算得到两条曲线的y值,并使用addpoints函数更新animatedline对象。最后,我们使用drawnow函数刷新图像,使得绘制的曲线在图像中显示出来。 除了颜色之外,还可以对animatedline对象进行其他设置,例如线型、线宽、标记等。可以使用set函数对animatedline对象进行设置,例如: ```matlab set(h1,'LineStyle','-','LineWidth',2,'Marker','o'); ``` 这会将h1对象的线型设置为实线,线宽设置为2,标记设置为圆圈。 总之,使用multiple animatedline绘制动态图像需要分别创建多个animatedline对象,并对每个对象进行单独的设置和更新。可以通过set函数对animatedline对象进行颜色、线型、线宽、标记等设置,使用drawnow函数刷新图像,实现实时绘制动态图像的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值