PHP畫圖基礎

來源:互聯網
上載者:User

Title:    PHP畫圖基礎

Author:  MoreWindows

Blog:     http://blog.csdn.net/MoreWindows

KeyWord:    PHP繪圖 畫點、線、弧線 繪製和填充地區 圖片特效 彩色聖誕節大雪花圖

 

本篇對PHP常用的繪圖函數進行總結。內容有建立映像,為映像分配顏色,畫點,畫線,畫弧線,繪製和填充地區,輸出字元和漢字及一些常見的圖片特效如反色和浮雕。此外還給出一些有趣的執行個體,如繪製彩色的聖誕節大雪花圖。


一.建立映像

           resource imagecreate( int $x_size , int $y_size )

imagecreate()返回一個映像標識符,代表了一幅大小為 x_sizey_size 的空白映像。

          resource imagecreatetruecolor( int $x_size , int $y_size )

imagecreatetruecolor() 返回一個映像標識符,代表了一幅大小為 x_sizey_size 的黑色映像。PHP手冊上推薦盡量使用imagecreatetruecolor()函數。

還有根據.gif、.png、.jpg等檔案來建立映像的函數。

         resource imagecreatefromgif( string $filename )

         resource imagecreatefrompng ( string $filename )

         resource imagecreatefromjpeg( string $filename )


二.為映像分配顏色

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

imagecolorallocate() 返回一個標識符,代表了由給定的 RGB 成分組成的顏色。redgreenblue 分別是所需要的顏色的紅,綠,藍成分。這些參數是 0 到 255 的整數或者十六進位的 0x00 到 0xFF。第一次映像調用 imagecolorallocate()表示設定映像背景色。

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

imagecolorallocatealpha() 的行為和imagecolorallocate()相同,但多了一個額外的透明度參數alpha,其值從 01270表示完全不透明,127 表示完全透明。

 

三.畫點

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

註:映像左上方為(0,0)

 

四.畫線

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

從(x1, y1)到(x2,y2)。線的風格可以由bool imagesetstyle( resource $image , array $style )來控制。寬度由bool imagesetthickness ( resource $image , int $thickness )控制,注意這個寬度在畫矩形、弧線時也生效。

 

五.畫橢圓弧

         bool imagearc(resource $image , int $cx , int $cy , int $w , int $h , int $s , int $e , int $color)

imagearc()cxcy(映像左上方為 0, 0)為中心在 image 所代表的映像中畫一個橢圓弧。wh 分別指定了橢圓的寬度和高度,起始和結束點以 se參數以角度指定。0度位於三點鐘位置,以順時針方向繪畫。如:

$black = imagecolorallocate($img, 0, 0, 0);

imagearc($img, 100, 100, 150, 180, 0, 90,$black);

將在(100,100)處畫一段寬150高180的從0到90度的弧,如所示(作為參照,右邊是全圖):


 

六.繪製地區

          矩形

          bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )

         橢圓

         bool imageellipse ( resource $image , int $cx , int $cy , int $w , int $h , int $color )

         多邊形

         bool imagepolygon ( resource $image , array $points , int $num_points , int $color )

 

七.填充地區

       填充地區

       bool imagefill( resource $image , int $x , int $y , int $color )

imagefill()在image映像的(x,y)處用 color顏色執列區域填充(即與 (x, y) 點顏色相同且相鄰的點都會被填充)。如以下程式碼片段會先畫出藍色的橢圓,然後用紅色填充橢圓內部。

$blue_color = imagecolorallocate($img, 0, 0, 255);             $red_color = imagecolorallocate($img, 255, 0, 0);imageellipse($img, 300, 200, 300, 200, $blue_color);imagefill($img, 300, 200, $red_color);
運行效果如下:

       畫一橢圓並填充

       bool imagefilledellipse( resource $image , int $cx , int $cy , int $w , int $h , int $color )

這種畫法橢圓是沒有邊框的,當然也可以如下實現:

$lucency_color = imagecolorallocatealpha($img, 0, 0, 0, 126);//127為全透明 0全不透明$red_color = imagecolorallocate($img, 255, 0, 0);imageellipse($img, 300, 200, 300, 200, $lucency_color);imagefill($img, 300, 200, $red_color);//imagefilledellipse($img, 300, 200, 300, 200, $red_color);

        畫一矩形並填充

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

類似於畫一橢圓並填充。

 

         畫一橢圓弧且填充

         bool imagefilledarc( resource $image , int $cx , int $cy , int $w , int $h , int $s , int $e , int $color , int $style )

對最後一個參數說明下,有4種值:

IMG_ARC_PIE    產生圓形邊界(如果兩個都用,IMG_ARC_CHORD生效)。

IMG_ARC_CHORD  用直線串連了起始和結束點。

IMG_ARC_NOFILL畫弧,只有輪廓,不填充。

IMG_ARC_EDGED  指明用直線將起始和結束點與中心點相連

看下實際的效果(圓弧角度從0到210度):


下一篇將用這個函數來畫餅狀圖。


八.字元

       水平地畫一個字元

       bool imagechar(resource $image , int $font , int $x , int $y , string $c , int $color)

      垂直地畫一個字元

      bool imagecharup(resource $image , int $font , int $x , int $y , string $c , int $color)

      水平地畫一行字串

      bool imagestring(resource $image , int $font , int $x , int $y , string $s , int $col)

      垂直地畫一行字串

       bool imagestringup(resource $image , int $font , int $x , int $y , string $s , int $col)

$font參數要注意下,要麼使用內建的字型(從1到5),要麼用int imageloadfont ( string $file )載入字型後再設定。

可以用輸出*來得到彩色的聖誕節雪花圖,代碼如下:

<?php// by MoreWindows( http://blog.csdn.net/MoreWindows )$imgWidth = 300;$imgHeight = 200;$img = imagecreate($imgWidth, $imgHeight);imagecolorallocate($img, 255, 255, 255);//設定底色$snowflake_size = 5; //可從1到5//產生雪花 其實就是調用imagechar()輸出*號for ($i=1; $i<=400; $i++)     imagechar($img, $snowflake_size, mt_rand(0, $imgWidth),mt_rand(0, $imgHeight), "*", imagecolorallocate($img, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255))); imagepng($img);imagedestroy($img);?>

運行效果如下:



九.文本

       array imagettftext(resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )

解釋幾個參數:

第二參數$size為字型大小。

第三參數$angle為文本旋轉角度,0度為從左向右讀的文本,更高數值表示逆時針旋轉。例如 90 度表示從下向上讀的文本。

第七個參數$fontfile表示字型檔,如"c:\\WINDOWS\\Fonts\\simhei.ttf"。

注意!使用這個函數應當配合imagecreatetruecolor(),而不是imagecreate()。

下面用這個imagettftext()來代替上面的imagechar從而產生彩色的聖誕節大雪花圖,代碼如下:

<?php// by MoreWindows( http://blog.csdn.net/MoreWindows )$imgWidth = 600;$imgHeight = 400;$img = imagecreatetruecolor($imgWidth, $imgHeight);imagefill($img, 0, 0, imagecolorallocate($img, 240, 240, 240));//設定底色$snowflake_size = 30;$font_file = "c:\\WINDOWS\\Fonts\\simhei.ttf"; //產生大雪花 其實就是調用imagettftext()輸出*號for ($i=1; $i<=400; $i++) {$font_color = imagecolorallocate($img, mt_rand(100,200), mt_rand(100,200), mt_rand(100,200));imagettftext($img, $snowflake_size, mt_rand(0, 180), mt_rand(0, $imgWidth),mt_rand(0, $imgHeight), $font_color, $font_file, "*"); }//浮水印文字$black_color = imagecolorallocate($img, 0, 0, 0);imagettftext($img, 12, 0, $imgWidth - 200 , $imgHeight - 20, $black_color, $font_file, "大雪花圖  by MoreWindows");imagepng($img);imagedestroy($img);?>

運行效果如下:


 

十.映像特效

        bool imagefilter ( resource $src_im , int $filtertype [,int $arg1 [, int $arg2 [, int $arg3 ]]] )

這裡提供了很多特效,如浮雕,反色(底片色),調節灰階、亮度,對比,模糊化等等。這隻展示幾種常用的特效,更多的請訪問http://www.php.net/manual/zh/function.imagefilter.php。

原圖:


將圖處儲存到D:\\1234.png,就可以執行下面的代碼了。

IMG_FILTER_NEGATE:將映像中所有顏色反轉(底片色)。


代碼:

<?php// by MoreWindows( http://blog.csdn.net/MoreWindows )$img = imagecreatefrompng("D:\\1234.png");imagefilter($img, IMG_FILTER_NEGATE);imagepng($img);imagedestroy($img);?>

IMG_FILTER_EMBOSS:使映像浮雕化。


代碼:

<?php// by MoreWindows( http://blog.csdn.net/MoreWindows )$img = imagecreatefrompng("D:\\1234.png");imagefilter($img, IMG_FILTER_EMBOSS);imagepng($img);imagedestroy($img);?>

本篇就介紹到此,下一篇《PHP 畫圖應用 驗證碼 柱狀圖》將用本篇介紹的函數來繪製驗證碼和柱狀圖。

 

 

轉載請標明出處,原文地址:http://blog.csdn.net/morewindows/article/details/7274870

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.