10.2、使用GD繪圖(繪制基本幾何圖形、填充圖形、生成驗證碼、水印等)
- 優酷線路1
- 騰迅線路2
- 西瓜線路3
- 本地線路4
- 奇藝線路5
- 圖文解說
10.2.3在圖像中添加文字
10.2.5導入外部圖像
10.2.8設計折線圖
10.2.1 繪制基本幾何圖形
<?php
$im = imagecreate(300,200); //創建一個畫布
$bg = imagecolorallocate($im, 255, 255, 255); //設置背景色為白色
$color = imagecolorallocate($im, 0, 0, 0); //設置前景色為白色
imageline($im, 0, 0, 300, 200, $color); //繪制一條直線
imagegif($im); //輸出圖像
?>
<?php
// 創建一個 200X200 的圖像
$img = imagecreate(200,200);
// 分配顏色
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
// 畫一個黑色的圓
imagearc($img, 100, 100, 150, 150, 0, 360, $black);
// 將圖像輸出到瀏覽器
header("Content-type: image/png");
imagegif($img); //輸出圖像
// 釋放內存
imagedestroy($img);
?>
<?php
header("Content-type: image/png"); //將圖像輸出到瀏覽器
$img = imagecreate(560, 200); //創建一個 560×200 的圖像
$bg = imagecolorallocate($img, 240, 240, 230); //設置圖像背景色
$color = imagecolorallocate($img, 255, 0, 0); //設置繪制圖像的顏色為紅色
imageline($img, 20, 20, 150, 180, $color); //繪制一條直線
imagearc($img, 250, 100, 150, 150, 0, 360, $color); //繪制一個圓
imagerectangle($img, 350, 20, 500, 170, $color); //繪制一個正方形
imagegif($img); //以gif格式輸出圖形
imagedestroy($img); //釋放資源
?>
10.2.2 填充圖形
<?php
$im = imagecreatetruecolor(100, 100);
// ½«±3Ύ°ΙθΞΊμΙ«
$red = imagecolorallocate($im, 255, 0, 0);
imagefill($im, 0, 0, $red);
header('Content-type: image/gif');
imagegif($im);
imagedestroy($im);
?>
<?php
// 創建圖像
$image = imagecreatetruecolor(400, 300);
$bg = imagecolorallocate($image, 255, 255, 255);
// 分配一些顏色
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$red = imagecolorallocate($image, 0xFF, 0x00, 0x00);
// 繪制圖形
imagefilledarc($image, 200, 150, 300, 200, 0, 45, $navy, IMG_ARC_PIE);
imagefilledarc($image, 200, 150, 300, 200, 45, 75 , $gray, IMG_ARC_PIE);
imagefilledarc($image, 200, 150, 300, 200, 75, 360 , $red, IMG_ARC_PIE);
// 輸出圖像
header('Content-type: image/gif');
imagegif($image);
imagedestroy($image);
?>
<?php
// 建立多邊形各頂點坐標的數組
$values = array(
40, 50, // Point 1 (x, y)
20, 240, // Point 2 (x, y)
60, 60, // Point 3 (x, y)
240, 20, // Point 4 (x, y)
50, 40, // Point 5 (x, y)
10, 10 // Point 6 (x, y)
);
// 創建圖像
$image = imagecreatetruecolor(250, 250);
// 設定顏色
$bg = imagecolorallocate($image, 200, 200, 200);
$blue = imagecolorallocate($image, 0, 0, 255);
// 畫一個多邊形
imagefilledpolygon($image, $values, 6, $blue);
// 輸出圖像
imagegif($image);
imagedestroy($image);
?>
10.2.3 在圖像中添加文字
<?php
// 建立一幅 100X30 的圖像
$im = imagecreate(100, 30);
// 白色背景和藍色文本
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// 把字符串寫在圖像左上角
imagestring($im, 5, 0, 0, "Hello world!", $textcolor);
// 輸出圖像
header("Content-type:image/png");
imagegif($im);
?>
<?php
header("content-type:image/jpeg"); //定義輸出為圖像類型
// 建立一幅500X200 的圖像
$im = imagecreate(500, 200);
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
$fnt="c:/windows/fonts/simhei.ttf"; //定義字體
$motto=iconv("gb2312","utf-8","Hello world!"); //定義輸出字體串
imagettftext($im,20,0,20,40,$textcolor,$fnt,$motto); //寫TTF文字到圖中
imagejpeg($im); //建立JPEG圖形
imagedestroy($im); //結束圖形,釋放內存空間
?>
10.2.4 生成驗證碼
checks.php代碼
<?php
if(!isset($_SESSION)){
session_start();
}
header("content-type:image/png"); //設置創建圖像的格式
$image_width=70; //設置圖像寬度
$image_height=18; //設置圖像高度
srand((double)microtime()*100000); //設置隨機數的種子
$new_number = "";
for($i=0;$i<4;$i++){ //循環輸出一個4位的隨機數
$new_number.=dechex(rand(0,15));
}
$_SESSION['check_checks']=$new_number; //將獲取的隨機數驗證碼寫入到SESSION變量中
$num_image=imagecreate($image_width,$image_height); //創建一個畫布
imagecolorallocate($num_image,255,255,255); //設置畫布的顏色
for($i=0;$i<strlen($_SESSION['check_checks']);$i++){ //循環讀取SESSION變量中的驗證碼
$font=mt_rand(3,5); //設置隨機的字體
$x=mt_rand(1,8)+$image_width*$i/4; //設置隨機字符所在位置的X坐標
$y=mt_rand(1,$image_height/4); //設置隨機字符所在位置的Y坐標
$color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)); //設置字符的顏色
imagestring($num_image,$font,$x,$y,$_SESSION['check_checks'][$i],$color); //水平輸出字符
}
imagepng($num_image); //生成PNG格式的圖像
imagedestroy($num_image); //釋放圖像資源
?>
index.php代碼
<?php
if(!isset($_SESSION)){
session_start();
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
if(!isset($_SESSION)){
session_start();
}
if(!empty($_POST["submit"]) && $_POST["submit"]!=""){
$checks=$_POST["checks"];
if($checks==""){
echo "<script> alert('驗證碼不能為空');window.location.href='index.php';</script>";
}
if($checks==$_SESSION['check_checks']){
echo "<h2>驗證碼輸入正確</h2>";
}else{
echo "<script> alert('您輸入的驗證碼不正確!');window.location.href='index.php';</script>";
}
}else{
?>
<h2>用戶登錄</h2>
<form name="form" method="post" action="">
<p>用戶名
<input type="text" name="txt_user" id="txt_user" size="20">
</p>
<p>密碼
<input type="password" name="txt_pwd" id="txt_pwd" size="20">
</p>
<p>驗證碼
<input name="checks" size="6" >
<img src="checks.php" width="70" height="18" border="0"></p>
<p>
<input type="submit" name="submit" value="登 錄">
</p>
</form>
<?php
}
?>
</body>
</html>
10.2.5 導入外部圖像
<?php
function LoadGif($imgname){
$im = @imagecreatefromgif($imgname);
if(!$im){
$im = imagecreatetruecolor (150, 30);
$bgc = imagecolorallocate ($im, 255, 255, 255);
$tc = imagecolorallocate ($im, 0, 0, 0);
imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);
imagestring ($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
return $im;
}
header('Content-Type: image/gif');
$img = LoadGif('girl.gif');
imagegif($img);
imagedestroy($img);
?>
10.2.6 為圖片添加文字水印
AddWaterPress.php代碼
<?php
class AddWaterPress{ //定義類文件
function getExtendsName($fileName){ //獲取上傳圖片后綴
return strtolower(strstr($fileName, ".")); //返回圖片后綴
}
function getImageRes($extendsName, $imageUrl){ //根據上傳圖片的后綴,和上傳文件的路徑新建圖像
switch($extendsName){ //根據上傳圖片的后綴進行判斷
case '.gif': //如果后綴為gif
$img =imagecreatefromgif($imageUrl); //則根據路徑創建一個GIF圖像
break;
case '.jpg': //如果后綴為jpg
$img =imagecreatefromjpeg($imageUrl); //則根據路徑創建一個JPG圖像
break;
case '.png':
$img =imagecreatefrompng($imageUrl);
break;
}
return $img; //返回創建圖像的標識
}
function outputImage($img, $extendsName, $imageUrl){ //根據圖像標識、圖片后綴和路徑輸出圖像
switch($extendsName){ //判斷圖像后綴
case '.gif': //如果后綴為gif
imagegif($img, $imageUrl); //則輸出img圖像
break;
case '.jpg':
imagejpeg($img, $imageUrl);
break;
case '.png':
imagepng($img, $imageUrl);
break;
}
}
function add($imageUrl, $watherImageUrl){ //定義添加方法
$img = @$this->getImageRes($this->getExtendsName($imageUrl), $imageUrl); //獲取被操作的圖像標識
$textcolor=imagecolorallocate($img,2550,0,0); //設置字體顏色為紅色,值為RGB顏色值
//imagestring($img, 20, 30, 100, "$watherImageUrl", $textcolor);
//$font=dirname(__FILE__) ."/font/SIMYOU.TTF"; //定義字體
$font= __DIR__ ."/font/SIMYOU.TTF"; //定義字體
imagettftext($img,24,0,20,30,$textcolor,$font,$watherImageUrl); //寫TTF文字到圖中
//根據圖像標識符、后綴和路徑,執行outputImage方法,輸出圖像
$this->outputImage($img, $this->getExtendsName($imageUrl), $imageUrl);
imagedestroy($img); //銷毀圖像
}
}
index.php代碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h2>圖片上傳</h2>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']?>" enctype="multipart/form-data">
選擇圖片:
<input type="file" name="file" class="input">
<input type="hidden" name="flag" value="1">
<input type="submit" name="imageField" value="上 傳">
</form>
</body>
</html>
<?php
if (!empty($_FILES["file"]) && $_FILES["file"]["name"]!="" && $_POST['flag']=='1'){ //判斷提交內容是否為空
$type = strstr($_FILES["file"]["name"], '.'); //獲取上傳圖片后綴
if($_FILES["file"]["name"]==''){ //判斷上傳圖片名稱是否為空
echo "<script>alert('圖片不能為空!');</script>";
exit();
}else if(!($type == '.jpg') && !($type == '.png') && !($type == '.gif')){ //判斷上傳圖片格式是否正確
echo "<script>alert('圖片格式不正確!');</script>";
exit();
}
function getUpfileName($fileName){ //定義上傳文件在服務器中存儲的名稱
return 'waterpress'.strstr($fileName, "."); //使用固定名稱(也可以通過時間戳、隨機數定義)
}
$saveDir = "upfiles/" . getUpfileName($_FILES["file"]["name"]); //定義上傳文件存儲路徑
if(move_uploaded_file($_FILES["file"]["tmp_name"], $saveDir)){ //執行文件上傳操作
require_once 'AddWaterPress.php'; //包含添加水印操作的文件
$addWaterPress = new AddWaterPress(); //類的實例化
$addWaterPress->add($saveDir, "www.mysite.com"); //執行添加方法,傳遞參數,指定水印文字
echo "<script>alert('圖片添加成功');</script>";
}
}
?>
10.2.7 為圖片添加圖像水印
AddWaterPress.php代碼
<?php
class AddWaterPress{ //定義類文件
function getExtendsName($fileName){ //獲取上傳圖片后綴
return strtolower(strstr($fileName, ".")); //返回圖片后綴
}
function getImageRes($extendsName, $imageUrl){ //根據上傳圖片的后綴,和上傳文件的路徑新建圖像
switch($extendsName){ //根據上傳圖片的后綴進行判斷
case '.gif': //如果后綴為gif
$img =imagecreatefromgif($imageUrl); //則根據路徑創建一個GIF圖像
break;
case '.jpg': //如果后綴為jpg
$img =imagecreatefromjpeg($imageUrl); //則根據路徑創建一個JPG圖像
break;
case '.png':
$img =imagecreatefrompng($imageUrl);
break;
}
return $img; //返回創建圖像的標識
}
function add($imageUrl, $watherImageUrl, $x, $y){ //定義添加方法
$img = @$this->getImageRes($this->getExtendsName($imageUrl), $imageUrl); //獲取被添加的圖像標識
$img1 = @$this->getImageRes($this->getExtendsName($watherImageUrl), $watherImageUrl); //獲取指定的水印圖片的圖像標識
$size = getimagesize($imageUrl); //獲取圖像大小
$size1 = getimagesize($watherImageUrl); //獲取水印圖片的大小
if($x==null && $y==null){ //判斷參數是否為空
$x1 = ($size[0]-$size1[0])/2; //根據圖像大小數組中返回的值,計算圖像的橫坐標
$y1 = ($size[1]-$size1[1])/2; //根據圖像大小數組中返回的值,計算圖像的縱坐標
}else{
$x1 = $x; //如果不為空,則直接使用坐標數據
$y1 = $y; //如果不為空,則直接使用坐標數據
}
imagecopy($img, $img1, $x1, $y1, 0, 0, $size1[0], $size1[1]); //將img1的一部分拷貝到img的指定位置
//根據圖像標識符、后綴和路徑,執行outputImage方法,輸出圖像
$this->outputImage($img, $this->getExtendsName($imageUrl), $imageUrl);
imagedestroy($img1); //銷毀圖像
imagedestroy($img); //銷毀圖像
}
function outputImage($img, $extendsName, $imageUrl){ //根據圖像標識、圖片后綴和路徑輸出圖像
switch($extendsName){ //判斷圖像后綴
case '.gif': //如果后綴為gif
imagegif($img, $imageUrl); //則輸出img圖像
break;
case '.jpg':
imagejpeg($img, $imageUrl);
break;
case '.png':
imagepng($img, $imageUrl);
break;
}
}
}
index.php代碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h2>圖片上傳</h2>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']?>" enctype="multipart/form-data">
選擇圖片:
<input type="file" name="file" class="input">
<input type="hidden" name="flag" value="1">
<input type="submit" name="imageField" value="上 傳">
</form>
</body>
</html>
<?php
if ( !empty($_FILES["file"]) && $_FILES["file"]["name"]!="" && $_POST['flag']=='1'){ //判斷提交內容是否為空
$type = strstr($_FILES["file"]["name"], '.'); //獲取上傳圖片后綴
if($_FILES["file"]["name"]==''){ //判斷上傳圖片名稱是否為空
echo "<script>alert('圖片".$i."不能為空!');</script>";
exit();
}else if(!($type == '.jpg') && !($type == '.png') && !($type == '.gif')){ //判斷上傳圖片格式是否正確
echo "<script>alert('圖片".$i."格式不正確!');</script>";
exit();
}
function getUpfileName($fileName){ //定義上傳文件在服務器中存儲的名稱
return "waterpress".strstr($fileName, "."); //使用時間戳、隨機數定義
}
$saveDir = "upfiles/" . getUpfileName($_FILES["file"]["name"]); //定義上傳文件存儲路徑
if(move_uploaded_file($_FILES["file"]["tmp_name"], $saveDir)){ //執行文件上傳操作
require_once 'AddWaterPress.php'; //包含添加水印操作的文件
$addWaterPress = new AddWaterPress(); //類的實例化
$addWaterPress->add($saveDir, "wateriImage/baidu.png" ,null, null); //執行添加方法,傳遞參數,指定水印文字
echo "<script>alert('圖片添加成功');</script>";
}
}
?>
10.2.8 設計折線圖
index.php代碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script>
function chkinput(form){
if(form.T1.value=='' || form.T2.value=='' || form.T3.value=='' || form.T4.value=='' || form.T5.value=='' || form.T6.value=='' || form.T7.value=='' || form.T8.value=='' || form.T9.value=='' || form.T10.value=='' || form.T11.value=='' || form.T12.value==''){
alert("表單中數值不能為空!");
return false;
}
return true;
}
</script>
<style type="text/css">
form p{
float:left;
width:40%;
margin:4px;
}
</style>
</head>
<body>
<h2>月度流量統計</h2>
<form method="post" name="myform" action="img.php" onsubmit="return chkinput(this)">
<p>1月:<input type="text" name="T1" size="12"></p>
<p>2月:<input name="T2" type="text" id="T2" size="12"></p>
<p>3月:<input name="T3" type="text" id="T3" size="12"></p>
<p>4月:<input name="T4" type="text" id="T4" size="12"></p>
<p>5月:<input name="T5" type="text" id="T5" size="12"></p>
<p>6月:<input name="T6" type="text" id="T6" size="12"></p>
<p>7月:<input name="T7" type="text" id="T7" size="12"></p>
<p>8月:<input name="T8" type="text" id="T8" size="12"></p>
<p>9月:<input name="T9" type="text" id="T9" size="12"></p>
<p>10月:<input name="T10" type="text" id="T10" size="12"></p>
<p>11月:<input name="T11" type="text" id="T11" size="12"></p>
<p>12月:<input name="T12" type="text" id="T12" size="12"></p>
<p><input type="hidden" name="flag" value="1"><input type="submit" value="提交" name="submit"></p>
</form>
</body>
</html>
img.php代碼
<?php
$data = array ($_POST ["T1"], $_POST ["T2"], $_POST ["T3"], $_POST ["T4"], $_POST ["T5"], $_POST ["T6"], $_POST ["T7"], $_POST ["T8"], $_POST ["T9"], $_POST ["T10"], $_POST ["T11"], $_POST ["T12"] );
$month = array ("Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec" );
$max = 0;
for($i = 0; $i < 12; $i ++) {
$max = $max + $data [$i]; //所有網站訪問量的累加和
}
$im = imagecreate ( 550, 300 ); //創建畫布
$green = imagecolorallocate ( $im, 214, 235, 214 ); //設置顏色值
$black = imagecolorallocate ( $im, 0, 0, 0 );
$red = imagecolorallocate ( $im, 255, 0, 0 );
$blue = imagecolorallocate ( $im, 0, 0, 255 );
imageline ( $im, 30, 230, 520, 230, $blue ); //設置X軸橫坐標
imageline ( $im, 30, 5, 30, 230, $blue ); //設置Y軸縱坐標
imagestring ( $im, 3, 520, 222, "X", $black ); //輸出字符X
imagestring ( $im, 3, 16, 1, "Y", $black ); //輸出字符Y
$l = 190;
$k1 = 30;
$k2 = 510;
for($j = 0; $j < 12; $j ++) {
imageline ( $im, $k1, $l, $k2, $l, $black ); //設置X軸網格線橫坐標
$l = $l - 40;
}
$f = 70;
$z1 = 20;
$z2 = 228;
for($j = 0; $j < 12; $j ++) {
imageline ( $im, $f, $z1, $f, $z2, $black ); //設置Y軸網格線縱坐標
$f = $f + 40;
}
//輸出Y軸坐標值
$l = 185;
for($j = 1; $j < 6; $j ++) {
imagestring ( $im, 2, 2, $l, 20 * $j . "%", $red );
$l = $l - 40;
}
$x = 20;
$y = 230;
for($i = 1; $i < 12; $i ++) {
$y_lt = $y - (($data [$i - 1] / $max) * 200); //設置網站訪問量的縱坐標值
$y_ht = $y - (($data [$i] / $max) * 200); //獲取每月網站訪問量數的縱坐標值
imageline ( $im, $x * ($i * 2 - 1) + 30, $y_lt, $x * (($i + 1) * 2 - 1) + 30, $y_ht, $red ); //繪制網站訪問量折線圖
}
for($i = 1; $i < 13; $i ++) {
$r1 = round ( (($data [$i - 1]) / $max) * 100, 2 );
imagestring ( $im, 2, $x * ($i - 1) * 2 + 40, $y + 11, $month [$i - 1], $black ); //輸出月份的值
imagestring ( $im, 2, $x * ($i - 1) * 2 + 36, $y + 25, $r1 . "%", $red ); //輸出網站訪問量的百分比
}
imagepng ( $im );
imagedestroy ( $im ); //釋放圖像資源
?>
相關教程分類
CSS樣式與DIV布局視頻教程 Ecshop仿站入門到精通視頻教程 shopex商城管理系統使用視頻教程 易企cms管理系統使用操作視頻教程 WP百科視頻教程(初中高級全套) 夫唯HTML基礎入門視頻教程19集 Ecshop精通二次開發個性化定制商城 齊永東Ecshop經典購物二開教程 snagit屏幕抓圖工具視頻教程 wordpress新手入門到插件制作 從域名購買到網站搭建全套流程 CSS層疊樣式表視頻教程 Ecshop傳智播客二次開發項目教程 織夢CMS后臺使用操作視頻教程 wordpress建站系統后臺使用教程 帝國CMS仿站入門到精通視頻教程 織夢CMS仿站入門到精通視頻教程 曹鵬CSS新手視頻教程 曹鵬FireWorks工具演示教程 HTML語言基礎知識視頻教程 明日學院零基礎學PHP視頻教程 PHP從零基礎到項目實戰 尚硅谷HTML+CSS視頻教程
歡迎來吐槽
掃碼關注微信公眾號獲取播放密碼

學員評分
- 教程難度:★★★☆☆
- 視頻清晰度:
- 視頻格式:mp4
- 界面語言:簡體中文
- 學習類型:免費觀看
- 軟件版本:共享軟件
- 更新時間:2019-12-06 14:56
熱門教程
- 3.3、PHP變量的使用/取消和常量的聲明/使用/定義)
- 16.6、獲取SQL錯誤信息(使用默認、警告、異常模式)
- 第22章、PHP錯誤和異常處理 視頻講解:25分鐘
- 8.1、設置系統時區
- 9.1、認識PHP會話機制(為什么要使用會話、PHP會話的方式)
- 20.2 XML請求時限、FormData數據對象、上傳文件、跨域訪問
- 2.2、手動安裝和配置PHP運行環境
- 10.2、使用GD繪圖(繪制基本幾何圖形、填充圖形、生成驗證碼、水印等)
- 18.3 PHP與JavaScript相互傳值
- 第18章、PHP與javascript技術 視頻講解:10分鐘
- 14.1、安裝和配置phpMyAdmin
- 15.5、使用預處理語句處理SELECT查詢結果