The first part can be handled with a piece of code like the script below.
<?php
define(PHOTO_DIR, '.');
define(MAX_WIDTH, 120);
define(MAX_HEIGHT, 120);
$source_file = str_replace('..', '', $_SERVER['QUERY_STRING']);
$source_path = PHOTO_DIR . "/$source_file";
if ( file_exists( $source_path ) )
{
$source = imagecreatefromjpeg($source_path);
$width = imagesx($source);
$height = imagesy($source);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
$thumb_width = floor($scale*$width);
$thumb_height = floor($scale*$height);
$temp = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($temp, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
imagedestroy($source);
$source = $temp;
}
else
{
$source = imagecreate(MAX_WIDTH,MAX_HEIGHT);
imagecolorallocate($source,0,30,80);
}
header("Content-type: image/jpeg");
imagejpeg($source);
?>
That script will create a image that fits inside a specified box (max_width x max_height) from a given source image. You can probably modify it pretty easily do do what you'd like to do.