Development

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!

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: ASCII art >>

Previous chapter: Analysis

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.