• RSS
  • Facebook
  • Twitter
Comments


Created by Martin Angelov on Sep 10th, 2009

Introduction

One of the main benefits of using a popular java script library, such as jQuery, is the incredible number of available plug-ins that will kick-start any development project.
Today we are going to build a custom gallery that scans a folder of images and outputs a slick gallery, utilizing PHP, CSS, jQuery and the jQuery Lightbox plug-in.
There is no need to download the plug-in – I’ve included it in the demo files, so grab them and get on reading.

The XHTML

We start off with our XHTML front-end.

demo.php

01<div id="container">
02 
03<div id="heading">
04<h1>A cool jQuery galleryh1>
05div>
06<div id="gallery">
07 
08php
09//our php code goes here
10?>
11 
12<div class="clear">div>
13div>
14 
15<div id="footer">
16div>
17 
18div>
And that is basically all there is to it. Note the highlighted area – this is where we put our PHP code, that will generate the gallery images. Now lets go through how this is done.

The PHP

The idea is simple – our PHP back-end is going to scan a folder that we’ve set up with our gallery images, and turn it into a fancy CSS & jQuery gallery. The great thing about this strategy is that it is incredibly easy to set up a gallery, and adding images to an existing one is a charm – just dump them in the gallery’s directory via FTP and it is ready to go.

demo.php

01$directory = 'gallery'; //where the gallery images are located
02 
03$allowed_types=array('jpg','jpeg','gif','png'); //allowed image types
04 
05$file_parts=array();
06$ext='';
07$title='';
08$i=0;
09 
10//try to open the directory
11$dir_handle = @opendir($directory) or die("There is an error with your image directory!");
12 
13while ($file = readdir($dir_handle))    //traverse through the files
14{
15    if($file=='.' || $file == '..') continue;   //skip links to the current and parent directories
16 
17    $file_parts = explode('.',$file);   //split the file name and put each part in an array
18    $ext = strtolower(array_pop($file_parts));  //the last element is the extension
19 
20    $title = implode('.',$file_parts);  //once the extension has been popped out, all that is left is the file name
21    $title = htmlspecialchars($title);  //make the filename html-safe to prevent potential security issues
22 
23    $nomargin='';
24    if(in_array($ext,$allowed_types))   //if the extension is an allowable type
25    {
26        if(($i+1)%4==0) $nomargin='nomargin';   //the last image on the row is assigned the CSS class "nomargin"
27        echo '
28        
class="pic '.$nomargin.'" style="background:url('.$directory.'/'.$file.') no-repeat 50% 50%;">
30        ';
31 
32        $i++;   //increment the image counter
33    }
34}
35 
36closedir($dir_handle);  //close the directory
By traversing through the files in the directory and skipping the non-image files, we output some XHTML code for every image . This code (lines 28-39) consists of a div container, with a CSS class pic (and in some cases a nomargin, more on that later), and we set its background to the image file via the style attribute. We position the image in the center of the background by specifying its position to be 50% 50%. This centers it both horizontally and vertically and thus shows  only the middle part, that fits into the div container’s size. This creates a nice thumbnail, with no need of actually resizing the image.
This works best with images with smaller resolutions, so you should consider resizing those 10 megapixel photos before uploading them.
The div contains a hyperlink which is linked to the image and has a title of the image filename. Both these attributes are used by the lightBox plugin to generate the lightbox gallery. So by renaming the file, you can change the caption that shows under it.
You may wonder what is the point of that nomargin class? Every image in the gallery has a right and a bottom margin. But this means that it is not possible the last element of each row to align with the right part of the heading div and it looks amateurish. So we assign this special class, which clears the right margin for the last element on each row and gives us a proper alignment.
The jQuery Gallery
The jQuery Gallery

The CSS

Everything is set up, but we still have to give it that cool look.

demo.css

01/* first reset some of the elements for browser compatibility */
02body,h1,h2,h3,p,td,quote,small,form,input,ul,li,ol,label{
03    margin:0px;
04    padding:0px;
05    font-family:Arial, Helvetica, sans-serif;
06}
07 
08body{   /* style the body */
09    margin-top:20px;
10    color:white;
11    font-size:13px;
12    background-color:#222222;
13}
14 
15.clear{ /* the clearfix class */
16    clear:both;
17}
18 
19a, a:visited {  /* a:visited is needed so it works properly in IE6 */
20    color:#00BBFF;
21    text-decoration:none;
22    outline:none;
23}
24 
25a:hover{    /* the hover effect */
26    text-decoration:underline;
27}
28 
29#container{ /* the main container div */
30    width:890px;
31    margin:20px auto;
32}
33 
34#heading,#footer{   /* the header and the footer share some of their style rules */
35    background-color:#2A2A2A;
36    border:1px solid #444444;
37    height:20px;
38    padding:6px 0 25px 15px;
39    margin-bottom:30px;
40    overflow:hidden;
41}
42 
43#footer{    /* ..but not all */
44    height:10px;
45    margin:20px 0 20px 0;
46    padding:6px 0 11px 15px;
47}
48 
49div.nomargin{   /* our special nomargin class */
50    margin-right:0px;
51}
52 
53.pic{   /* divs that hold all the pictures in the gallery */
54    float:left;
55    margin:0 15px 15px 0;
56    border:5px solid white;
57    width:200px;
58    height:250px;
59}
60 
61.pic a{ /* in every .pic container there is a hyperlink exactly the size of the container */
62    width:200px;
63    height:250px;
64    text-indent:-99999px;
65    display:block/* don't forget that widths and heights of hyperlinks are useless without display:block */
66}
67 
68h1{ /* style the heading */
69    font-size:28px;
70    font-weight:bold;
71    font-family:"Trebuchet MS",Arial, Helvetica, sans-serif;
72}
73 
74h2{ /* the footer text */
75    font-weight:normal;
76    font-size:14px;
77    color:white;
78}

The jQuery

To make it all tick, we need to include the jQuery java script library in our page, and add the lightBox plugin. The following code was taken from the head section of demo.php:
1<link rel="stylesheet" type="text/css" href="lightbox/css/jquery.lightbox-0.5.css">
2<link rel="stylesheet" type="text/css" href="demo.css" />
3 
4<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">script>
5<script type="text/javascript" src="lightbox/js/jquery.lightbox-0.5.pack.js">script>
6<script type="text/javascript" src="script.js">script>
On line 1 we include the lightbox plugin’s CSS file, which styles the lightbox that displays the images. On line 2 we include our own CSS file.
Line 4 is where we include the jQuery library from Google’s CDN. Later come the lightbox plugin itself and our own script.js file.
Now we are ready to put the finishing touch.

script.js

01// after the page has finished loading
02$(document).ready(function(){
03 
04    $('.pic a').lightBox({
05    // we call the lightbox method, and convert all the hyperlinks in the .pic container into a lightbox gallery
06 
07        imageLoading: 'lightbox/images/loading.gif',
08        imageBtnClose: 'lightbox/images/close.gif',
09        imageBtnPrev: 'lightbox/images/prev.gif',
10        imageBtnNext: 'lightbox/images/next.gif'
11 
12    });
13 
14});
The lighbox() method takes an object as an optional parameter. The only reason we provide any parameters is that I changed the default location of the plugin, putting it in a subfolder /lightbox which aids for a cleaner file structure. Unfortunately the images that the plug-in uses become inaccessible and have to be provided manually.
With this our gallery is complete.

Conclusion


Postado por Fernando Schimit - Fox Creative

Categories: ,

Leave a Reply