首頁  >  文章  >  後端開發  >  詳細分析PHP中怎樣定義顏色、繪製點、線和矩形?

詳細分析PHP中怎樣定義顏色、繪製點、線和矩形?

WBOY
WBOY原創
2021-10-19 17:14:112504瀏覽

在之前的文章中為大家帶來了《PHP中怎麼輸出圖片? (圖例詳解)》,其中詳細介紹了應該怎樣在PHP中輸出圖片,本篇文章繼續給大家帶來我們應該怎樣在PHP中繪製圖像,希望能夠幫助到大家!

詳細分析PHP中怎樣定義顏色、繪製點、線和矩形?

在PHP中繪製圖像一切還是基於上一篇文章中的畫布,建立畫布,然後在畫布上進行繪製圖像。想到圖像我們就想到了色彩,所以首先,我們來看一下,我們應該如何在PHP中為圖像定義顏色。

圖像定義顏色

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

  • imagecolorallocate()函數

imagecolorallocate() 函數能夠為映像分配顏色,想要設定多種顏色的話,需要多次呼叫該函數,函數的語法格式:

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

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

範例如下:

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

輸出結果:

詳細分析PHP中怎樣定義顏色、繪製點、線和矩形?

  • # imagecolorallocatealpha()

    函數

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

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

其中,前面的參數與imagecolorallocate()函數的參數表示為一致的,$alphab表示的是透明度的參數,取值範圍在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(&#39;Content-type:image/png&#39;);
    //最后输出结果
    imagepng($image);
    imagedestroy($image);
?>

輸出結果:

詳細分析PHP中怎樣定義顏色、繪製點、線和矩形?

#由此透過imagecolorallocate() 和imagecolorallocatealpha()這兩個函數已經能夠實現在圖像上定義顏色了。同時影像不僅是由顏色構成的,還需要有點、線條還有不同的形狀。那接下來我們來看看,應該怎麼去解決這些問題。 繪製點和線

繪製點和線可以說是PHP中繪製影像最基本的操作了,雖然很基本,但是靈活應用起來,可以透過它們繪製出更多複雜的圖像,我們可以透過

 imagesetpixel()

函數在畫布中繪製一個點,也可以設定點的顏色,它的函數的語法格式如下:

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

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

繪製一條線段則可以使用

imageline()

函數,其語法格式如下:

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

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

接下來我們可以透過循環和隨機數的結合來進行範例:

<?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(&#39;Content-type:image/jpeg&#39;);
    imagejpeg($img);
    imagedestroy($img);
?>

#輸出結果:

詳細分析PHP中怎樣定義顏色、繪製點、線和矩形?

繪製矩形

在PHP中,我們想要繪製矩形的話,需要透過 imagerectangle()
imagefilledrectangle()

函數來進行。 imagefilledrectangle() 函數會在繪製完成矩形後填入矩形,但是imagerectangle() 函數不會。它們的語法格式如下:


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()函數後的顏色代表的是矩形邊線的顏色,imagefilledrectangle()函數後的顏色表示的是矩形內的填滿顏色。

接下來透過範例透過 imagerectangle() 或imagefilledrectangle() 函數分別繪製一個矩形,範例如下:

<?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(&#39;Content-type:image/jpeg&#39;);
    imagejpeg($img);
    imagedestroy($img);
?>

輸出結果:

詳細分析PHP中怎樣定義顏色、繪製點、線和矩形?

## #####

推薦學習:《PHP影片教學

以上是詳細分析PHP中怎樣定義顏色、繪製點、線和矩形?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn