Buscar

Membuat Photo Gallery dengan PHP

Kali ini saya akan berbagi tentang bagaimana membuat photo gallery sederhana di php.
Sumber belajarnya dari mas stieven. :)
Tampilan sederhana photo gallery yang saya buat adalah seperti di bawah ini.




Form Inputannya :



Form akan error jika ukuran file image yang kita upload terlalu besar


Kita langsung saja ke scriptnya.
Pertama kita buat file dengan nama gallery_config.php
<?
$config['document_title'] = "My Gallery by Anggun Patriana";
$config['root_direktori_image'] = "E:/xampp/htdocs/belajar/gallery_foto/Temp/";
$config['nama_direktori_image'] = "images";
$config['thumb_direktori_image'] = "thumbs";
$config['thumbs_width'] = 150;//pixel
$config['user_database'] = "E:/xampp/htdocs/belajar/gallery_foto/Temp/user.txt";
$config['kolom_tabel'] = 3; //jumlah kolom perhalaman
$config['baris_tabel'] = 3; //jumlah baris perhalaman
?>
Keterangan :
$config['document_title'] untuk Judul halaman HTML anda
$config['root_direktori_image'] Folder untuk database photo dan thumbnail yang akan diupload
$config['nama_direktori_image'] Nama folder untuk menyimpan photo master yang diupload
$config['thumb_direktori_image'] Nama folder untuk menyimpan thumbnail photo
$config['thumbs_width'] Besar thumbnail dalam satuan pixel
$config['user_database'] Database user untuk akses mengupload photo
$config['kolom_tabel'] Banyaknya kolom dalam satu halaman
$config['baris_tabel'] Banyaknya baris dalam satu halaman
Kedua kita buat file dengan nama gallery_function.php dimana isi file tersebut adalah kumpulan fungsi-fungsi yang kita butuhkan dalam membuat program sederhana ini.

<?php
class myuser{
 var $userdb = array();
 function loaduser() {
  global $config;
  if (file_exists($config['user_database'])){
   $fp=fopen($config['user_database'],"r");
   while($fg=fgets($fp,1024)) {
    $fg=trim($fg);
    list($user,$pass)=explode("=",$fg);
    $this->userdb[$user]=$pass;
   }
   fclose($fp);
  }
  else { 
   die("File database {$config['user_database']} tidak ditemukan"); 
  }
 }
 function getuser($usr,$pswd){
  $this -> loaduser();
  while(list($user,$pass)=each($this->userdb)){
   if ($user==$usr and $pass == $pswd) { 
    return true; 
   }
  }
 }
}
function html_header() {
 global $config;
 echo "<html>
   <head>
   <title>{$config['document_title']}</title>
   </head>
   <style>
   body,td,th { font-size:9pt; font-family:Tahoma, Arial, Verdana; }
   </style>
     <body>";
}
function html_footer() {
 echo "</body></html>";
}

function imgThumbs($FILESRC,$FILETHUMBS,$wm=75,$hws=75) {
 if (file_exists($FILESRC)) {
  list($imagewidth,$imageheight)=getimagesize($FILESRC);
  $mw=$imagewidth;$hw=$imageheight;
  if ($imagewidth > $wm) {
   $imageheight=round(($wm/$imagewidth)*$imageheight,0);
   $imagewidth=$wm;
  }
 if ($imageheight >= $hws) {
  $imagewidth=round(($hws/$imageheight)*$imagewidth,0);
  $imageheight=$hws;
 }
 $cc=floor(($hws-$imageheight)/2);$ch=floor(($wm-$imagewidth)/2);
 $img_src=imagecreatetruecolor($imagewidth,$imageheight);
 $red01=imagecolorallocate($img_src,48,0,0);
 $red=imagecolorallocate($img_src,0,0,0);
 $wred= imagecolorallocate($img_src,255,255,0);
 $des_src=imagecreatefromjpeg($FILESRC);
 imagecopyresized($img_src,$des_src,0,0,0,0,$imagewidth,$imageheight,$mw,$hw);
  @imagejpeg($img_src,$FILETHUMBS);
  @imagedestroy($img_src);
 }
}
function viewimages($imgdb,$page) {
 global $config,$allpage;
 $x=0;$l=0;
 $page=($page-1)*($config['kolom_tabel']*$config['baris_tabel']);
 for($i=$page;$i<count($imgdb);$i++) {
 $x++;$l++;
 if ($x==1) { echo "<tr>"; }
 echo "<td align=\"center\">
  <a href=\"imageview.php?master=1&source={$imgdb[$i]}\"
  target=\"_blank\">
  <img src=\"imageview.php?source=".$imgdb[$i]."\"
  alt=\"{$imgdb[$i]}\">
  </a>
  </td>";
 if ($x==$config['kolom_tabel']) { echo "</tr>";$x=0; }
 if ($l==($config['kolom_tabel']*$config['baris_tabel'])) break;
 }
 $allpage=round(count($imgdb)/($config['kolom_tabel']*$config['baris_tabel']))." ";
 $cp=$allpage*($config['kolom_tabel']*$config['baris_tabel']);
 if (count($imgdb)>$cp) { $allpage=$allpage+1; }
}

function readdbimage($page=4) {
global $config;
$r=0;
if(file_exists($config['root_direktori_image'].$config['nama_direktori_image']) and file_exists($config['root_direktori_image'].$config['thumb_direktori_image'])){
 $od=@opendir($config['root_direktori_image'].$config['nama_direktori_image']);
while($rd=@readdir($od)) {
 if ($rd == ".." or $rd == "." or !eregi("\.jpg$",$rd))
  continue;
 $imgdb[$r]=$rd;$r++;
}
 fclose($od);
}
viewimages($imgdb,$page);
}
?>
Ketiga kita buat file dengan nama imageview.php
<?php
include_once("gallery_config.php");
 if ($_GET['master']){
  @readfile($config['root_direktori_image'].$config['nama_direktori_image']."/".$_GET['source']); 
 }
 else{
  @readfile($config['root_direktori_image'].$config['thumb_direktori_image']."/".$_GET['source']); 
 }
?>

Terakhir kita buat file gallery.php untuk tempat form inputan.

<?php
session_start();
$raw = phpversion();
list($v_Upper,$v_Major,$v_Minor) = explode(".",$raw);
if (($v_Upper == 4 and  $v_Major < 1) or $v_Upper < 4) {
	$_FILES = $HTTP_POST_FILES;
	$_ENV = $HTTP_ENV_VARS;
	$_GET = $HTTP_GET_VARS;
	$_POST = $HTTP_POST_VARS;
	$_COOKIE = $HTTP_COOKIE_VARS;
	$_SERVER = $HTTP_SERVER_VARS;
	$_SESSION = $HTTP_SESSION_VARS;
	$_FILES = $HTTP_POST_FILES;
}
$file_name=$HTTP_POST_FILES["file"]["name"];
$file_size=$HTTP_POST_FILES["file"]["size"];
$file_tmp=$HTTP_POST_FILES["file"]["tmp_name"];
$file_type=$HTTP_POST_FILES["file"]["type"];


include_once("gallery_config.php");
include_once("gallery_function.php");

if (is_writeable(!$config['root_direktori_image'])){ 
	die("Folder {$config['root_direktori_image']} harus writeble"); 
}
if(!file_exists($config['root_direktori_image'].$config['nama_direktori_image'])){ 
	@mkdir($config['root_direktori_image'].$config['nama_direktori_image']); 
}
if(!file_exists($config['root_direktori_image'].$config['thumb_direktori_image'])){ 
	@mkdir($config['root_direktori_image'].$config['thumb_direktori_image']); 
}

if ($_GET['mode'] == "upload"){
	if ($_POST['Submit']){
		$user = new myuser;
		//if ($user->getuser($_POST['username'],$_POST['password']) and  $_POST['username'] and  $_POST['password']) {
			if(($file_type == "image/pjpeg" or $file_type == "image/jpg" or	$file_type == "image/jpeg" or $file_type == "image/pjpg") and  $file_size < 256000){
				$data=$config['root_direktori_image'].$config['nama_direktori_image']."/".$file_name;
			@move_uploaded_file($file_tmp,$data);
			imgThumbs($data,$config['root_direktori_image'].$config['thumb_direktori_image']."/$file_name");
			echo "<script>location.replace('?')</script>";
			}
			else{ 
				echo "Error!<br>File type image/pjpeg your file $file_type<br>File size maximum 256 KB your file".round($file_size/1024)."KB"; 
			}
		//}
		
	}
	echo '<form action="" method="post" enctype="multipart/form-data" name="form1">
	<table width="100%" border="0" cellspacing="1" cellpadding="2">
		<tr>
			<td>File Photo</td>
			<td><input type="file" name="file"></td>
		</tr>
		<tr>
			<td>User </td>
			<td><input name="username" type="text"></td>
		</tr>
		<tr>
			<td valign="top">Password</td>
			<td><input name="password" type="password"></td>
		</tr>
		<tr>
			<td> </td>
			<td><input type="submit" name="Submit" value="Submit"><button onClick="location.replace(\''.$_SERVER['PHP_SELF'].'\')">Cancel</button></td>
		</tr>
	</table>';
}
else {
	html_header();
	if (!$_GET['page']){ 
		$_GET['page']=1; 
	};
	echo "";
	echo "<table width=\"100%\"><tr><td width=\"50%\"></td><td
	align=\"right\"><a href=\"?mode=upload\">Upload</a></td></tr></table>";
	echo "<table width=\"100%\">";
	readdbimage($_GET['page']);
	echo "</table>Halaman {$_GET['page']} dari $allpage : ";
	for ($i=1;$i<=$allpage;$i++) { 
		echo "<a href=\"?page=$i\">$i</a>"; 
		}
	html_footer();
}
?>

0 comments:

Posting Komentar