PHP中绘制图像的一些函数总结_php技巧
在PHP中绘制图像的函数非常丰富,包括点、线、各种几何图形等可以想象出来的平面图形,都可以通过PHP中提供的各种画图函数完成。我们在这里介绍一些常用的图像绘制,如果使用我们没有介绍过的函数,可以参考手册实现。另外,这些图形绘制函数都需要使用画布资源,并在画布中的位置通过坐标(原点是在画布左上角的起始位置,以像素为单位,沿着X轴正方向向右延伸,Y轴正方向向下延伸)决定,并且还可以通过函数的最后一个参数,设置每个图形的颜色。画布中的坐标系统如图所示。
一、函数图形区域填充imageFill()
通过PHP仅仅绘制出只有边线的几何图形是不够的,还可以使用对应的填充函数,完成图形区域的填充。除了每个图形都有对应的填充函数之外,还可以使用imageFill()函数实现区域填充。该函数的语法格式如下:
bool imagefill(resource $image,int $x ,int $y,int $color) //区域填充
该函数在参数$image代表的图像上,相对于图像左上角(0,0)坐标处,从坐标($x,$y)处用参数$color指定的颜色执行区域填充。与坐标($x,$y)点颜色相同且相邻的点都会被填充。例如在下面的示例中,将画布的背景设置为红色。代码如下所示:
$im = imagecreatetruecolor(100, 100); //创建100*100大小的画布
$red = imagecolorallocate($im, 255, 0, 0); //设置一个颜色变量为红色
imagefill($im, 0, 0, $red); //将背景设为红色
header('Content-type:image/png'); //通知浏览器这不是文本而是一个图片
imagepng($im); //生成PNG格式的图片输出给浏览器
imagedestroy($im); //销毁图像资源,释放画布占用的内存空间
?>
二、绘制点和线imageSetPixel()、imageline()
画点和线是绘制图像中最基本的操作,如果灵活使用,可以通过它们绘制出千变万化的图像。在PHP中,使用imageSetPixel()函数在画布中绘制一个单一像素的点,并且可以设置点的颜色。其函数的原型如下所示:
bool imagesetpixel(resource $image,int $x,int $y,int $color) //画一个单一像素
该函数在第一个参数$image中提供的画布上,距离圆点分别为$x和$y的坐标位置,绘制一个颜色为$color的一个像素点。理论上使用画点函数便可以画出所需要的所有图形,也可以使用其他的绘图函数。如果需要绘制一条线段,可以使用imageline()函数,其语法格式如下所示:
bool imageline(resource $image,int $x1,int $y1,int $x2,int $y2,int $color) //画一条线段
我们都知道两点确定一条线段,所以该函数使用$color颜色在图像$image中,从坐标($x1,$y1)开始到($x2,$y2)坐标结束画一条线段。
三、绘制矩形imageRectangle()、imageFilledRectangle()
可以使用imageRectangle()函数绘制矩形,也可以通过imageFilledRectangle()函数绘制一个矩形并填充。这两个函数的语法格式如下所示:
bool imagerectangle(resource $image,int $x1 , int $y1,int $x2,int $y2,int $color) //画一个矩形
bool imagefilledrectangle(resource image,int $x1 ,int $y1 ,int $x2 ,int $y2,int $color) //画一个矩形并填充
这两个函数的行为类似,都是在$image图像中画一个矩形,只不过前者是使用$color参数指定矩形的边线颜色,而后者则是使用这个颜色填充矩形。相对于图像左上角的(0,0)位置,矩形的左上角坐标为($x1,$y1),右下角坐标为($x2,$y2)。
四、绘制多边形imagePolygon()、imagefilledpolygon()
可以使用imagePolygon()函数绘制一个多边形,也可以通过imageFilledPolygon()函数绘制一个多边形并填充。这两个函数的语法格式如下:
bool imagepolygon(resource $image,array $points,int $num_points,int $color) //画一个多边形
bool imagefilledpolygon(resource $image ,array $points,int $num_points,int $color) //画一个多边形并填充
这两个函数的行为类似,都是在$image图像中画一个多边形,只不过前者是使用$color参数指定多边形的边线颜色,而后者则是使用这个颜色填充多边形。第二个参数$points是一个PHP数组,包含了多边形的各个顶点坐标。即points[0]=x0,points[1]=y0,points[2]=x1,points[3]=y1,依此类推。第三个参数$num_points是顶点的总数,必须大于3.
五、绘制椭圆imageEllipse()、imageFilledElipse()
可以使用imageEllipse()函数绘制一个椭圆,也可以通过imageFilledEllipse()函数绘制一个椭圆并填充。这两个函数的语法格式如下:
bool imageellipse(resource $image,int $cx,int $cy,int $w,int $h,int $color) //画一个椭圆
bool imagefilledellipse(resource $image,int $cx,int $cy,int $w,int $h,int $color) //画一个椭圆填充
这两个函数行为类似,都是在$image图像中画一个椭圆,只不过前者是使用$color参数指定椭圆形的边线颜色,而后者则是使用它填充颜色。相对于画布左上角坐标(0,0),以($cx,$cy)坐标为中心画一个椭圆,参数$w和$h分别指定了椭圆的宽和高。如果成功则返回TRUE,失败则返回FALSE。
六、绘制弧线imageArc()
前面介绍的3D扇形统计图示例,就是使用绘制填充圆弧的函数实现的。可以使用imageArc()函数绘制一条弧线,以及圆形和椭圆形。这个函数的语法格式如下:
bool imagearc(resource $image ,int $cx,int $cy,int $w,int $h,int $s,int $e ,int $color) //画椭圆弧
相对于画布左上角坐标(0,0),该函数以($cx,$cy)坐标为中心,在$image所代表的图像中画一个椭圆弧。其中参数$w和$h分别指定了椭圆的宽度和高度,起始点和结束点以$s和$e参数以角度指定。0º位于三点钟位置,以顺时针方向绘画。如果要绘制一个完整的圆形,首先要将参数$w和$h设置为相等的值,然后将起始角度$s设置为0,结束角度$e指定为360.如果需要绘制填充圆弧,可以查询imageFilledArc()函数使用。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
