<?php
define('LARGE', 'large/');
define('MEDIUM', 'medium/');
define('SMALL', 'small/');
$processed = 0;
$skipped = 0;
$errors = 0;
function resize($im, $destination, $width, $height, $max) {
	if($width > $max) {
		$ratio = $width / $height;
		$newwidth = $max;
		$newheight = $max / $ratio;
		$new = imagecreatetruecolor($newwidth, $newheight);
		imagecopyresampled($new, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
		imagejpeg($new, $destination);
		imagedestroy($new);
	} else {
		imagejpeg($im, $destination);
	}
}
$jpgfiles = glob(LARGE . '*.jpg');
foreach($jpgfiles as $file) {
	if(!file_exists(MEDIUM . basename($file))) {
		$im = imagecreatefromjpeg($file);
		if($im) {
			$width = imagesx($im);
			$height = imagesy($im);
			resize($im, MEDIUM . basename($file), $width, $height, 640);
			resize($im, SMALL . basename($file), $width, $height, 200);
			imagedestroy($im);
			$processed++;
		} else {
			$errors++;
		}
	} else {
		$skipped++;
	}
}
echo "$processed files processed.<br />$skipped files skipped.<br />unable to process $errors files.";
?>