如何使用 PHP 中的 imageline() 函数绘制一条线?

phpserver side programmingprogramming

imageline() 是 PHP 中的内置函数,用于在两个给定点之间绘制一条线。

语法

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

参数

imageline() 有六个不同的参数:$image、$x1、$y1、$x2、$y2 和$color。

  • $image − 指定要处理的图像资源。

  • $x1 − 指定起始 x 坐标。

  • $y1 − 指定起始 y 坐标。

  • $x2 − 指定结束 x 坐标。

  • $y2 − 指定结束 y 坐标。

  • $color −指定使用 imagecolorallocate() 函数创建的线条颜色和颜色标识符。

返回值

imageline() 在成功时返回 True,在失败时返回 False。

示例 1 − 向图像添加一条线

<?php
   // 使用 imagecreatefrompng() 函数创建图像
   $img = imagecreatefrompng('C:\xampp\htdocs\test\515.png');

   // 分配线条颜色
   $text_color = imagecolorallocate($img, 255, 255, 0);

   // 设置线条的粗细
   imagesetthickness($img, 5);

   // 使用 imageline() 函数添加线条。
   imageline($img, 80, 300, 1140, 300, $text_color);

   // 图像的输出
   header('Content-type: image/png');
   imagepng($img);
   imagedestroy($img);
?>

输出

示例 2

<?php
   // 使用 imagecreate() 函数创建图像
   $img = imagecreate(700, 300);
   
   // 分配颜色
   $grey = imagecolorallocate($img, 122, 122, 122);
   $blue = imagecolorallocate($img, 0, 0, 255);

   // 设置线条的粗细
   imagesetthickness($img, 15);

   // 添加灰色背景颜色
   imageline($img, 0, 0, 550, 400, $grey);

   // 添加蓝线
   imageline($img, 0, 0, 550, 400, $blue);

   // 输出图像
   header('Content-type: image/png');
   imagepng($img);
   imagedestroy($img);
?>

输出


相关文章