首頁 後端開發 php教程 photoshop cs2 v9.0 綠色中文版 php 影像函數大舉例(非原始)

photoshop cs2 v9.0 綠色中文版 php 影像函數大舉例(非原始)

Jul 29, 2016 am 08:40 AM

如下方式是一種方法:
if(!function_exists('imagecreate')) {
die('本伺服器不支援GD模組');
}
如果不支援的話,如何設定? 下載gd模組的dll檔,修改php.ini,重啟伺服器即可.
以下簡稱PHP作圖為PS.
當您打算PS的話,應該完成如下以下步驟,這是必經的.
1:建立基本PS物件(我假設為$image),填充背景(預設黑),以後的全部ps操作都是基於這個背景影像的.
2:在$image上作圖.
3:輸出這個圖像.
4:銷毀物件,清除使用記憶體.
首先,我們來認識幾個常用的函數,這些函數在php手冊裡面都有詳細介紹,此處大體引用下.
resource imagecreate ( int x_size, int y_size )
imagecreate() 傳回一個圖片標識符​​,代表了一幅大小為x_size 和y_size 的空白圖片。
此函數基本上是同imagetruecolor($width,$height).
------------------------------- --------------------------
int imagecolorallocate ( resource image, int red, int green, int blue )
imagecolorallocate()傳回一個標識符,代表了由給定的RGB 成分組成的顏色。 image 參數是 imagecreatetruecolor() 函數的回傳值。 red,green 和 blue 分別是所需的顏色的紅,綠,藍成分。這些參數是 0 到 255 的整數或十六進位的 0x00 到 0xFF。 imagecolorallocate() 必須被呼叫以建立每一種用在 image 所代表的影像中的顏色。
----------------------------------------------- ----------
bool imagefill ( resource image, int x, int y, int color )
imagefill() 在image 影像的座標x,y(圖左上角為0, 0 )處以color 顏色執行區域填滿(即與x, y 點顏色相同且相鄰的點都會被填滿)。
----------------------------------------------- ----------
bool imageline ( resource image, int x1, int y1, int x2, int y2, int color )
imageline() 用color 顏色在圖片image 中從座標x1 ,y1 到x2,y2(影像左上角為0, 0)畫一條線段。
----------------------------------------------- ----------
bool imagestring ( resource image, int font, int x, int y, string s, int col )
imagestring() 用col 顏色將字串s 畫到image所代表的圖像的x,y 座標處(這是字串左上角座標,整個圖像的左上角為0,0)。如果 font 是 1,2,3,4 或 5,則使用內建字體。
----------------------------------------------- ----------
array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )
本函數比較重要,參數較多,此處不再列出,它主要是寫字到圖像上,和上面的函數類似,但必前者強大.
------------------- --------------------------------------
bool imagefilltoborder ( resource image, int x, int y, int border, int color )
imagefilltoborder() 從x,y(圖片左上角為0, 0)點開始用color 顏色執行區域填充,直到碰到顏色為border 的邊界。 【註:邊界內的所有顏色都會被填滿。如果指定的邊界色和該點顏色相同,則沒有填滿。如果影像中沒有該邊界色,則整個影像都會被填滿。 】
---------------------------------------------- --
bool imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color )
imagefilledellipse() 在image 所代表的圖片中以cx,cy(圖片為0, 0)為中心畫一個橢圓。 w 和 h 分別指定了橢圓的寬和高。橢圓用 color 顏色填滿。如果成功則傳回 TRUE,失敗則傳回 FALSE。
=============================================== ==
輸出影像資料:imagepng($image[,$filename])
============================== ==========================
例一:輸出藍色背景與交叉白線的圖形
$width=35;
$height=35;
//建立物件
$image=imagecreate($width,$height);
//擷取顏色
$color_white=imagecolorallocate ($image,255,255,255);//白色
$color_blue=imagecolorallocate($image,0,0,108);//藍色
imagefill($image,0,0,$color_blue);
/ /作圖
//線寬
imagesetthickness($image,3);
imageline($image,0,0,$width,$height ,$color_white);
imageline($image ,$width,0,0,$height ,$color_white);
//發送物件至頭
header('content-type:image/png');
imagepng($image);
/*
//發送物件至檔案
$filename="ex1.png";
imagepng($image,$filename);
*/
//銷毀物件
imagedestroy($image);
?>
輸出影像:
線上示範:http://www.phzzy.org/temp/5do8/ex1.php
例二: 陰陽圖
$width=400;
$height=400;
$image=imagecreatetruecolor($width,$height);
//提取顏色
$color_black= imagecolorallocate($image,0,2,0);//
$color_white=imagecolorallocate($image,255,255,255);//白色
$color_blue=imagecolorallocate($image,0,0,1088);色
$color_red=imagecolorallocate($image,151,0,4);//紅色
$color_my=imagecolorallocate($image,192,192,255);//背景
$color_temp=image,ocate( 199,199,199);//背景
//作圖
imagefill($image,0,0,$color_white);
//第一個是大圓
imagefilledarc ($image,$width/ 2,$height/2,$height,$height,0,360,$color_blue,IMG_ARC_PIE);
//兩個小圓
imagefilledellipse ($image,$width/2,$height/4 ,$height /2,$height/2,$color_red);
imagefilledellipse ($image,$width/2,$height/4 * 3,$height/2,$height/2,$color_blue);
/ *imagefilledellipse -- 畫一橢圓並填滿*/
imagefilledarc ($image,$width/2,$height/2,$height,$height,-90,90,$color_red,IMG_ARC_PIE);
imagefilleipse ($image,$width/2,$height/4 * 3,$height/2,$height/2,$color_blue);
//發送物件至頭
header('content-type:image /png');
imagepng($image);
/*
//發送物件
$filename="ex1.png";
imagepng($image,$filename );
*/
//銷毀物件
imagedestroy($image);
?>
示範:
http://www.phzzy.org/temp/5do8/ ex2.php
例三:3D圖片--cool
$width=400;
$height=400;
$image = imagecreatetruecolor($width, $height) ;
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0); $navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$darknavy = imagecolorallocate($image, 0x00, 0x00, 000,000,0005, color00, 0x00, colorx image, 0xFF, 0x00, 0x00);
$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);
imagefill($image,0,0, 0x00);
imagefill($image,0,0,$white. 3D effect
for ($i = $height /2 +20; $i > $height /2; $i--) {
imagefilledarc($image, $width/2, $i, $width/ 2, $height /2, 0, 45, $darknavy, IMG_ARC_PIE);
imagefilledarc($image, $width/2, $i, $width/2, $height /2, 45, 75 , $darkgray, IMG_ARC_PIE);
imagefilledarc($image, $width/2, $i, $width/2, $height /2, 75, 360 , $darkred, IMG_ARC_PIE);
}
imagefilledarc($image, $width/2, $height /2, $width/2, $height /2, 0, 45, $navy, IMG_ARC_PIE);
imagefilledarc($image, $width/2 , $height /2, $width/2, $height /2, 45, 75 , $gray, IMG_ARC_PIE);
imagefilledarc($image, $width/2, $height /2, $width/2, $ height /2, 75, 360 , $red, IMG_ARC_PIE);
// flush image
header('Content-type: image/png');
imagepng($image); imagedestroage ($image);
/*
//傳送物件至檔案
$filename="ex1.png";
imagepng($image,$filename);
*/
?>
輸出:
示範:http://www.phzzy.org/temp/5do8/ex3.php
例四:簡單的驗證碼
PHP建立驗證碼非常容易,容易的要死,簡單的思路是這樣的:
隨機種子生成,提取隨機字元,相連打印到圖形,輸出.,為了防止色盲,可以隨機提取顏色,也可以自訂顏色,下面看看:
session_start();
$width=65;
$height=20;
$sourcestrings="0123456789aqwertyuiopasdfghjkxcvbc $ height);
$colorarrs=array(
imagecolorallocate($image,255,255,255),//white
imagecolorallocate($image,0 ,0 , 0)///1ack
); unset($sessionval);
imagesetthickness($image,3);
//隨機得到字串個數
$strsize=rand(3,5);
imagefill($image,0 ,0,$colorarrs[0]);
//一個個的寫字串到圖片
for($i=0;$i$i_temp=rand (1,62);
$sessionval .=$sourcestrings[$i_temp];
$f
$y_i = $height/2 + $font_size /3 ;
imagechar($image,5 , 1+ $i * $width /$strsize,5,$sourcestrings[$i_temp],$fontcolor);
}
//寫入session,以後驗證用
unset($_SESSION[' cjjer']);
$_SESSION['cjjer'] = $sessionval;
//新增雜點
for($i=0;$i{ $i_x=rand(0,$width);
$i_y=rand(0,$height);
$pixelcolor=imagecolorallocate($image,rand(0,255),rand(0,255) ,rand(0,255));
imagesetpixel($image,$i_x,$i_y,$pixelcolor);
}
header('content-type:image/png');
imagepng( $image);
imagedestroy($image);
?>
產生的樣式示範:
線上示範:http://www.phzzy.org/temp/5do8/ex4_login.php
有個很明顯的問題就是生成的圖片不夠艷麗,從而很多的用戶看起來不清楚,這樣吧,我們自己設定幾個比較艷麗的顏色然後輸出,擴展colorarrs數組:
$colorarrs= array(
imagecolorallocate($image,255,255,255),
imagecolorallocate($image,0,0,0),
imagecolorallocate($image,0,70,0),
imagecolorallocate($image,0,70,0),
imagecolorallocate($image,0,70,0), <.>imagecolorallocate($image,0,0,128),
imagecolorallocate($image,233,10,216)
);
然後把23行變成(17行):
$f
輸出:
線上示範:http://www.phzzy.org/temp/5do8/ex5_login.php
例五:大點的比較cool的驗證碼
PS 的圖像還是比較小的,有時候為了某些原因(個人站點為了玩cool,just我,商業站點玩風格,吸引用戶,just google,後話),驗證碼不是局限於十幾個px的限制,有時候完全可以整個2百多,沒啥問題,這時候,一種方案是把前面生成的小圖強制大點,可以不? 可以,但是,看起來不夠光滑,這是事實,明顯,寬頻不再是最重要的問題,不說其他的,下面示範幾個比較好看的生成方式:
session_start();
$width=600;
$ height=100;
if($height $height=$width / 4;
$sourcestrings="0123456789aqwertyuiopasdfghjklzxcvbnmQWERTYYm/ =imagecreate($width,$height);
$white=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$white);
//載入字型庫
$f
putenv('"gdf
$f
$f / 2);
//得到字串
unset($sessionval);
$strsize=rand( 5,8);
for($i=0;$i$i_temp=rand(1,62);
$sessionval .=$sourcestrings[$i_temp ];
$x_i =$font_size + $i *$width / ($strsize+1);
$y_i = $height / 2;
$angle_i=rand(-120,120);
$f
imageTTFText($image,$font_size,$angle_i,$x_i,$y_i,$fontcolor_a,$fontname,$sourcestrings[$i_temp]);
}
unset($_SESSION['cjjer ']);
$_SESSION['cjjer'] = $sessionval;
//雜點數目
for($i=0;$i{
$i_x=rand(0,$width);
$i_y=rand(0,$height);
imagesetpixel($image,$i_x,$i_y,imagecolorallocate($image ,rand(0,255),rand(0,2550),rand(0,255)));
}
//發送物件
header('content-type:image/png');
imagepng($image);
imagedestroy($image);
?>
線上測試: http://www.phzzy.org/temp/5do8/ex6_login.php
解釋性說明:
首先是寬和高,高太小字都看不清楚.隨機提取的字符還是那麼幾個:
$sourcestrings="0123456789aqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
建立物件,填入白色:
$image=$imagecreate(>建立物件,填入白色: ,255) ;
imagefill($image,0,0,$white);
然後加載您要驗證碼的字體:
$f//返回當前根目錄,字體檔案複製到這裡,字體檔案是*.ttf檔
putenv('"gdf
$f
定義字元的高度,這裡我設定成高度的一半:
$f / 2);
清除變數,隨機設定要產生字元的數:
unset($sessionval);
$strsize=rand(5,8);
迴圈,一個個的把字元打上去:
得到本次循環的字串.,並加在變數後面一會兒寫入session
$i_temp=rand(1,62);
$sessionval .=$sourcestrings[$i_temp];
得到寫入映像的字串的位置(x和y座標)
$x_i =$font_size + $i *$width / ($strsize+1);
$y_i = $height / 2;
設定傾斜度,是從正面看的,.
$angle_i=rand(-120,120);
隨機產生顏色,
$f
寫入到圖片;
imageTTFText($image,$font_size,$angle_i ,$x_i,$y_i,$fontcolor_a,$fontname,$sourcestrings[$i_temp]);
如果對此函數有疑問,請查閱相關資料.非常容易.
寫入到session,一邊註冊碼使用:
unset($_SESSION['cjjer']);
$_SESSION['cjjer'] = $sessionval;
新增雜點:
//雜點數目
for( $i=0;$i{
$i_x=rand(0,$width);
$i_y=rand(0,$height) ;
imagesetpixel($image,$i_x,$i_y,imagecolorallocate($image,rand(0,255),rand(0,2550),rand(0,255)));
}
輸出到頭:
header('content-type:image/png');//這行顯示是png影像,可不要,預設可以輸出的.但不是影像的頭格式
imagepng( $image);
imagedestroy($image);
你可以載入你自己的字型庫,設定旋轉角度$angle_i=rand(-120,120);設定字型高度$f / 2);和隨機數的個數:$strsize=rand(5,8);
例六:給圖片打上水印,產生縮列圖
傳統的ASP頁子打水印和生成縮列圖都是比較繁瑣的,一般使用到的是其他組件什麼的,但是,PHP可以輕鬆的干這些事情,正如您預料,不到30行的程序搞定這一切,請看這個源程序:
$source="my.jpg";
$zoom=0.5;
$str='我是帥哥,你是MM麼?';
$image=imagecreatefromjpeg($source);
$width=imagesx($image);
$height=imagesy($image);
$color_red=imagecolorallocate($image,111,0,0);//紅色
$f " //simsun.ttc";
$str=iconv('GB2312','UTF-8',$str);
$f
$angle=25;
$fromx=$width /5;
$fromy=$height/2;
imagettftext($image,$fontsize,$angle,$fromx,$fromy,$color_red,$font,$str);
$width_temp= imagesx($image) * $zoom;
$height_temp=imagesy($image) * $zoom;
$img=imagecreatetruecolor($width_temp,$height_temp);
imagecopyresized ($img,$image, 0,0,0,0,$width_temp, $height_temp,$width,$height);
imagedestroy($image);
$file_zoomname="my_zoom_jpeg.jpg";
imagejpeg($img, $file_zoomname);
imagedestroy($img);
?>
原始圖片:
產生的jpg圖片:
原始圖片70K, 這裡說下,如果生成gif,文件18k多,而png要用去76k,so我們產生縮列圖用jpeg格式.
程式碼分析:
這裡我先設定了幾個參數:
$source="my.jpg"; //來源圖片
$zoom=0.5; //縮放百分比
$str='我是帥哥,你是MM麼?'; //水印的文字
裝載源圖(不打水印不裝載) :
$image=imagecreatefromjpeg($source);
取得長,寬的大小:
$width=imagesx($image);
$height=imagesy($image);
設定水印字體,因為我們用的是中文,必須導入中文字體庫,否則寫不上或亂碼,然後必須轉換字串編碼
$f "//simsun.ttc";
$str=iconv ('GB2312','UTF-8',$str);
設定開始點,字體大小,視角:,寫上字串:
$f
$angle=25;
$ fromx=$width/5;
$fromy=$height/2;
imagettftext($image,$fontsize,$angle,$fromx,$fromy,$color_red,$font,$str);
依照縮放的大小要求產生新大小的物件:
$width_temp=imagesx($image) * $zoom;
$height_temp=imagesy($image) * $zoom;
$img=imagecreatetruecolor( $width_temp,$height_temp);
把來源圖copy到新圖,gd函式庫的imagecopyresized自動縮放大小的
imagecopyresized ($img,$image,0,0,0,0,$width_temp, $height_temptemp, $height_temp ,$width,$height);
產生小圖片,清除物件:
imagedestroy($image);
$file_zoomname="my_zoom_jpeg.jpg";
imagejpeg($img,$file_zoomname)$file_zoomname)$file_zoomname)$file_zoomname)$file_zoomname)$file_zoomname) ;
imagedestroy($img);
產生縮洌圖,水印大體核心技術就這麼點.

以上就介紹了photoshop cs2 v9.0 綠色中文版 php 圖像函數大舉例(非原創),包括了photoshop cs2 v9.0 綠色中文版方面的內容,希望對PHP教程有興趣的朋友有所幫助。

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

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

在PHP API中說明JSON Web令牌(JWT)及其用例。 在PHP API中說明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

會話如何劫持工作,如何在PHP中減輕它? 會話如何劫持工作,如何在PHP中減輕它? Apr 06, 2025 am 12:02 AM

會話劫持可以通過以下步驟實現:1.獲取會話ID,2.使用會話ID,3.保持會話活躍。在PHP中防範會話劫持的方法包括:1.使用session_regenerate_id()函數重新生成會話ID,2.通過數據庫存儲會話數據,3.確保所有會話數據通過HTTPS傳輸。

描述紮實的原則及其如何應用於PHP的開發。 描述紮實的原則及其如何應用於PHP的開發。 Apr 03, 2025 am 12:04 AM

SOLID原則在PHP開發中的應用包括:1.單一職責原則(SRP):每個類只負責一個功能。 2.開閉原則(OCP):通過擴展而非修改實現變化。 3.里氏替換原則(LSP):子類可替換基類而不影響程序正確性。 4.接口隔離原則(ISP):使用細粒度接口避免依賴不使用的方法。 5.依賴倒置原則(DIP):高低層次模塊都依賴於抽象,通過依賴注入實現。

PHP 8.1中的枚舉(枚舉)是什麼? PHP 8.1中的枚舉(枚舉)是什麼? Apr 03, 2025 am 12:05 AM

PHP8.1中的枚舉功能通過定義命名常量增強了代碼的清晰度和類型安全性。 1)枚舉可以是整數、字符串或對象,提高了代碼可讀性和類型安全性。 2)枚舉基於類,支持面向對象特性,如遍歷和反射。 3)枚舉可用於比較和賦值,確保類型安全。 4)枚舉支持添加方法,實現複雜邏輯。 5)嚴格類型檢查和錯誤處理可避免常見錯誤。 6)枚舉減少魔法值,提升可維護性,但需注意性能優化。

在PHPStorm中如何進行CLI模式的調試? 在PHPStorm中如何進行CLI模式的調試? Apr 01, 2025 pm 02:57 PM

在PHPStorm中如何進行CLI模式的調試?在使用PHPStorm進行開發時,有時我們需要在命令行界面(CLI)模式下調試PHP�...

如何在系統重啟後自動設置unixsocket的權限? 如何在系統重啟後自動設置unixsocket的權限? Mar 31, 2025 pm 11:54 PM

如何在系統重啟後自動設置unixsocket的權限每次系統重啟後,我們都需要執行以下命令來修改unixsocket的權限:sudo...

解釋PHP中的晚期靜態綁定(靜態::)。 解釋PHP中的晚期靜態綁定(靜態::)。 Apr 03, 2025 am 12:04 AM

靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。

See all articles