Location: PHPKode > scripts > pfpFileTree > example5.php
<?php

/**
 * example5.php
 *
 * apply a callback function to all the files in a tree 
 * this example creates thumbnails for every jpeg image (which doesn't
 * already have one)
 */

require_once('pfpFileTree.inc.php');

$src='/var/www/html/';

$t=new pfpFileTree($src);

$t->readTree(array('name'=>'{*.jpg,*.jpeg}','type'=>'f','r'=>true));
$t->applyCallBack('cb',null);
$t->readTree(array('name'=>'{*.jpg,*.jpeg}','type'=>'f','r'=>true));

$t->ls();

function cb($filename, $attrs, $additional)
{
	// create a directory called thumbnails in every
	// directory - and populate with smaller verions
	// of the jpeg files
	$size=128;
	$currentdir=dirname($filename);
	if (basename($currentdir)!='thumbnails') {
		// because we don't want to make thumbnails of the thumbnails!
		if (!is_dir($currentdir . '/thumbnails')) {
			if (!mkdir($currentdir . '/thumbnails')) {
				// returning false wold halt progress
				print "unable to create thumbnails in $currentdir\n";
				return true;
			}
		} else {
			if (filemtime($filename)
				<filemtime($currentdir . '/thumbnails/' . basename($filename))) {
				// thumbnail is newer
				return true;
			}
		}
		list($width, $height) = getimagesize($filename);
		$divisor=max($width/$size, $height/$size);
		if ($divisor<1) $divisor=1;
		$img=imagecreatefromjpeg($filename);
		$thumb=imagecreatetruecolor(
			(integer)($width/$divisor), (integer)($height/$divisor));
		imagecopyresampled($thumb, $img, 
			0, 0, 0, 0, 
			(integer)($width/$divisor),(integer)($height/$divisor), 
			$width, $height);
		imagejpeg($thumb, $currentdir . '/thumbnails/' . basename($filename));
		imagedestroy($thumb);
		imagedestroy($img);
	}
	return true;
}
Return current item: pfpFileTree