Presentation is loading. Please wait.

Presentation is loading. Please wait.

相本 照片該如何存 一般的做法是將照片以用檔案的方式存在server上,雖然使用方便,但是管理麻煩,也有安全上的問題。在這裡使用資料庫的方式來儲存二進位的大型資料(包括圖片以及文件等),做法與一般的檔案管理稍有不同。

Similar presentations


Presentation on theme: "相本 照片該如何存 一般的做法是將照片以用檔案的方式存在server上,雖然使用方便,但是管理麻煩,也有安全上的問題。在這裡使用資料庫的方式來儲存二進位的大型資料(包括圖片以及文件等),做法與一般的檔案管理稍有不同。"— Presentation transcript:

1 相本 照片該如何存 一般的做法是將照片以用檔案的方式存在server上,雖然使用方便,但是管理麻煩,也有安全上的問題。在這裡使用資料庫的方式來儲存二進位的大型資料(包括圖片以及文件等),做法與一般的檔案管理稍有不同。

2 資料庫規劃 現在我們第二個實例是做校園相本,可以將一些活動相片整理並且上傳,放在WWW上供網友上線瀏灠 ,原則上我們必須要做到二項功能: 1.開放式的瀏灠空間,並且有縮圖以及圖片說明,加上分頁處理 2.有管理介面,可以任意新增、刪除以及修改每項記錄

3 資料庫 例如:建立一個album table,其中資料如下: 欄位資料型態說明 idint相片編號/流水號 namechar(30)相片名稱
detailchar(100)簡單說明 ownerchar(10)攝影者 ctimedatetime上傳日期 bigpicMEDIUMBLOB相片(原圖) smallpicMEDIUMBLOB相片(縮圖) type1char(20)原圖相片檔案型態   縮圖相片檔案型態 註:blob可以儲存大型資料,blob可分為tinyblob,medium blob,longblob分別可存儲存各種不同大小的資料。有關其資料範圍可以參考這裡

4 資料庫設定 MYSQL資料庫設定: $mysql -u root -p
//建立 table album mysql>use dbname; mysql>create table album(id int unsigned not null auto_increment,name char(30),detail char(100),owner char(100),ctime datetime,bigpic mediumblob,smallpic mediumblob,type1 char(20),type2 char(20),primary key(id)); //grant 相關權限給iuser mysql> grant insert,delete,update,select on dbname.album to identifi ed by '1234'; mysql> show columns from album;

5 Global.php 廣域設定檔:global.php
<? $HOST="localhost"; $DB="dbname"; $TABLE1="album"; $USER="iuser"; $PASS="1234"; $PAGE=3; $MAXSIZE= ; ?>

6 新增相片表單 當我們按下「送出」按鈕,add.php必須執行新增一筆資料到資料庫的動作,如以下說明:
if($submit) //按下submit { //連結資料庫 $link=mysql_pconnect($HOST,$USER,$PASS); mysql_select_db($DB,$link); //從上傳暫存檔讀取照片 $fd = fopen($userfile1, "rb"); $block = fread($fd, filesize($userfile1)); fclose($fd); //從上傳暫存檔讀取照片縮圖 $fd = fopen($userfile2, "rb"); $block2 = fread($fd, filesize($userfile2)); fclose($fd); //存入資料庫前處理跳脫字元 $block = addslashes($block); $block2 = addslashes($block2); //指定圖檔html型態 $type1=$userfile1_type; $type2=$userfile2_type; //INSERT SQL 指令,並指執行之 $str="INSERT INTO $TABLE1 (name,detail,owner,ctime,bigpic,smallpic,type1,type2) VALUES('$name','$detail','$owner',now(),'$block','$block2','$type1','$type2')"; mysql_query($str,$link); //回應新增成功 echo "<h1>您的相片資料已經新增了</h1>"; echo "<a href=viewpic.php>觀看相片</a>"; exit; } ?> 新增相片表單

7 如何在WEB上觀看資料庫裡的相片 因為相片己儲存在WEB上,所以我們不能用傳統的指定圖檔的方式來觀看圖片,如<IMG SRC="pic.gif">,我們必須用php程式來產生圖形,其php語法為://...讀取資料庫欄位.... Header( "Content-type: $type"); //$type為檔案型態,例如 html/gif 為gif檔 echo $pic_data; 我們利用相片的id來產生圖片,例如 getdata.php?id=2&big=yes 來取得編號為2的原相片,而 getdata.php?id=2 則產生編號為2為縮圖,我們可以:<IMG SRC="getdata.php?id=2&big=yes">來做圖片連結

8 如何在WEB上觀看資料庫裡的相片 <? if($id) {
$HOST="localhost";$USER="iuser";$PASS="1234";$DB="airfire";$TABLE1="album"; //include("global.php"); mysql_pconnect($HOST,$USER,$PASS); @mysql_select_db($DB); if($big=="yes") $query = "select bigpic,type1 from $TABLE1 where id=$id"; else $query = "select smallpic,type2 from $TABLE1 where id=$id"; $result Header( "Content-type: $type"); echo $data; }; ?>

9 分頁瀏覽相片縮圖 接著,我們要做一個瀏覽圖片縮圖的介面,每頁設定為三張,可以分頁顯示,這支程式只要稍微將前面的留言板程式修改一下就可以用了:

10 <? include("global.php"); //database連結 $link=mysql_pconnect($HOST,$USER,$PASS); mysql_select_db($DB); if(!isset($p) || $p <=0 ) $p=1; //若未設頁碼,預設為第1頁 //計算總筆數 $str="select count(*) from $TABLE1"; $result=mysql_query($str); list($total)=mysql_fetch_row($result); //計算總頁數 $totalpage=ceil($total/$PAGE); //計算目前頁數的起始植 $begin=($p-1)*$PAGE; //查詢本頁的留言資料 $str="select id,name,owner,detail,ctime from $TABLE1 order by ctime DESC LIMIT $begin,$PAGE"; $result=mysql_query($str); ?> <? while(list($id,$name,$owner,$detail,$ctime)=mysql_fetch_row($result)){ ?> <TABLE border=1 cellpadding="1" cellspacing="0" bordercolordark="#ffffff" bordercolorlight="#cccccc" class="TEXT11"> <TR> <TD width="134"> <? echo "<img src=getdata.php?id=".$id.">";?> </TD> <TD width="534" bgcolor="#E8E8E8"> <? echo "照片名稱:".$name."<br>"; echo "攝影人員:".$owner."<br>"; echo "上傳日期:".$ctime."<br>"; echo "照片說明:".$detail."<br>"; echo "<a href=getdata.php?id=".$id."&big=yes>"."看大照片</a>"; ?> </TD> </TR> </TABLE> <?} ?> 分頁瀏覽相片縮圖


Download ppt "相本 照片該如何存 一般的做法是將照片以用檔案的方式存在server上,雖然使用方便,但是管理麻煩,也有安全上的問題。在這裡使用資料庫的方式來儲存二進位的大型資料(包括圖片以及文件等),做法與一般的檔案管理稍有不同。"

Similar presentations


Ads by Google