小心!如果您將空陣列作為第二個參數傳遞給 imagesetstyle(),它將會導致您的伺服器崩潰!
我剛才在使用它時不小心這樣做了,頁面花了一分多鐘的時間處理,然後我的 Apache 伺服器出現了 Windows 的「傳送錯誤報告」視窗。
(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)
imagesetstyle — 設定線條繪製樣式
imagesetstyle() 設定所有線條繪製函式(例如 imageline() 和 imagepolygon())在使用特殊顏色 IMG_COLOR_STYLED
或以顏色 IMG_COLOR_STYLEDBRUSHED
繪製圖像線條時所使用的樣式。
image
一個 GdImage 物件,由圖像創建函式之一返回,例如 imagecreatetruecolor()。
style
像素顏色陣列。您可以使用 IMG_COLOR_TRANSPARENT
常數來添加透明像素。請注意,style
不能是空的 陣列。
以下範例腳本繪製一條從畫布左上角到右下角的虛線
範例 #1 imagesetstyle() 範例
<?php
header("Content-type: image/jpeg");
$im = imagecreatetruecolor(100, 100);
$w = imagecolorallocate($im, 255, 255, 255);
$red = imagecolorallocate($im, 255, 0, 0);
/* Draw a dashed line, 5 red pixels, 5 white pixels */
$style = array($red, $red, $red, $red, $red, $w, $w, $w, $w, $w);
imagesetstyle($im, $style);
imageline($im, 0, 0, 100, 100, IMG_COLOR_STYLED);
/* Draw a line of happy faces using imagesetbrush() with imagesetstyle */
$style = array($w, $w, $w, $w, $w, $w, $w, $w, $w, $w, $w, $w, $red);
imagesetstyle($im, $style);
$brush = imagecreatefrompng("http://www.libpng.org/pub/png/images/smile.happy.png");
$w2 = imagecolorallocate($brush, 255, 255, 255);
imagecolortransparent($brush, $w2);
imagesetbrush($im, $brush);
imageline($im, 100, 0, 0, 100, IMG_COLOR_STYLEDBRUSHED);
imagejpeg($im);
imagedestroy($im);
?>
上述範例將輸出類似以下的內容
小心!如果您將空陣列作為第二個參數傳遞給 imagesetstyle(),它將會導致您的伺服器崩潰!
我剛才在使用它時不小心這樣做了,頁面花了一分多鐘的時間處理,然後我的 Apache 伺服器出現了 Windows 的「傳送錯誤報告」視窗。
需要說明的是,對於厚度大於 1 的線條,$style 陣列的總長度需要是線條長度的確切除數。
這是因為圖案會沿長度方向重複,並環繞到第二列像素,導致出現交錯現象。
因此,如果您有 5 個紅色像素和 5 個白色像素,並且您希望線條長度為 55 個像素,則可以將長度更改為 10 的倍數,或者將虛線更改為例如 6 個紅色和 5 個白色。
基本虛線的捷徑,可以輕鬆調整長度
<?php
$length1 = 20;
$length2 = 10;
$style = array_merge(array_fill(0, $length1, $red), array_fill(0, $length2, $w));
imagesetstyle($im, $style);
?>
建立帶有隨機漸變效果線條的函式
<?php
函數 fading_line($img,$sx,$sy,$ex,$ey){
$r=rand(0,5);$g=rand(0,5);$b=rand(0,5);
$l=sqrt((($ex-$sx)*($ex-$sx))+(($ey-$sy)*($ey-$sy)));
for($i=0;$i<$l;$i++){
$a = 陣列(255-((255/$l)*$i), 255,0,(255/$l)*$i/2,(255/$l)*$i,(255-((255/$l)*$i))/2);
$style[]=imagecolorallocate($img,$a[$r],$a[$g],$a[$b]);
}
imagesetstyle($img,$style);
imageline($img,$sx,$sy,$ex,$ey,IMG_COLOR_STYLED);
}
fading_line($img,10,20,490,40); // 影像, 起始 x, 起始 y, 結束 x, 結束 y
?>
當使用 imagesetstyle 繪製的線條似乎只產生一條細白線時,請確保已停用反鋸齒功能。
<?
imageantialias($im, false);
$style = 陣列($gridxcolor, $gridxcolor, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT);
imagesetstyle($im, $style);
imageline($im, $x, 0, $x, $ymax+5, IMG_COLOR_STYLED);
imageantialias($im, true);
?>
Setstyle 和反鋸齒功能不相容。
請注意,樣式是應用於線條的寬度而不是線性方向。
要轉換用於粗線條的樣式,您可以使用以下函數
<?php
/*
ImageStyleThicken(<aStyle>,<iThickness>) --> <aThickStyle>
<aStyle> 是厚度為 1 的樣式陣列(參見 imagesetstyle())。
<iThickness> 是要套用的新厚度(參見 imagesetthickness())。
<aThickStyle> 是適用於給定厚度的樣式陣列。
*/
function ImageStyleThicken($_1,$_2) {
$a = array();
foreach ($_1 as $x) {
$i = $_2;
do $a[] = $x; while (--$i>0); }
return $a;
}
?>
使用這個函式來設定像素的任意組合樣式。
您可以傳入任意數量的修飾符。
使用格式 [數量]r[紅色]g[綠色]b[藍色]。
例如:
$im=dashed($im,"4r255g0b0","2r0g255b0","5r0g0b255");
imageline($im, 0, 0, 600, 600, IMG_COLOR_STYLED);
function dashed($im){
$size = func_num_args();
for($i = 0; $i < $size; $i++){
$arg = func_get_arg($i);
if(!is_resource($arg)){
$r=substr($arg,strpos($arg,"r")+1,
strpos($arg,"g")-strpos($arg,"r")-1);
$g=substr($arg,strpos($arg,"g")+1,
strpos($arg,"b")-strpos($arg,"g")-1);
$b=substr($arg,strpos($arg,"b")+1,
strlen($arg)-strpos($arg,"b"));
$color = imagecolorallocate($im,$r,$g,$b);
$x = substr($arg,0,strpos($arg,"r"));
$vals[$i] = array_fill(0,$x,$color);
}
}
for($k=0;$k<count($vals)+1;$k++)
if(array_key_exists($k,$vals)) $prop=array_merge($prop,$vals[$k]);
imagesetstyle($im, $prop);
return $im;
}