程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php 圖像函數大舉例(非原創)

php 圖像函數大舉例(非原創)

編輯:關於PHP編程

如下方式是一種方法:
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])
======================================================
例一:輸出藍色背景和交叉白線的圖形
<?php
$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
例二: 陰陽圖
<?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,108);//藍色
$color_red=imagecolorallocate($image,151,0,4);//紅色
$color_my=imagecolorallocate($image,192,192,255);//背景
$color_temp=imagecolorallocate($image,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);
imagefilledellipse ($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
<?php
$width=400;
$height=400;
$image = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
$red = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);
imagefill($image,0,0,$white);
// make the 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);
imagedestroy($image);
/*
//發送對象至文件
$filename="ex1.png";
imagepng($image,$filename);
*/
?>
輸出:
演示:http://www.phzzy.org/temp/5do8/ex3.php

例四:簡單的驗證碼
PHP創建驗證碼非常容易,容易的要死,簡單的思路是這樣的:
隨機種子生成,提取隨機字符,相連打印到圖形,輸出.,為了防止色盲,可以隨機提取顏色,也可以自定義顏色,下面看看:
<?php
session_start();
$width=65;
$height=20;
$sourcestrings="0123456789aqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
$image=imagecreate($width,$height);
$colorarrs=array(
imagecolorallocate($image,255,255,255),//white
imagecolorallocate($image,0 ,0 , 0)//black
);
unset($sessionval);
imagesetthickness($image,3);
//隨機得到字符串個數
$strsize=rand(3,5);
imagefill($image,0,0,$colorarrs[0]);
//一個個的寫字符串到圖片
for($i=0;$i<$strsize;$i++){
$i_temp=rand(1,62);
$sessionval .=$sourcestrings[$i_temp];
$fontcolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
$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<$width /$height *2;$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,92,0,12),
imagecolorallocate($image,0,0,128),
imagecolorallocate($image,233,10,216)
);
然後把23行變為(17行):
$fontcolor=$colorarrs[rand(1,count($colorarrs)-1)];
輸出:
在線演示:http://www.phzzy.org/temp/5do8/ex5_login.php
例五:大點的比較cool的驗證碼
PS 的圖像還是比較小的,有時候為了某些原因(個人站點為了玩cool,just我,商業站點玩風格,吸引用戶,just google,後話),驗證碼不是局限於十幾個px的限制,有時候完全可以整個2百多,沒啥問題,這時候,一種方案是把前面生成的小圖強制大點,可以不? 可以,但是,看起來不夠光滑,這是事實,明顯,寬帶不再是最重要的問題,不說其他的,下面演示幾個比較好看的生成方式:
<?php
session_start();
$width=600;
$height=100;
if($height < $width /6)
$height=$width / 4;
$sourcestrings="0123456789aqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
//創建對象
$image=imagecreate($width,$height);
$white=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$white);
//加載字體庫
$fonts= dirname(__FILE__);
putenv('"gdfontpath=".$fonts=.""');
$fontname='arial';
$font_size=floor($height / 2);
//得到字符串
unset($sessionval);
$strsize=rand(5,8);
for($i=0;$i<$strsize;$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);
$fontcolor_a=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
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<$width * $height / 100;$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($width,$height);
$white=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$white);
然後加載您要驗證碼的字體:
$fonts= dirname(__FILE__);//返回當前根目錄,字體文件復制到這裡,字體文件是*.ttf文件
putenv('"gdfontpath=".$fonts=.""');
$fontname='arial';
定義字符的高度,這裡我設置成高度的一半:
$font_size=floor($height / 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);
隨機生成顏色,
$fontcolor_a=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
寫入到圖像;
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<$width * $height / 100;$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);設置字體高度$font_size=floor($height / 2);字體顏色$fontcolor_a和隨機數的個數:$strsize=rand(5,8);

例六:給圖片打上水印,生成縮列圖
傳統的ASP頁子打水印和生成縮列圖都是比較繁瑣的,一般使用到的是其他組件什麼的,但是,PHP可以輕松的干這些事情,正如您預料,不到30行的程序搞定這一切,請看這個源程序:
<?php
$source="my.jpg";
$zoom=0.5;
$str='我是帥哥,你是MM麼?';
$image=imagecreatefromjpeg($source);
$width=imagesx($image);
$height=imagesy($image);
$color_red=imagecolorallocate($image,111,0,0);//紅色
$font=dirname(__FILE__). "//simsun.ttc";
$str=iconv('GB2312','UTF-8',$str);
$fontsize=30;
$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);
設置水印字體,因為我們用的是中文,必須導入中文字體庫,否則寫不上或亂碼,然後必須轉換字符串編碼
$font=dirname(__FILE__). "//simsun.ttc";
$str=iconv('GB2312','UTF-8',$str);
設置開始點,字體大小,視角:,寫上字符串:
$fontsize=30;
$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_temp,$width,$height);
生成小圖片,清除對象:
imagedestroy($image);
$file_zoomname="my_zoom_jpeg.jpg";
imagejpeg($img,$file_zoomname);
imagedestroy($img);
生成縮洌圖,水印大體核心技術就這麼點.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved