Hudzilla.org - the homepage of Paul Hudson
Contents > Practical PHP > Creating thumbnails Wish List | Report Bug | About Me ]

22.4.2     Development

This is NOT the latest copy of this book; click here for the latest version.

Getting the same functionality from PHP code is also remarkably simple. Here is the same thing in PHP:

<?php
    
function recreate_thumbnail($filename) {
        
$src_img = imagecreatefromjpeg("fullsize/$filename");
        
$src_x = imagesx($src_img);
        
$src_y = imagesy($src_img);
        
$dest_x = $src_x / 1.5;
        
$dest_y = $src_y / 1.5;
        
        
$dst_img = imagecreatetruecolor($dest_y, $dest_y);
        
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_x, $dest_y, $src_x, $src_y);
        
imagejpeg($dst_img, "thumbnails/$filename", 100);
        
imagedestroy($src_img);
        
imagedestroy($dst_img);
    }

    function
scandir_strip($directory) {
        
$result = scandir($directory);
        
array_shift($result);
        
array_shift($result);
        return
$result;
    }

    
$thumbcount = file_get_contents('thumbcount.txt');
    
$filelist = scandir_strip('thumbnails');

    if (
$thumbcount != count($filelist)) {
        foreach(
$filelist as $file) {
            
unlink("thumbnails/$file");
        }
    
        
$filelist = scandir_strip('fullsize');
        print
'Regenerating thumbnails...<BR />';
        
array_filter($filelist, "recreate_thumbnail");
    }

    
$filelist = scandir_strip('thumbnails');
    foreach(
$filelist as $thumbnail) {
        print
"<IMG SRC=\"thumbnails/$thumbnail\">";
    }

    
file_put_contents('thumbcount.txt', count($filelist));
?>

If you are thinking "that isn't simple at all!" just hold on - when you break it down it really is nothing special. Firstly, a function is defined to handle thumbnail resizing - this uses almost exactly the same code as our previous thumbnail generation script, with the difference being that a variable filename is read in, and also the second parameter to imagejpeg() is used so that the result is saved to a file as opposed to being output to the screen.

Next up is a custom function called scandir_strip(), which is a small extension to the standard function scandir() with the difference that it strips off the first two values in the array - usually the "." and ".." directories, both of which we're not interested in. This is really just there to make the code shorter for printing - it will actually slow your script down to use it as a function.

From there on the code pretty much matches what you saw in the pseudocode. Note that the use of a function for making our thumbnails means we can use array_filter() to do the hard work for us.

Save that script as thumbgen.php, create a directory "fullsize", a directory "thumbnails", and an empty text file "thumbcount.txt". Now, fill the fullsize directory with some JPEG pictures, and try running the script - it should work as expected.

Now, what happens if you change the thumbnails regularly - perhaps you have a webcam, and you add a new picture to the archive every five minutes or so. In the current solution the whole thumbnail archive is deleted each time a file is added or deleted, which is inherently bad. A better solution is to compare the list of files in fullsize against the files in thumbnails, and generate any thumbnails that are missing/delete any thumbnails that no longer exist as full size pictures. An even better solution is to also check the last modified time of each full size picture and thumbnail, regenerating thumbnails if the original has been modified - see what you can come up with!





<< 22.4.1 Analysis   22.5 ASCII art >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
furmanorama@gmail.com - 07 Sep 2008

It`s not working for me :(

furmanorama@gmail.com - 07 Sep 2008

It`s not working for me :(

furmanorama@gmail.com - 07 Sep 2008

It`s not working for me :(

reywob@php.net - 07 Sep 2008

If you're disappointed with the quality of the thumbnails produced by the GD graphics library, PEAR's Image_Transform package provides an easy API to make thumbnails using the GD, Imagick or Imlib PHP extensions; or command-line ImageMagick or NetPBM.

http://pear.php.net/package/Image_Transform

A PHP User - 07 Sep 2008

Only that the function file_get_contents is only available between PHP 4 >= 4.3.0, PHP 5.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is one plus six?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow