溫馨提示×

溫馨提示×

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

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

PHP中如何定義顏色、繪制點、線和矩形

發(fā)布時間:2021-10-19 17:34:25 來源:億速云 閱讀:128 作者:小新 欄目:編程語言

這篇文章主要介紹PHP中如何定義顏色、繪制點、線和矩形,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

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

圖像定義顏色

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

  • imagecolorallocate()函數(shù)

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

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

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

示例如下:

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

輸出結果:

PHP中如何定義顏色、繪制點、線和矩形

  • imagecolorallocatealpha()函數(shù)

imagecolorallocatealpha()函數(shù)與imagecolorallocate()函數(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個交迭的圓
    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');
    //最后輸出結果
    imagepng($image);
    imagedestroy($image);
?>

輸出結果:

PHP中如何定義顏色、繪制點、線和矩形

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

繪制點和線

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

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

其中,$image表示的是創(chuàng)建的畫布,$x和$y表示的是在($x,$y)這個坐標點,這個坐標點的顏色是$color。

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

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

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

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

<?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);
?>

輸出結果:

PHP中如何定義顏色、繪制點、線和矩形

繪制矩形

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

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)

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

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

<?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);
?>

輸出結果:

PHP中如何定義顏色、繪制點、線和矩形

以上是“PHP中如何定義顏色、繪制點、線和矩形”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

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

php
AI