如何用 PHP 中的 imageline() 函式繪製一條線?


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
   // Create an image using imagecreatefrompng() function
   $img = imagecreatefrompng('C:\xampp\htdocs\test\515.png');

   // allocated the line color
   $text_color = imagecolorallocate($img, 255, 255, 0);

   // Set the thickness of the line
   imagesetthickness($img, 5);

   // Add a line using imageline() function.
   imageline($img, 80, 300, 1140, 300, $text_color);

   // Output of the image
   header('Content-type: image/png');
   imagepng($img);
   imagedestroy($img);
?>

輸出

示例 2

<?php
   // Create an image using imagecreate() function
   $img = imagecreate(700, 300);
   
   // Allocate the colors
   $grey = imagecolorallocate($img, 122, 122, 122);
   $blue = imagecolorallocate($img, 0, 0, 255);

   // Set the thickness of the line
   imagesetthickness($img, 15);

   // Add a grey background color
   imageline($img, 0, 0, 550, 400, $grey);

   // Add a blue line
   imageline($img, 0, 0, 550, 400, $blue);

   // Output the image
   header('Content-type: image/png');
   imagepng($img);
   imagedestroy($img);
?>

輸出

更新於: 09-Aug-2021

2K+ 瀏覽量

開啟你的 事業

完成課程即可獲得認證

開始
廣告