Home php教程 php手册 PHP中绘制图像的一些函数总结

PHP中绘制图像的一些函数总结

Jun 06, 2016 pm 08:17 PM
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()函数绘制一条弧线,以及圆形和椭圆形。这个函数的语法格式如下:

复制代码 代码如下:

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles