Visit Twellow.com
Popular » Can't Buy The Top Copycat Spammers Online Obstacles Crimes On YouTube eBay Fair Trade eBay Feedback
Directory Listings » Blogs Conferences Forums Software Tutorials Submit Site

A Dynamic PHP Thumbnail Gallery Part 3

In my last column, I introduced the resize() function and a simple cache system is used to create and keep updated the thumbnails for all the images that are to be displayed in the picture gallery. This week, I'll introduce the second half of the AutoGallery's management system by discussing the checkup() member function which handles the removal of thumbnails which are no longer necessary.

The checkup() function If you recall from part 1 of this series, two functions that are called every time an instance of the AutoGallery object is created are the resize() and checkup() functions. Last week, you learned how the AutoGallery script works when creating thumbnails using the simple indexing and cache system. The checkup() function performs the complementing functionality to the resize() function and handles the removal the thumbnail images and index entries for items where the original picture has been removed (therefore no longer part of the photo gallery). Let’s get started. Making the reference string The first step in the checkup() function is the creating of a reference string. The reference string is basically a complete listing of all of the images that are a part of the photo gallery (all of the files located in the directory path represented by the $src variable) represented by a single string. Specifically, the format of the string will be something along the following: image1.jpgimage2.jpgimage3.jpg Although not obviously useful, consider the use of PHP’s strstr() function. This function, given a string and a string to search strstr() will return a portion of the search string starting from the first location the string your looking for was found and, more importantly, returns false if the string wasn’t found at all. So, using the above reference string (represented by the variable $searchstr below) consider the following example: Note: The code below is not a part of the AutoGallery script. It is shown only as an example of the strstr() function. <?php         $myresult = strstr($searchstr, "image2.jpg");         echo $myresult; // Echos "image2.jpgimage3.jpg"         $myresult = strstr($searchstr, "image4.jpg");         echo $myresult; // $myresult is false ?> In the first call to strstr(), you are searching for the string "image2.jpg&" in the reference string $searchstr. Since the string existed in the reference string, strstr() returns the portion of the string including and immediately following where the search string was found. In the second example, the string "image4.jpg&" is searched for but does not exist so the strstr() function returns false. To use this concept practically, you’ll need to create a reference string consisting of every filename in the photo-gallery directory (remember the gallery directory is stored in the member variable $src) and then cross-reference that string against the filenames found in the cache indexing file. If at any point in time an entry for a file exists in the index, but not in the reference string, you’ll need to delete the corresponding thumbnail file. For your reference, here is the format of the index file again: <thumbnail filename> , <origional filename>, <org. width> , <org height> Keep this format in mind as I introduce the actual code below. Code Flow

  • Load the correct indexing file into memory using the file() function
  • Create a directory object using the dir() function for the original image directory (represented by the variable $src)
  • Append every filename in the directory to the reference string $ref_idx
  • Cycle through every line of the index file and compare the second entry of each line (the original file name) to make sure it exists in the reference string. If it does not, delete the corresponding thumbnail filename. Otherwise, store the entire line of the reference file in the variable $new_idx
<?php  function checkup() {    $idx = file("{$this->path}/{$this->idxfile}" );    $i=0;   $d = dir($this->src );    while($entry=$d->read() ) {        $ref_idx .= "$entry,";    }    $d->close();    for($f = 0; $f < count($idx); $f++) {        $items = explode(",", $idx[$f] );        if(strstr($ref_idx, trim($items[1]) )) {            $new_idx[$i] = trim($idx[$f])."n";            $i++;       } else {            if(file_exists("{$this->path}/{$items[0]}"))                unlink("{$this->path/{$items[0]}");       }     } } Looking at the above code, you can probably see a bit more clearly how the checkup() function makes sure the AutoGallery is up to date by removing unnecessary thumbnails. But what about updating the index? Looking at the above code, you’ll notice that an array $new_idx is being used to store the current line of the index file whenever the filename exists in the reference string. After the completion of the code segment about $new_idx should be of the exact same format of the original index, except only contain those entries that did not get deleted. The next and final step in the AutoGallery checkup() function is to re-write the index file using only the information stored in this $new_idx variable as shown below: Code Flow
  • Open the index file using the fopen() command in write mode
  • Write every entry in the $new_idx array into the index file
  • Close the index file
$fp = fopen("{$this->path}/{$this->idxfile}", "w+" );  for($k = 0; $k < count($new_idx); $k++) {             fwrite($fp, trim($new_idx[$k])."n",                  strlen(trim($new_idx[$k])."n"));  }  fclose($fp); Once the file has been closed, both the thumbnails and index file are now completely up to date based on the original filenames of the images in your photo gallery. Until Next Week More to come next week! Now that you have a complete understanding of how the AutoGallery script maintains the images that exist in the photo gallery as well as (hopefully) a solid understanding of the index file your ready to go on to the remainder of the script!

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/.

Digg This! StumbleUpon This!
AddThis Social Bookmark Widget

News Tags: 3, Reference, Code, PHP
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/. cover

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
11 + 8 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.