2024 年 PHP Conference Japan

imagedashedline

(PHP 4, PHP 5, PHP 7, PHP 8)

imagedashedline繪製虛線

說明

imagedashedline(
    GdImage $image,
    int $x1,
    int $y1,
    int $x2,
    int $y2,
    int $color
): bool

此函數已棄用。請改用 imagesetstyle()imageline() 的組合。

參數

image

一個 GdImage 物件,由圖像創建函數之一返回,例如 imagecreatetruecolor()

x1

左上角 x 座標。

y1

左上角 y 座標,0, 0 是圖像的左上角。

x2

右下角 x 座標。

y2

右下角 y 座標。

color

填充顏色。使用 imagecolorallocate() 創建的顏色識別碼。

返回值

成功時返回 true,失敗時返回 false

更新日誌

版本 說明
8.0.0 image 現在需要一個 GdImage 實例;以前需要一個有效的 gd 資源

範例

範例 #1 imagedashedline() 範例

<?php
// 建立一個 100x100 的圖像
$im = imagecreatetruecolor(100, 100);
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);

// 繪製一條垂直虛線
imagedashedline($im, 50, 25, 50, 75, $white);

// 儲存圖像
imagepng($im, './dashedline.png');
imagedestroy($im);
?>

上述範例將輸出類似以下的內容

Output of example : imagedashedline()

範例 #2 imagedashedline() 的替代方案

<?php
// 建立一個 100x100 的影像
$im = imagecreatetruecolor(100, 100);
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);

// 定義我們的樣式:前 4 個像素是白色,
// 接下來的 4 個是透明的。這將產生虛線效果
$style = Array(
$white,
$white,
$white,
$white,
IMG_COLOR_TRANSPARENT,
IMG_COLOR_TRANSPARENT,
IMG_COLOR_TRANSPARENT,
IMG_COLOR_TRANSPARENT
);

imagesetstyle($im, $style);

// 繪製虛線
imageline($im, 50, 25, 50, 75, IMG_COLOR_STYLED);

// 儲存影像
imagepng($im, './imageline.png');
imagedestroy($im);
?>

另請參閱

新增註解

使用者貢獻的註解 5 則註解

1
tgabor72 at freemail dot hu
14 年前
您可以使用具有額外功能的 imagepatternedline() 函數來代替 imagedashedline(),以便在影像上顯示虛線和其他任何種類的圖案線條。此例程還會管理線條的粗細。玩得開心!

<?php
// imagepatternedline() function
// Routine was developed to draw any kind of straight line with thickness. Routine uses imageline() function to draw line.
// Parameters are (similar to imageline() function):
// $image: (resource) imagefile
// $xstart, $ystart: (int) x,y coordinates for first point
// $xend, $yend: (int) x,y coordinates for last point
// $color: (int) color identifier that created with imagecolorallocate()
// extra parameters:
// $thickness: (int) thickness of line in pixel
// $pattern: (string) pattern of line, which repeats continuously while line is being drawed.
// If there is '1' in the pattern that means the actual dot of line is visible,
// '0' means dot is not visible (space between two line elements).
// All characters regard for one pixel. Default: 1 dot wide dashed line with 4-4 dots and spaces.
// Examples for pattern:
// "1" or "" continuous line
// "10" close dotline
// "10000" dotline
// "111111110000001100000011111111" dotline for design drawing
// "111111111100000011000000110000001111111111" double dotline
// some examples for using imagepatternedline():
// imagepatternedline($image,300,300,442,442,$color,200,""); // a square with 200 length of edge and rotated 45 degrees
// imagepatternedline($image,100,200,289,200,$color,100,
// "11001100000011001111000011001111110000001100001100"
// ."00001111001100111100000011000000110000110011001100"
// ."11000011111100111111000011001111001111000011110000"
// ."1111001111110011000011000000001100110011"); // barcode

function imagepatternedline($image, $xstart, $ystart, $xend, $yend, $color, $thickness=1, $pattern="11000011") {
$pattern=(!strlen($pattern)) ? "1" : $pattern;
$x=$xend-$xstart;
$y=$yend-$ystart;
$length=floor(sqrt(pow(($x),2)+pow(($y),2)));
$fullpattern=$pattern;
while (
strlen($fullpattern)<$length) $fullpattern.=$pattern;
if (!
$length) {
if (
$fullpattern[0]) imagefilledellipse($image, $xstart, $ystart, $thickness, $thickness, $color);
return;
}
$x1=$xstart;
$y1=$ystart;
$x2=$x1;
$y2=$y1;
$mx=$x/$length;
$my=$y/$length;
$line="";
for(
$i=0;$i<$length;$i++){
if (
strlen($line)==0 or $fullpattern[$i]==$line[0]) {
$line.=$fullpattern[$i];
}else{
$x2+=strlen($line)*$mx;
$y2+=strlen($line)*$my;
if (
$line[0]) imageline($image, round($x1), round($y1), round($x2-$mx), round($y2-$my), $color);
$k=1;
for(
$j=0;$j<$thickness-1;$j++) {
$k1=-(($k-0.5)*$my)*(floor($j*0.5)+1)*2;
$k2= (($k-0.5)*$mx)*(floor($j*0.5)+1)*2;
$k=1-$k;
if (
$line[0]) {
imageline($image, round($x1)+$k1, round($y1)+$k2, round($x2-$mx)+$k1, round($y2-$my)+$k2, $color);
if (
$y) imageline($image, round($x1)+$k1+1, round($y1)+$k2, round($x2-$mx)+$k1+1, round($y2-$my)+$k2, $color);
if (
$x) imageline($image, round($x1)+$k1, round($y1)+$k2+1, round($x2-$mx)+$k1, round($y2-$my)+$k2+1, $color);
}
}
$x1=$x2;
$y1=$y2;
$line=$fullpattern[$i];
}
}
$x2+=strlen($line)*$mx;
$y2+=strlen($line)*$my;
if (
$line[0]) imageline($image, round($x1), round($y1), round($xend), round($yend), $color);
$k=1;
for(
$j=0;$j<$thickness-1;$j++) {
$k1=-(($k-0.5)*$my)*(floor($j*0.5)+1)*2;
$k2= (($k-0.5)*$mx)*(floor($j*0.5)+1)*2;
$k=1-$k;
if (
$line[0]) {
imageline($image, round($x1)+$k1, round($y1)+$k2, round($xend)+$k1, round($yend)+$k2, $color);
if (
$y) imageline($image, round($x1)+$k1+1, round($y1)+$k2, round($xend)+$k1+1, round($yend)+$k2, $color);
if (
$x) imageline($image, round($x1)+$k1, round($y1)+$k2+1, round($xend)+$k1, round($yend)+$k2+1, $color);
}
}
}
?>
1
ProfessorNeo at gmx dot de
18 年前
'michi at marel dot at' 回報的錯誤在 PHP 5.1.1 版中也存在。這個函數只適用於垂直線!
0
ihsanduvac at gmail dot com
12 年前
它不適用於水平線。請使用 imagesetstyle()
0
alien-scripts.de
19 年前
我自己畫虛線

<?
for($l=50;$l<=550;$l+=5)
{
if($da == 0) { $da = 1; }
elseif($da == 1){
imageline($bild,$l,50,$l+5,50,$green);
$da = 0; }
}
?>

$l 是 x 值
這樣我們就有一條虛線 :)
-1
michi at marel dot at
21 年前
這個函數在 PHP 4.0.4 之前有一個錯誤。您只能繪製垂直虛線。要繪製其他虛線,您可以將 <ImageSetStyle> 設定為特殊的虛線樣式,然後使用 <ImageLine> 繪製它。

範例程式碼
<?php
function MDashedLine($image, $x0, $y0, $x1, $y1, $fg, $bg)
{
$st = array($fg, $fg, $fg, $fg, $bg, $bg, $bg, $bg);
ImageSetStyle($image, $st);
ImageLine($image, $x0, $y0, $x1, $y1, IMG_COLOR_STYLED);
}
?>
To Top