Article

Build An Automated PHP Gallery System In Minutes

Page: 1 2 3 4 5 6 7 8

Further Functionality

The above solutions create the basic capabilities of a gallery system, though in the future you may want to be able to edit and/or delete the photos from the database. You might also want the ability to add, edit, and delete the gallery’s categories, all from a browser-based interface.

Well, that might sound like a lot of work, but actually, it isn't -- the only catch here lies in building this script in a way that blocks unauthorised users from messing around with your gallery. A simple authentication form should be enough to achieve that.

Here are a few functions you could use:

Editing Photo Details and Moving a Photo to a New Category

function edit_photo( $photo_id, $new_caption, $new_category )        
{        
 mysql_query( "UPDATE gallery_photo SET photo_caption='".addslashes( $new_caption )."', photo_category='".addslashes( $new_category )."' WHERE photo_id='".addslashes( $photo_id )."'"  );        
}

Call: edit_photo( PHOTO_ID, “New Caption”, NEW_CATEGORY_ID );

Deleting a Photo

function delete_photo($photo_id)        
{        
 $result = mysql_query("        
   SELECT photo_filename        
   FROM gallery_photo        
   WHERE photo_id = '" . addslashes($photo_id) . "'        
 ");          
 list($filename) = mysql_fetch_array($result);        
 mysql_free_result($result);        
       
 unlink($images_dir . '/' . $filename);        
       
 mysql_query("        
   DELETE FROM gallery_photo        
   WHERE photo_id='" . addslashes($photo_id) . "'        
 ");        
}

Call: delete_photo(PHOTO_ID);

Adding a Category

function add_category( $category_name )        
{        
 mysql_query( "INSERT INTO gallery_category(`category_name`) VALUES('".addslashes( $category_name )."' )" );        
}

Call: add_category( “Category Name” );

Editing Category Details

function edit_category( $category_id, $new_name )        
{        
 mysql_query( "UPDATE gallery_category SET category_name='".addslashes( $new_name )."' WHERE category_id='".addslashes( $category_id )."'" );        
}

Call: edit_category( CATEGORY_ID, “New Name” );

Deleting A Category

function delete_category( $category_id )        
{        
 global $images_dir;        
 $result = mysql_query( "SELECT photo_filename FROM gallery_photo WHERE photo_category='".addslashes( $category_id )."'" );          
 while( $row = @mysql_fetch_array( $result ))        
 {        
   unlink($images_dir."/".$row[0]);        
 }        
 mysql_query( "DELETE FROM gallery_photo WHERE photo_category='".addslashes( $category_id )."'" );        
 mysql_query( "DELETE FROM gallery_category WHERE category_id='".addslashes( $category_id )."'" );        
}

Call: delete_category( CATEGORY_ID );

Incorporating Your Site’s Design

After you have completed and tested your gallery system, you’re bound to think “Something is missing! When I upload the gallery on my Web server, it doesn’t fit into my current design!” No problem! I have a little tweak for you.

In all the scripts we’ve made (excluding config.inc.php), we had included the database connection file before we started any coding. Therefore, to include our design structure, we need to add our own design file too. Therefore, you need to add the following piece of code just after including your database connection file:

include 'design.inc.php';

Your design.inc.php could look like this:

$design_header = <<<__HTML_END        
       
<!-- Add your site's header here -->        
       
__HTML_END;        
       
$design_footer = <<<__HTML_END        
       
<!-- Add your site's footer here -->        
       
__HTML_END;

You can easily add your HTML design in the above code by breaking it down into header and footer blocks. Then, locate the <html> tag in all the 3 scripts (refer to the files included in the downloadable archive) and add the variables $design_header and $design_footer in appropriate locations.

That’s All Folks!

Now that you have a decent gallery for your personal use, you can actually shape it into a gallery that can accept public submissions from your site’s visitors. The above code may look like a lot of work at first, but it will actually eradicate much future manual work, especially if you frequently add images to your Website.

As an ending note, I would like to say that over the years I have developed similar yet pretty complex gallery solutions, and I have seen an unlimited number of features that can be integrated into such a versatile application.

While in this article I’ve only described the bare necessities of a decent gallery system, there really is no limit to what can be developed. For example, in the future you might want to add a watermark to your images, or maybe automatically resize large images to a preset size, or even build ecards out of your gallery images; the possibilities are endless, what really matters is what you need...

Note: For version older than 4.1.0, equivalent of _GET, _POST, _FILES are arrays HTTP_GET_VARS, HTTP_POST_VARS, HTTP_POST_FILES.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: