PHP Photo Gallery (PPG)

Greetings to all, first of all I apologize if I can only write tips & tricks again now after some time ago my busy assignments took up all my time. Currently I am still busy with assignments but I have a longing to give a few tips for creating a Photo gallery using PHP.


Creating a Photo Gallery with PHP

OK, let's get started. What you need to prepare;

  1. PC (Personal Computer); what are the specifications?
  2. System Operation; up to you 
  3. Web Server; IIS/PWS, apache or whatever 
  4. PHP Zend Engine 4.2+ or 5.0+ 
  5. Text Editor.

Step 1

Create a file using your favorite text editor, then name the file gallery_config.php and the contents of the file are as follows:

<?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

?>

Information:

  • $config['document_title'] for your HTML page Title
  • $config['root_image_directory'] Folder for the photo and thumbnail database to be uploaded.
  • $config['image_directory_name'] Folder name to store uploaded master photos.
  • $config['thumb_image_directory'] Folder name for storing photo thumbnails
  • $config['thumbs_width'] Thumbnail size in pixels
  • $config['user_database'] User database for access to upload photos
  • $config['column_table'] Number of columns in one page
  • $config['table_rows'] Number of rows in one page

Step 2 

Create a file named 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);
}

?>

Step 3

Create a file named 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']); }
?>

Step 4

Create a file named 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>&nbsp;</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();
}
?>

Source:

IlmuKomputer.Com
Stieven R. Kalengkian  stieven@kawanua.web.id
http://stieven.kawanua.web.id


Post a Comment

Previous Next

نموذج الاتصال