日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

PHP画图基础

發(fā)布時間:2025/4/14 php 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP画图基础 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Title:?? ?PHP畫圖基礎(chǔ)

Author:? MoreWindows

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

KeyWord:??? PHP繪圖 畫點(diǎn)、線、弧線 繪制和填充區(qū)域 圖片特效 彩色圣誕節(jié)大雪花圖

?

本篇對PHP常用的繪圖函數(shù)進(jìn)行總結(jié)。內(nèi)容有建立圖像,為圖像分配顏色,畫點(diǎn),畫線,畫弧線,繪制和填充區(qū)域,輸出字符和漢字及一些常見的圖片特效如反色和浮雕。此外還給出一些有趣的實(shí)例,如繪制彩色的圣誕節(jié)大雪花圖。


一.新建圖像

?????????? resource imagecreate( int $x_size , int $y_size )

imagecreate()返回一個圖像標(biāo)識符,代表了一幅大小為 x_size 和y_size 的空白圖像。

????????? resource imagecreatetruecolor( int $x_size , int $y_size )

imagecreatetruecolor() 返回一個圖像標(biāo)識符,代表了一幅大小為 x_size 和y_size 的黑色圖像。PHP手冊上推薦盡量使用imagecreatetruecolor()函數(shù)。

還有根據(jù).gif、.png、.jpg等文件來創(chuàng)建圖像的函數(shù)。

???????? resource imagecreatefromgif( string $filename )

????? ?? resource imagecreatefrompng ( string $filename )

???????? resource imagecreatefromjpeg( string $filename )


二.為圖像分配顏色

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

imagecolorallocate() 返回一個標(biāo)識符,代表了由給定的 RGB 成分組成的顏色。red,green 和 blue 分別是所需要的顏色的紅,綠,藍(lán)成分。這些參數(shù)是 0 到 255 的整數(shù)或者十六進(jìn)制的 0x00 到 0xFF。第一次圖像調(diào)用 imagecolorallocate()表示設(shè)置圖像背景色。

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

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

?

三.畫點(diǎn)

?????????? 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)。線的風(fēng)格可以由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()以cx,cy(圖像左上角為 0, 0)為中心在 image 所代表的圖像中畫一個橢圓弧。w和h 分別指定了橢圓的寬度和高度,起始和結(jié)束點(diǎn)以 s 和e參數(shù)以角度指定。0度位于三點(diǎn)鐘位置,以順時針方向繪畫。如:

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

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

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


?

六.繪制區(qū)域

????????? 矩形

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

?

七.填充區(qū)域

?????? 填充區(qū)域

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

imagefill()在image圖像的(x,y)處用 color顏色執(zhí)行區(qū)域填充(即與 (x, y) 點(diǎn)顏色相同且相鄰的點(diǎn)都會被填充)。如以下代碼片段會先畫出藍(lán)色的橢圓,然后用紅色填充橢圓內(nèi)部。

$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);運(yùn)行效果如下:


?????? 畫一橢圓并填充

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

這種畫法橢圓是沒有邊框的,當(dāng)然也可以如下實(shí)現(xiàn):

$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 )

對最后一個參數(shù)說明下,有4種值:

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

IMG_ARC_CHORD? 用直線連接了起始和結(jié)束點(diǎn)。

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

IMG_ARC_EDGED? 指明用直線將起始和結(jié)束點(diǎn)與中心點(diǎn)相連

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


下一篇將用這個函數(shù)來畫餅狀圖。


八.字符

?????? 水平地畫一個字符

?????? 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參數(shù)要注意下,要么使用內(nèi)置的字體(從1到5),要么用int imageloadfont ( string $file )加載字體后再設(shè)置。

可以用輸出*來得到彩色的圣誕節(jié)雪花圖,代碼如下:

<?php // by MoreWindows( http://blog.csdn.net/MoreWindows ) $imgWidth = 300; $imgHeight = 200; $img = imagecreate($imgWidth, $imgHeight); imagecolorallocate($img, 255, 255, 255);//設(shè)置底色 $snowflake_size = 5; //可從1到5 //生成雪花 其實(shí)就是調(diào)用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); ?>

運(yùn)行效果如下:



九.文本

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

解釋幾個參數(shù):

第二參數(shù)$size為字體大小。

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

第七個參數(shù)$fontfile表示字體文件,如"c:\\WINDOWS\\Fonts\\simhei.ttf"。

注意!使用這個函數(shù)應(yīng)當(dāng)配合imagecreatetruecolor(),而不是imagecreate()。

下面用這個imagettftext()來代替上面的imagechar從而生成彩色的圣誕節(jié)大雪花圖,代碼如下:<?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));//設(shè)置底色 $snowflake_size = 30; $font_file = "c:\\WINDOWS\\Fonts\\simhei.ttf"; //生成大雪花 其實(shí)就是調(diào)用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); ?>

運(yùn)行效果如下:


?

十.圖像特效

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

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

原圖:


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

IMG_FILTER_NEGATE:將圖像中所有顏色反轉(zhuǎn)(底片色)。


代碼:

<?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 畫圖應(yīng)用 驗證碼 柱狀圖》將用本篇介紹的函數(shù)來繪制驗證碼和柱狀圖。

?

?

轉(zhuǎn)載請標(biāo)明出處,原文地址:http://blog.csdn.net/morewindows/article/details/7274870



轉(zhuǎn)載于:https://www.cnblogs.com/long12365/p/9731294.html

總結(jié)

以上是生活随笔為你收集整理的PHP画图基础的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。