溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP中定義顏色、繪制點(diǎn)、線和矩形的方法步驟

發(fā)布時(shí)間:2021-10-20 09:35:19 來源:億速云 閱讀:295 作者:iii 欄目:編程語言

這篇文章主要講解了“PHP中定義顏色、繪制點(diǎn)、線和矩形的方法步驟”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“PHP中定義顏色、繪制點(diǎn)、線和矩形的方法步驟”吧!

PHP中定義顏色、繪制點(diǎn)、線和矩形的方法步驟

在PHP中繪制圖像一切還是基于上一篇文章中的畫布,創(chuàng)建畫布,然后在畫布上進(jìn)行繪制圖像。想到圖像我們就想到了色彩,所以首先,我們來看一下,我們應(yīng)該怎樣在PHP中給圖像定義顏色。

圖像定義顏色

在我們使用PHP進(jìn)行圖像操作時(shí),必然離不開的就是顏色的設(shè)置,不同的顏色勾勒出了這漂亮的圖像。那么在PHP中我們應(yīng)該怎樣給圖像來提供顏色呢?這時(shí)候我們就要用到imagecolorallocate() 和 imagecolorallocatealpha()這兩個(gè)函數(shù)。接下來,我們就來看一看應(yīng)該怎樣使用這兩個(gè)函數(shù)。

  • imagecolorallocate()函數(shù)

imagecolorallocate() 函數(shù)能夠?yàn)閳D像分配顏色,想要設(shè)置多種顏色的話,需要多次調(diào)用該函數(shù),函數(shù)的語法格式:

imagecolorallocate(resource $image, int $red, int $green, int $blue)

其中,$image表示了需要設(shè)置顏色的圖像,該函數(shù)會(huì)返回一個(gè)標(biāo)識(shí)符,表示了給定的RGB成分組成的顏色,$red,$green 和 $blue 分別是所需要的顏色的紅,綠,藍(lán)成分,取值范圍是 0 到 255 的整數(shù)或者十六進(jìn)制的 0x00 到 0xFF。

示例如下:

<?php
    $image = imagecreate(100, 100);
    $blue = imagecolorallocate($image, 0, 0, 255);
    header('Content-type:image/jpeg');
    imagejpeg($image);
    imagedestroy($image);
?>

輸出結(jié)果:

PHP中定義顏色、繪制點(diǎn)、線和矩形的方法步驟

  • imagecolorallocatealpha()函數(shù)

imagecolorallocatealpha()函數(shù)與imagecolorallocate()函數(shù)相比,它們的作用是相同的,但是多了一個(gè)用來設(shè)置透明參數(shù)的alpha,它的語法格式如下:

imagecolorallocatealpha(resource $image, int $red, int $green, int $blue, int $alpha)

其中,前面的參數(shù)與imagecolorallocate()函數(shù)的參數(shù)表示為一致的,$alphab表示的是透明度的參數(shù),取值范圍在 0 到 127 之間,0 表示完全不透明,127 則表示完全透明。

示例如下:

<?php
    $size=300;
    $image=imagecreatetruecolor($size,$size);
    $back=imagecolorallocate($image,0,0,0);
    $border=imagecolorallocate($image,255,255,255);
    imagefilledrectangle($image,0,0,$size-1,$size-1,$back);
    imagerectangle($image,0,0,$size-1,$size-1,$border);
    $yellow_x=100;
    $yellow_y=75;
    $red_x=100;
    $red_y=165;
    $blue_x=187;
    $blue_y=125;
    $radius=150;
    //用alpha值分配一些顏色
    $yellow=imagecolorallocatealpha($image,200,200,0,75);
    $red=imagecolorallocatealpha($image,200,0,0,75);
    $blue=imagecolorallocatealpha($image,0,0,200,75);
    //畫3個(gè)交迭的圓
    imagefilledellipse($image,$yellow_x,$yellow_y,$radius,$radius,$yellow);
    imagefilledellipse($image,$red_x,$red_y,$radius,$radius,$red);
    imagefilledellipse($image,$blue_x,$blue_y,$radius,$radius,$blue);
    //不要忘記輸出正確的header!
    header('Content-type:image/png');
    //最后輸出結(jié)果
    imagepng($image);
    imagedestroy($image);
?>

輸出結(jié)果:

PHP中定義顏色、繪制點(diǎn)、線和矩形的方法步驟

由此通過imagecolorallocate() 和 imagecolorallocatealpha()這兩個(gè)函數(shù)已經(jīng)能夠?qū)崿F(xiàn)在圖像上定義顏色了。同時(shí)圖像不僅是由顏色構(gòu)成的,還需要有點(diǎn)、線還有不同的形狀。那接下來我們來看一看,應(yīng)該怎樣去解決這些問題。

繪制點(diǎn)和線

繪制點(diǎn)和線可以說是PHP中繪制圖像最基本的操作了,雖然很基本,但是靈活應(yīng)用起來,可以通過它們繪制出更多復(fù)雜的圖像,我們可以通過 imagesetpixel() 函數(shù)在畫布中繪制一個(gè)點(diǎn),也可以設(shè)置點(diǎn)的顏色,它的函數(shù)的語法格式如下:

imagesetpixel(resource $image, int $x, int $y, int $color)

其中,$image表示的是創(chuàng)建的畫布,$x和$y表示的是在($x,$y)這個(gè)坐標(biāo)點(diǎn),這個(gè)坐標(biāo)點(diǎn)的顏色是$color。

繪制一條線段則可以使用 imageline() 函數(shù),其語法格式如下:

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

其中,表示在坐標(biāo)($x1,$y1)到坐標(biāo)($x2,$y2)的一條顏色為$color的線段。

接下來我們可以通過循環(huán)和隨機(jī)數(shù)的結(jié)合來進(jìn)行示例:

<?php
    $img = imagecreate(200, 100);
    imagecolorallocate($img, 0, 0, 0);
    $blue = imagecolorallocate($img, 0, 0, 255);
    $red = imagecolorallocate($img, 255, 0, 0);
    for ($i=0; $i <= 50; $i++) {
        $color = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
        imagesetpixel($img, rand(0, 200), rand(0, 100), $color);
        imageline($img, rand(0, 200), rand(0, 100), rand(0, 200), rand(0, 100), $color);
    }
    header('Content-type:image/jpeg');
    imagejpeg($img);
    imagedestroy($img);
?>

輸出結(jié)果:

PHP中定義顏色、繪制點(diǎn)、線和矩形的方法步驟

繪制矩形

在PHP中,我們想要繪制矩形的話,需要通過 imagerectangle() 或者 imagefilledrectangle() 函數(shù)來進(jìn)行。imagefilledrectangle() 函數(shù)會(huì)在繪制完成矩形后填充矩形,但是imagerectangle() 函數(shù)不會(huì)。它們的語法格式如下:

imagerectangle(resource $image, int $x1, int $y1, int $x2, int $y2, int $color)
imagefilledrectangle(resource $image, int $x1, int $y1, int $x2, int $y2, int $color)

其中,兩者表示的都是繪制一個(gè)左上角坐標(biāo)為($x1,$y1),右下角坐標(biāo)為($x2,$y2)的矩形,兩者不一樣的是:imagerectangle()函數(shù)后的顏色代表的是矩形邊線的顏色,imagefilledrectangle()函數(shù)后的顏色表示的是矩形內(nèi)的填充顏色。

接下來通過示例通過 imagerectangle() 或者 imagefilledrectangle() 函數(shù)分別繪制一個(gè)矩形,示例如下:

<?php
    $img = imagecreate(300, 150);
    imagecolorallocate($img, 255, 255, 255);
    $green = imagecolorallocate($img, 0, 255, 0);
    $blue = imagecolorallocate($img, 0, 0, 255);
    imagerectangle($img, 5, 5, 145, 145, $green);
    imagefilledrectangle($img, 150, 5, 295, 145, $blue);
    header('Content-type:image/jpeg');
    imagejpeg($img);
    imagedestroy($img);
?>

輸出結(jié)果:

PHP中定義顏色、繪制點(diǎn)、線和矩形的方法步驟

感謝各位的閱讀,以上就是“PHP中定義顏色、繪制點(diǎn)、線和矩形的方法步驟”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對PHP中定義顏色、繪制點(diǎn)、線和矩形的方法步驟這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI