Salam baku dapa samua, pertama mohon maaf kalau baru saat ini saya bisa menulis tips & trik kembali setelah beberapa waktu yang lalu kesibukan tugas menyita semua waktu saya. Saat ini masih dalam kesibukan tugas namun saya memiliki kerinduan untuk memberikan sedikit tips untuk membuat Photo gallery menggunakan PHP.

Membuat Photo Gallery dengan PHP
OK, mari kita mulai. Yang perlu disiapkan;
- PC (Personal Computer); spesifikasi apa saja
- Operasi Sistem; terserah anda
- Web Server; IIS/PWS, apache atau apa saja
- PHP Zend Engine 4.2+ atau 5.0+
- Text Editor.
Langkah 1
Buat file dengan menggunakan text editor favorit anda, kemudian namakan file gallery_config.php Dan isi file adalah sebagai berikut:
<?php
$config['document_title'] = "My Gallery by Stieven R. Kalengkian";
//Gunakan format UNIX direktori dan harus dalam mode writeable (777)
//hal ini sangat pengaruh untuk sistem operasi UNIX/LINUX
$config['root_direktori_image'] = "C:/Temp/";
$config['nama_direktori_image'] = "images";
$config['thumb_direktori_image'] = "thumbs";
$config['thumbs_width'] = 150;//pixel
//harus dalam mode readable (dapat diakases)
$config['user_database'] = "C:/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
Langkah 2
Buat file dengan nama gallery_function.php
<?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,$imagehe ight,$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['b aris_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_i mage']) and
file_exists($config['root_direktori_image'].$config['thumb_direktori_i mage'])) {
$od=@opendir($config['root_direktori_image'].$config['nama_dire ktori_image']); while($rd=@readdir($od)) {
if ($rd == ".." or $rd == "." or !eregi("\.jpg$",$rd)) continue;
$imgdb[$r]=$rd;$r++;
}
fclose($od);
}
viewimages($imgdb,$page);
}
?>
Langkah 3
Buat file dengan nama imageview.php
<?php
include_once("config.php"); if ($_GET['master']) {
@readfile($config['root_direktori_image'].$config['nama_direkto ri_image']."/".$_GET['source']); }
else {
@readfile($config['root_direktori_image'].$config['thumb_direkt ori_image']."/".$_GET['source']); }
?>
Langkah 4
Buat file dengan nama gallery.php
<?php
/*
* Gallery Photo versi 1.0
* Script Source by Stieven R. Kalengkian
* http://stieven.kawanua.web.id/
* */
session_start(); $raw = phpversion();
list($v_Upper,$v_Major,$v_Minor) = explode(".",$raw);
if (($v_Upper == 4 && $v_Major < 1) || $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_imag e']); }
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_i mage']."/".$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"; }
}
else { echo "Invalid Username or Password"; }
}
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</butto n></td> </tr>
</table>
</form>
';
} else {
html_header(); //echo $_GET['page'];
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();
}
?>
Sumber:
IlmuKomputer.Com
Stieven R. Kalengkian stieven@kawanua.web.id
http://stieven.kawanua.web.id