Photo Album Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
caseymanus
Forum Commoner
Posts: 34
Joined: Wed Nov 20, 2002 10:32 pm
Contact:

Photo Album Script

Post by caseymanus »

All,

I dont know if this forum is a good place to post this sort of thing, but I am registered here so I thought I would. I created this semi-generic script to allow users to browse a folder of photos, with subfolders (the albums in this case).

You must be using PHP 4.1 for this to work.

To install, installs this script as pics.php in your document root, then in your doc root create a folder (referred to as $basedir) to hold your subfolders or albums. You must then modify the variable $basedir to be the actual path to the folder your created to hold your albums. Then just drop pictures in your albums!

When you call this page initially you need to pass in pics.php?album=none to display the list of albums in $basedir.

No, this isnt the cleanest code it took only about 20 mins and needs more work but I dont have time. Anyway here it is, feel free to modify and repost.

Code: Select all

<?PHP 
$basedir = "/Library/WebServer/Documents/photo/";
$album = $_REQUEST&#1111;'album'];
if ($_REQUEST&#1111;'album'] == 'none')
    &#123;
	printf("<html><head><style>");
	printf("body &#123;background:#000000;
		      color:#ffffff;
		      font-family:verdana;
		      font-weight:bold;
		      font-size:10pt;&#125;
		a &#123;text-decoration:none;
		   font-family:verdana;
		   font-size:9pt;
		   color:#ff0000;&#125;</style></head><body>"); 

	printf("<center>Choose a Photo Album<br>");
	if ($fhandle = opendir($basedir))
	&#123;
	    while (false !== ($file = readdir($fhandle)))
		&#123; $filepath = sprintf("%s%s", $basedir, $file);
		  if (is_dir($filepath)&&($file != '.')&&($file !='..'))
			&#123;
			 printf("<a href='pics.php?album=%s'>%s</a>", $file, $file);
			 printf("<br>");
			&#125;
		&#125;
	closedir($fhandle);
	&#125;
    &#125;
else
&#123;
$dir = sprintf("%s%s/",$basedir, $album);

$pic_num = $_REQUEST&#1111;'pic_num'];
if ($pic_num == '')
    &#123;
        $pic_num = 0;
        &#125;
$pic_counter = 0;


if ($handle = opendir($dir))
	&#123;
		while (false !== ($file = readdir($handle)))
		&#123;
		 if (($file != '.')&&($file != '..')&&($file != '.DS_Store'))
			&#123;
                        $pic_counter++;
                        //printf($pic_counter);
    
                        &#125;
                
                 &#125;
    closedir($handle);
        &#125;
else &#123;
        printf("Couldnt Open Directory");
        &#125;
if ($pic_counter == 0)
	&#123;
	 printf("No Pictures To Display");
	&#125;
                
if($pic_num == 0)
        &#123;$prev_pic = $pic_counter-1;&#125;
else
        &#123;$prev_pic = $pic_num-1;&#125;
if($pic_num == $pic_counter-1)
        &#123;$next_pic = 0;&#125;
else
        &#123;$next_pic = $pic_num+1;&#125;
$sec_pic_counter = 0;
        
    if ($sechandle = opendir($dir))
    &#123;
        while(false !== ($file = readdir($sechandle)))
            &#123;
            if (($file != '.')&&($file != '..')&&($file != '.DS_Store'))
			&#123;

                            if ($pic_num == $sec_pic_counter)
                            &#123;
                            printf("<html><head><style>
                                    body &#123; background:#000000;
                                           font-family:verdana;
                                           font-size:8pt;
                                           color:#ffffff;&#125;
                                    a &#123; font-family:verdana;
                                        text-decoration:none;
                                        font-size:11pt;
                                        color:#ff0000;
                                        &#125;
                                    </style>
                                    <script language=JavaScript>
<!--

var message='Dont Steal!';

function clickIE4()&#123;
if (event.button==2)&#123;
alert(message);
return false;
&#125;
&#125;

function clickNS4(e)&#123;
if (document.layers||document.getElementById&&!document.all)&#123;
if (e.which==2||e.which==3)&#123;
alert(message);
return false;
&#125;
&#125;
&#125;

if (document.layers)&#123;
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
&#125;
else if (document.all&&!document.getElementById)&#123;
document.onmousedown=clickIE4;
&#125;

document.oncontextmenu=new Function('alert(message);return false');

// --> 
</script>
                                    </head>
                                    <body>
                                    ");

                            printf("<center>");
                            printf("Displaying Picture: %s", $file);
                            printf("<BR>");
                            printf("<table><tr>");
                            printf("<td align=left><a href='/pics.php?pic_num=%s&album=%s'>Next Pic >></a></td>", $next_pic, $album);
                            printf("<td width=250 align=center><a href='/pics.php?album=none'>Back To Albums List</a></td>");
                            printf("<td align=right><a href='/pics.php?pic_num=%s&album=%s'><< Previous Pic</a></td>", $prev_pic, $album);
                            printf("</td></tr></table>");
                            printf("<br>");
                            printf("<img src='/Photo/%s/%s' border=2>", $album, $file);
                                                  
                            printf("</center>");
                            &#125;
                             $sec_pic_counter++;
                        &#125;
           
           
            &#125;
                   
    closedir($sechandle);
    &#125;
&#125;
?>
User avatar
Wayne Herbert
Forum Commoner
Posts: 34
Joined: Tue Apr 29, 2003 3:13 pm
Location: Houston, Texas

Post by Wayne Herbert »

I like mine better. Uses a mysql database to store thumbs and annotation, displays by category, etc.

See http://www.herbhost.com/seasia/index.htm

3 scripts... more than 20 mins :D Email me if you want the scripts that drive this site.
User avatar
caseymanus
Forum Commoner
Posts: 34
Joined: Wed Nov 20, 2002 10:32 pm
Contact:

Post by caseymanus »

The point of my script is once its installed all you have to do is drop a pic in the folder and its that simple. To create albums all you have to do is create a folder. I wanted something any 4 yo could add albums and pics to without any complicated admistration.
Post Reply