Home php教程 php手册 说说PHP作图(二)

说说PHP作图(二)

Jun 21, 2016 am 09:08 AM
nbsp

    上次说了一种简单的回避GD的作图方法,而后又用GD作了最简单的一幅“图”——直线。
这次我就接着画直线向下说。上次代码中详细解释过的部分,这次不再赘述。


  Header("Content-type: image/png");
  $im = ImageCreate (200, 100);
  $col_black = ImageColorAllocate($im, 0,0,0);
  $col_orn = ImageColorAllocate($im, 255,192,0);
  // 今天用橘色吧。
  // 跟 imageline 函数完全相同的用法,
  ImageDashedLine($im,0,100,199,100,$col_orn);
  // 这样就画了一条虚线。
  
  // 下面我们来做个试验。用以说明一个问题。
  $col_yel = ImageColorAllocate($im, 255,255,0);
  // 黄色。
  ImageLine($im,0,99,199,99,$col_yel);
  // 在图象的最下沿画了一条黄色的线。
  ImageLine($im,200,0,200,100,$col_orn);
  // 试图在图象最右沿画一条澄色的线,结果什么也没有。
  // 这表明,宽200,高100的图象,其坐标的范围是(0,0)到(199,99)。
  
  ImagePNG($im);
  ImageDestroy($im);
  // 这一段先结束吧。
?>


    接下来这个效果就爽了!我也是现学现卖。PHP4.0.6以上增加了这个用法——可以用交替的
颜色画线!示例如下:


  Header("Content-type: image/png");
  $im = ImageCreate (200, 100);
  $col_black = ImageColorAllocate($im, 0,0,0);
  $col_orn = ImageColorAllocate($im, 255,192,0);
  $col_red = ImageColorAllocate($im, 255,0,0);

  $style=array($col_red,$col_red,$col_black,$col_orn,$col_orn,$col_orn,$col_black);
  ImageSetStyle($im, $style);
  ImageLine($im, 0, 50, 199, 50, IMG_COLOR_STYLED);

  ImagePNG($im);
  ImageDestroy($im);
?>

看看效果吧。

    其中我用空行分割开的那三行,说明一下。定义了一个数组 $style,它的成员是一系列的颜色;
然后执行了一个函数,而后用 IMG_COLOR_STYLED “颜色”画出来的是这么神奇的“直线”——
红色、黑色、橙色交替的效果。仔细看一下你就会发现,红、黑、橙交替的顺序,就是我们定义的
$style数组成员的序列:红、红、黑、橙、橙、橙、黑,然后周而复始……
    看明白了吗?注意,这个函数在PHP4.0.6以后才支持。



    有了我详细讲解的画线的基础,我想把画其他几何图形的函数一笔代过。需要提示大家的是,无论
画哪种几何图形,无非是抓住这种图形的几个要素。先不算颜色,各种图形的要素如下:

点,两个要素:横坐标、纵坐标

矩形,四个要素:左上角、右下角的横、纵坐标

弧,这样理解:弧可以包括圆弧、椭圆弧;画圆弧画他360度就可以成一个圆,画椭圆弧画他360度也就画
              成一个椭圆;所以这个弧的要素有六:中心点横、纵坐标,横轴长、纵轴长、弧的始、终点。

看下面这段例子。


  Header("Content-type: image/png");
  $im = ImageCreate (200, 100);
  $col_blk = ImageColorAllocate($im, 0,0,0);
  $col_orn = ImageColorAllocate($im, 255,192,0);
  $col_red = ImageColorAllocate($im, 255,0,0);
  $col_grn = ImageColorAllocate($im, 0,255,0);
  $col_blu = ImageColorAllocate($im, 0,0,255);
  
  ImageSetPixel($im,20,10,$col_orn);
  // 小小一个点,不知道能否看得见?
  ImageRectangle($im,25,20,95,55,$col_blu);
  // 蓝色的矩形。
  ImageArc($im,20,85,50,40,225,360,$col_grn);
  // 绿色的椭圆弧,中心在(20,85),横轴50,纵轴40,225度至360度。
  // 由此可见,这里的圆弧始、终点是以角度计量,
  // 是以水平向右的方向为0度,顺时针计算的。
  ImageArc($im,160,60,40,40,0,360,$col_orn);
  // 橙色的整圆。只要横轴长与纵轴长相等,就是正圆。
  // 上高中我们就学过:圆是椭圆的特例嘛!
  // 最后再画一段圆弧。圆心能否在图象以外?
  ImageArc($im,160,140,240,240,0,360,$col_red);
  // 可以!
  
  ImagePNG($im);
  ImageDestroy($im);
?>

    作图当然免不了要把某一区域涂成某种颜色。GD有三种着色方式,一种是矩形区域着色,
一种是指定的点所处的封闭区域着色,另一种是指定的颜色所包围的区域着色。看以下的例子:

  Header("Content-type: image/png");
  $im = ImageCreate (200, 100);
  $col_blk = ImageColorAllocate($im, 0,0,0);
  $col_orn = ImageColorAllocate($im, 255,192,0);
  $col_yel = ImageColorAllocate($im, 255,255,0);
  $col_red = ImageColorAllocate($im, 255,0,0);
  $col_grn = ImageColorAllocate($im, 0,255,0);
  $col_blu = ImageColorAllocate($im, 0,0,255);
  
  ImageFilledRectangle($im,20,10,100,50,$col_blu);
  ImageFilledRectangle($im,5,40,50,90,$col_red);
  ImageFilledRectangle($im,40,80,100,95,$col_orn);
  ImageFilledRectangle($im,90,35,110,90,$col_yel);
  // 以上是第一种着色。直接绘制矩形。
  // 我故意用四个不同颜色的矩形围起一小块区域,
  // 用以说明第二种着色。
  
  ImagePNG($im);
  ImageDestroy($im);
  
  // 看一下效果。
  
?>

接着:


  Header("Content-type: image/png");
  $im = ImageCreate (200, 100);
  $col_blk = ImageColorAllocate($im, 0,0,0);
  $col_orn = ImageColorAllocate($im, 255,192,0);
  $col_yel = ImageColorAllocate($im, 255,255,0);
  $col_red = ImageColorAllocate($im, 255,0,0);
  $col_grn = ImageColorAllocate($im, 0,255,0);
  $col_blu = ImageColorAllocate($im, 0,0,255);
  
  ImageFilledRectangle($im,20,10,100,50,$col_blu);
  ImageFilledRectangle($im,5,40,50,90,$col_red);
  ImageFilledRectangle($im,40,80,100,95,$col_orn);
  ImageFilledRectangle($im,90,35,110,90,$col_yel);
  // 以上是第一种着色。直接绘制矩形。
  // 我故意用四个不同颜色的矩形围起一小块区域,
  // 用以说明第二种着色。
  
  ImageFill($im,70,70,$col_grn);
  // 这是第二种着色。
  
  ImageRectangle($im,120,40,190,90,$col_grn);
  // 暂且画一个矩形来做框吧。事实上任何样子的边界都可以做框。
  ImageFilltoBorder($im,130,50,$col_grn,$col_orn);
  // 把绿色矩形框内涂成橙色。
  // 只要指定的点位于这个“框”的范围内即可,与该点在区域内的位置无关。
  // 这个函数其实是这样工作的:
  // 从指定的点开始,向外,寻找指定颜色的边界,如果找到,则停止,
  // 找不到,就把途经的点涂成需要的颜色。
  
  ImagePNG($im);
  ImageDestroy($im);
  
  // 看一下效果。
  // 现在我们作出的图已经是花花绿绿了,可是在浏览器里,图片上,
  // 右键->属性:只有 214 个字节!
  
?>

    这一次说到这里吧先。
    
    



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

Video Face Swap

Video Face Swap

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

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)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to Fix Activation Error Code 0xc004f069 in Windows Server How to Fix Activation Error Code 0xc004f069 in Windows Server Jul 22, 2023 am 09:49 AM

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using

See all articles