How I created the Photo Gallery

In the last post I talked about creating a simple photo gallery. It was supposed to be a simple and easy to update photo gallery without any community features and commenting system … just reinventing the wheels.

I was using php script for the code and here is the "index.php" file. All it does is display the content of the folder "images". I have all the photo files in separate folders in the image folder. Click on the name of the folder opens up the gallery of images located in the folder.

<?

$path = "images/";

$dir_handle = @opendir($path) or die("Unable to open $path");

echo "Listing of galleries $path<br/>";

while ($file = readdir($dir_handle))
{
   if($file!="." && $file!="..")
      echo "<a href=’$path$file’>$file</a><br/>";
}

closedir($dir_handle);

?>

That was easy. But the hardest part was to prepare the code to display the images located in the folder when user opens-up the folder. I wondered around — talked about it in the previous post — and found this simple script to do the job effectively. Please visit the author’s page for the file with comments and guides to know what it does and how it is dong. All it does is create thumbnails of photos and place them in a sub-folder and create a gallery page automatically.

<?php

class thumbnail {

    public $Thumbsize;       
    public $Framewidth;
    public $Framecolor;   
    public $Backgroundcolor;   
    public $Shadow;       
    public $Chmodlevel;
    public $Thumblocation;
    public $Thumbprefix;
    private $image;   
    private $im;
    private $thumb;
    private $size;
    private $shadow_offset;
    private $thumbx;
  private $thumby;

  public function __construct() {
        $this->Thumbsize                                = 160;       
        $this->Framewidth                                = 0;   
        $this->Shadow                                        = false;       
        $this->Backgroundcolor                    = ‘#FFFFFF’;
        $this->Framecolor                                = ‘#FFFFFF’;
        $this->Chmodlevel                                = ”;
        $this->Thumblocation                        = ‘thumbs/’;
        $this->Thumbprefix                            = ‘thumb_’;
    }

    public function __destruct() {
        if(is_resource($this->im)) imagedestroy($this->im);
        if(is_resource($this->thumb)) imagedestroy($this->thumb);
    }

    public function Createthumb($filename) {

        foreach ($filename as $name) {
            $this->image=$name;
            $this->thumbmaker();
            $this->savethumb();
        }
    }

    private function thumbmaker() {

        if($this->loadimage()) {
            $this->createemptythumbnail();
            if ($this->Shadow) {$this->addshadow();}
            imagecopyresampled($this->thumb,$this->im,$this->Framewidth,$this->Framewidth,0,0,$this->thumbx-(2*$this->Framewidth)-$this->shadow_offset,$this->thumby-2*$this->Framewidth-$this->shadow_offset,imagesx($this->im),imagesy($this->im));
        }
    }

    private function loadimage() {

        if (file_exists($this->image)) {
            $this->size=GetImageSize($this->image);
            switch($this->size[2]) {
                case 1:
                    if (imagetypes() & IMG_GIF) {$this->im=imagecreatefromgif($this->image);return true;} else {$this->invalidimage(‘No GIF support’);return false;}
                    break;
                case 2:
                    if (imagetypes() & IMG_JPG) {$this->im=imagecreatefromjpeg($this->image);return true;} else {$this->invalidimage(‘No JPG support’);return false;}
                    break;
                case 3:
                    if (imagetypes() & IMG_PNG) {$this->im=imagecreatefrompng($this->image);return true;} else {$this->invalidimage(‘No PNG support’);return false;}
                    break;
                default:
                    $this->invalidimage(‘Filetype ?????’);
                    return false;
            }
        } else {
            $this->invalidimage(‘File not found’);
            return false;
        }
    }

    private function invalidimage($message) {
        $this->thumb=imagecreate(80,75);
        $black=imagecolorallocate($this->thumb,0,0,0);$yellow=imagecolorallocate($this->thumb,255,255,0);
        imagefilledrectangle($this->thumb,0,0,80,75,imagecolorallocate($this->thumb,255,0,0));
        imagerectangle($this->thumb,0,0,79,74,$black);imageline($this->thumb,0,20,80,20,$black);
        imagefilledrectangle($this->thumb,1,1,78,19,$yellow);imagefilledrectangle($this->thumb,27,35,52,60,$yellow);
        imagerectangle($this->thumb,26,34,53,61,$black);
        imageline($this->thumb,27,35,52,60,$black);imageline($this->thumb,52,35,27,60,$black);
        imagestring($this->thumb,1,5,5,$message,$black);
    }       

    private function createemptythumbnail() {
        $thumbsize=$this->Thumbsize;
        if ($thumbsize>$this->size[0] && $thumbsize>$this->size[1]) {$thumbsize=max($this->size[0],$this->size[1]);}
        if ($this->Shadow) {$this->shadow_offset=3;} else {$this->shadow_offset=0;}
        $x1=$this->Framewidth*2+$thumbsize+$this->shadow_offset;
        $x2=$this->Framewidth*2+ceil($this->size[0]/($this->size[1]/$thumbsize))+$this->shadow_offset;
        $y1=$this->Framewidth*2+ceil($this->size[1]/($this->size[0]/$thumbsize))+$this->shadow_offset;
        $y2=$this->Framewidth*2+$thumbsize+$this->shadow_offset;
        if ($this->size[0]>$this->size[1]) {$this->thumb=imagecreatetruecolor($x1,$y1);} else {$this->thumb=imagecreatetruecolor($x2,$y2);}
        $this->thumbx=imagesx($this->thumb);$this->thumby=imagesy($this->thumb);
        imagefill($this->thumb,0,0,imagecolorallocate($this->thumb,hexdec(substr($this->Backgroundcolor,1,2)),hexdec(substr($this->Backgroundcolor,3,2)),hexdec(substr($this->Backgroundcolor,5,2))));           
        imagefilledrectangle($this->thumb,0,0,$this->thumbx-$this->shadow_offset,$this->thumby-$this->shadow_offset,imagecolorallocate($this->thumb,hexdec(substr($this->Framecolor,1,2)),hexdec(substr($this->Framecolor,3,2)),hexdec(substr($this->Framecolor,5,2))));
    }

    private function addshadow() {
        $gray=imagecolorallocate($this->thumb,192,192,192);
        $middlegray=imagecolorallocate($this->thumb,158,158,158);
        $darkgray=imagecolorallocate($this->thumb,128,128,128);
        imagerectangle($this->thumb,0,0,$this->thumbx-4,$this->thumby-4,$gray);
        imageline($this->thumb,0,$this->thumby-3,$this->thumbx,$this->thumby-3,$darkgray);
        imageline($this->thumb,$this->thumbx-3,0,$this->thumbx-3,$this->thumby,$darkgray);
        imageline($this->thumb,2,$this->thumby-2,$this->thumbx,$this->thumby-2,$middlegray);
        imageline($this->thumb,$this->thumbx-2,2,$this->thumbx-2,$this->thumby,$middlegray);
        imageline($this->thumb,2,$this->thumby-1,$this->thumbx,$this->thumby-1,$gray);
        imageline($this->thumb,$this->thumbx-1,2,$this->thumbx-1,$this->thumby,$gray);
    }

    private function savethumb() {
        switch($this->size[2]) {
            case 1:
                imagegif($this->thumb,$this->Thumblocation.$this->Thumbprefix.$this->image);
                break;
            case 2:
                imagejpeg($this->thumb,$this->Thumblocation.$this->Thumbprefix.$this->image,90);
                break;
            case 3:
                imagepng($this->thumb,$this->Thumblocation.$this->Thumbprefix.$this->image);
                break;
        }       
        if ($this->Chmodlevel!=”) {chmod($this->Thumblocation.$this->Thumbprefix.$this->image,octdec($this->Chmodlevel));}
        imagedestroy($this->im);
        imagedestroy($this->thumb);
    }

}

$thumb = new thumbnail;

$thumb->Thumbsize                = 140;                                                // The max. width or height
$thumb->Framewidth            = 10;                                                    // The frame width around the photo
$thumb->Shadow                    = true;                                                // Drop a shadow around the thumbnail   
$thumb->Backgroundcolor    = ‘#FFFFFF’;                                    // The background color (needed for the sahdow effect)
$thumb->Framecolor            = ‘#FFFFFF’;                                    // The frame color
$thumb->Chmodlevel            = ‘0755’;                                            // The chmod level for saving the thumbnails
$thumb->Thumblocation        = ‘thumbs/’;                                    // The directory path for the thumbnails
$thumb->Thumbprefix            = ‘thumb_’;                                        // The prefix for the thumbnails
$tableformat                        = ‘3×3’;                                            // The table size: columns x rows

$scriptname=basename(__FILE__);
if ($scriptname==”) {$scriptname=’index.php’;}

$images=array();$newthumbs=array();
if ($dir=@opendir(‘./’)) {
  while ($filename=@readdir($dir)) {
    if (($filename!=’.’) && ($filename!=’..’) && !is_dir($filename)) {
      $extension=strtolower(substr($filename,strrpos($filename,’.’)+1,strlen($filename)));
      if ($extension==’jpg’ || $extension==’jpeg’ || $extension==’png’ || $extension==’gif’) {
                $images[]=$filename;
                if (!file_exists($thumb->Thumblocation.$thumb->Thumbprefix.$filename))
                    $newthumbs[]=$filename;
      }
    }
  }
}

if (count($newthumbs)>0) {
    if (!file_exists($thumb->Thumblocation)) {@mkdir($thumb->Thumblocation);@chmod($thumb->Thumblocation,octdec($thumb->Chmodlevel));}
    $thumb -> Createthumb($newthumbs);
}

echo "<style type="text/css">n";
    echo "a:link,a:visited,a:hover {n";
    echo "    font-family : Helvetica, Arial;n";
    echo "    font-size : 12px;n";
    echo "    color : #000000;n";
    echo "    text-decoration : none;n";
    echo "}n";
echo "</style>n";

echo "<table width="100%" height="100%"><tr><td align="center" valign="middle">n";

$column=0;
$table=explode(‘x’,$tableformat);
$tablesize=$table[0]*$table[1];
if (isset($_REQUEST[‘page’])) {$page=intval($_REQUEST[‘page’]);} else {$page=1;}
if (isset($_REQUEST[‘photo’])) {$photo=$_REQUEST[‘photo’];} else {$photo=”;}
echo "<table>n";
if ($photo==”) {
    foreach($images as $key => $image) {
        if ($key>=($tablesize*($page-1)) && $key<($tablesize*$page)) {
            if ($column==0) {echo "<tr>";}
            echo "<td><a href="$scriptname?page=$page&photo=" . urlencode($image) . ""><img src="" . $thumb->Thumblocation.$thumb->Thumbprefix.$image. "" border="0"></a></td>";
            $column+=1;
            if ($column==$table[0]) {$column=0;echo "</tr>n";}
        }
    }
    if ($column<$table[0] && $column>0)
        echo str_repeat(‘<td>&nbsp;</td>’,$table[0]-$column) . "</tr>n" . "</table>n";
    else
        echo "</table>n";
} else {
    echo "<tr><td><a href="$scriptname?page=$page"><img src="" . basename($photo) . "" border="0"></a></td></tr>n". "</table>n";
}

if ($tablesize<count($images) && $photo==”) {
    echo "<table><tr><td align="center">";
    for ($i=1;$i<=ceil(count($images)/$tablesize);$i++) {
        if ($page==$i) {echo " <a href="$scriptname?page=$i"><u>$i</u></a>";} else {echo " <a href="$scriptname?page=$i">$i</a>";}
    }
    echo "</td></tr></table>n";
}

echo "</td></tr></table>n";

?>

That was cool. I will post the link to the gallery after I do some modifications the layouts. I might want to add some commenting or ranking system to it. Let’s see………

Advertisement

Leave a Reply

Your email address will not be published. Required fields are marked *