Creating An Image Management System for Your Website with Smart Image Resizer and Lightbox 2
When I was developing this site, I decided early on that I wanted to use Lightbox 2 as my primary image viewing module. It's attractive and suits the clean look on my site. However, as I started adding more CMS features to my site through PHP, I realized I would need software that would automatically resize images as well.
Enter Smart Image Resizer. As its name implies, it doesn't just reduce the physical size of the image, but also does so so that the image can be cropped and resized gracefully. Even if you blow up the image a bit, it will be filtered so as not to become pixelated.
These two solutions are all well and good, but in order to make them work together properly, I had to write some of my own code through PHP to generate the final HTML.
Experience requirement: Intermediate
Be aware: I do not recommend a straight copy/paste method approach to this code. Understand what you are using and how secure or flexible you want the inputs to be. I don't supply secure code.
The Tools
First thing, Lightbox 2 uses Scriptaculous and Prototype, so you'll have to make sure you have those installed. Please follow the directions on those sites to make your site ready to go.
The basic way to instantiate Lightbox 2 is through the following code:
<!-- For single samples attached to a link -->
<a href="images/image-1.jpg" rel="lightbox" title="my caption">image #1</a>
<!-- For multiple samples attached to an image link -->
<a href="images/image-1.jpg" rel="lightbox[group]" title="my caption"><img src="images/image-1_thumb.jpg"></a>
<a href="images/image-2.jpg" rel="lightbox[group]" title="my caption"></a>
In the code above, you can see the example of how a link might use the lightbox, versus an imagelink. This should be pretty straightforward. Things get a little more interesting when you decide to take advantage of Lightbox 2's version of albums, as seen in the second example. You don't need a physical link to appear for additionally grouped images to appear in the same lightbox album. For this reason, this module worked well for me, as I wanted to have single images represent a link to items that had multiple pages worth of samples in the same category.
Image Resizer is cool because it uses a PHP script referred to in the <img> tag to automatically place in the resized image.
<!-- Notice how HTML needs the path to the image.php file (SIR's path, basically), followed by a '/' and the filename, followed by various parameters that you can pass through to SIR. -->
<img src="/php/image.php/samplefile.jpg?width=150&height=150&cropratio=1:1ℑ=/samplefile.jpg" />
The PHP
Now that we have our essential HTML elements in place, we need a way to upload and manage the files. I decided, rather than uploading files directly to the database (a method with the potential of slowing down my site because of heavy database access), I would create a process that uploads the files into a designated folder on my server, and also updates the database at the same time.
The Final Show
So now that we have the ability to upload pictures, see all of the images we've uploaded, and delete them when necessary, we're ready to figure out how to make them so that they list into the site's album page. I created the this file (with a .php end, not .rtf) to be included wherever I have image samples I want to list.
For this portion, I run each image in the database through two loops. The first loop runs through each item row in the database. The second loop checks each item's group designation with the groups that have already been processed. If there has already been a link produced with the group in question, the item doesn't get its own image thumbnail. If there isn't any other items with a group, or if the item doesn't have a designated group, then the item gets its own thumbnail.
What this ends up producing is a catalog of thumbnail images, and if an image has other images that I've associated with it in the database (through the 'group' mechanic), those images will show up as additional pages in the Lightbox 2 module that pops up, but won't show themselves in the initial album. In this way, I have a 'pages within pages' architecture that well-suits the examples I have in my portfolio.
If you have any questions about how all of this works, or any suggestions about my advice here, please contact me.
Tags: image, management, php, scriptaculous, prototype, lightbox 2, html, code
Some other articles you might enjoy...
| How to Talk Like PHP: Part 2 | Monday, June 15, 2009 |
| JQuery/PHP: Simple Random Text Generation Box | Monday, June 22, 2009 |
| Snazzy jQuery Form Alert Boxes | Tuesday, February 9, 2010 |
| PHP Popular Tags List | Saturday, June 27, 2009 |
| How to Talk Like PHP: Part 1 | Tuesday, June 9, 2009 |
design 16 articles
user interface 7 articles
php 7 articles
code 7 articles
web 6 articles
jquery 6 articles
tutorial 6 articles
picture 5 articles
marketing 5 articles
html 4 articles
2012
2011
2010
2009