Today I'll be starting a new series focusing on the use of PHP's image manipulation functions to create a sharp-looking web based photo gallery. If you have been working with PHP for any reasonable length of time (or for that matter, really looked at our own Code Gallery) you'll notice that there have been many different types of PHP scripts focused on creating "thumbnail" images. This week's spotlight takes that concept a few steps further and includes some great features like multiple-gallery support, the addition of a border around your thumbnails, and a gallery indexing system used to speed up the web page as a whole.
Note: This week's spotlight uses some client-side JavaScript to accomplish some of the desired behaviors in the final page. It is recommended that you have some understanding of JavaScript before proceeding, as the details of the JavaScript involved will not be discussed in this series.
How the AutoGallery Works (image management) The Auto Photo Gallery (AutoGallery) is designed to work in such a way so that any directory containing Jpeg (.jpg) files can automatically be turned into a clean and well-organized photo gallery. The first step in the AutoGallery script is to create the necessary thumbnails for the images that do not yet have one and store them in a cache directory. Once the thumbnails have been created, an index file is also created containing useful information regarding the images for future reference in the same directory. After the creation of necessary thumbnails, a second function is executed to remove any unnecessary thumbnails that may still exist in the index and cache directory by cross-referencing the index with the original graphic images. Any thumbnail image that exists in the index and whose original graphic no longer exists is removed to conserve space.
The AutoGallery ObjectToday's spotlight script is in reality a PHP class consisting of a total of seven member functions and seven member variables. Because it is often easier to explain from the inside-out I will first discuss the member functions that the class uses internally and then build off of those to explain the functions that are used to actually pull the entire class together. Let's start with the constructor function and the seven member variables that the class uses.
The autogallery constructorThe AutoGallery constructor function in today's script has two purposes. It's first purpose is the initialization of all seven of the member variables (all of which I will describe below). After the variables have been initialized, the construct also calls two functions resize() and check() -- both of which I will describe later. For the most part, the member variables set in the constructor can be left as-is without modification. However, three of the seven must be set by values passed as parameters to the constructor function.
The AutoGallery constructor function requires three parameters to be passed to it as follows:
$path A web-accessible directory where the index and thumbnail files can be stored.
$src The web-accessible directory containing the Jpeg image files to be displayed in the gallery
$tpl The location of the PNG-format graphic file used as a border for the completed thumbnail
With this in mind, let's take a look at the constructor function below:
Code Flow
- Initialize the member variables
- Call the check() and resize() member functions
class autogallery { function autogallery($path="cache",$src="images/2001",$tpl="images/frame.png" ) {
$this->path = $path; $this->src = $src; $this->tpl = $tpl;
$this->page = 'p'; $this->gallery = 'g'; $this->prefix = sprintf('%s_',substr(md5($this->src ), 0, 6 ) ); $this->idxfile = sprintf('_%s.txt',md5($this->src ) );
$this->resize();
$this->checkup();
}
As you can see, the three parameters are immediately stored in their respective members variables to be used later. The last four parameters however, require a bit more description. The first two of these four parameters $page and $gallery actually stores the name of the variable as used in a GET or POST method to indicate the respective page/gallery number that the script will be displaying. For instance, if the value of $page was "mypage" and the value of $gallery was "mygallery" the URL to show Page 2 of Gallery 4 would be something along the following:
/mygallery/index.php?mypage=2&mygallery=4
Where if the values of $page and $gallery was left as is, the same pictures could be retrieved using a similar URL:
/mygallery/index.php?p=2&g=4
The next two values, $prefixand $idxfile, are used to ensure that both the index file (which I'll discuss later) and the thumbnail filenames are unique. Specifically, $prefix must be a unique identifier which is appended to the beginning of every thumbnail file and $idxfile, as the name implies, represents the index filename used by the gallery script. Because both of these variables must be unique to a given set of photos, their values have been generated from a partial unique MD5 string. The uniqueness of these two parameters is very important, as you will learn later all thumbnails (regardless of the gallery that they belong in) are stored in a single universal directory along with their associated index file. Without unique filenames, the script could not function properly with multiple galleries.
Finally, after all of the variables have been taken care of the constructor calls first the resize() and then the checkup() function.
More to come!That's it for this week. Now that you have a fundamental understanding of how the AutoGallery script works when dealing with image management, next week you'll be able to better understand the two management functions resize() and checkup() which perform the thumbnail creation and removal processes I outlined earlier in the column.
Article originally published by Zend, the php company.
John Coggeshall is a PHP consultant and author who started losing sleep
over PHP around five years ago. Lately you'll find him losing sleep
meeting deadlines for books or online columns on a wide range of PHP
topics. He maintains a PHP website packed full of PHP-related materials,
tutorials, projects and more at http://www.coggeshall.org/.
About the author:
| John Coggeshall is a PHP consultant and author who started losing sleep over PHP around five years ago. Lately you'll find him losing sleep meeting deadlines for books or online columns on a wide range of PHP topics. He maintains a PHP website packed full of PHP-related materials, tutorials, projects and more at http://www.coggeshall.org/. | |
Comments
Post new comment