This function does it for me:
function resizeIt($tmpname, $names=array(), $dest=array(), $max=array())
{
// Has to be uploaded file already, so let's get to it.
$o_dims = getimagesize($tmpname);
// $o_dims[0] = image width
// $o_dims[1] = image height
if($o_dims === false || $o_dims[2] != 2)
die('There was an error with the image.');
$o_img = imagecreatefromjpeg($tmpname);
$tall = ($o_dims[1] >= $o_dims[0]);
if(empty($max))
{
$max['thumb'][0] = 250;
$max['thumb'][1] = 500;
$max['view'][0] = 500;
$max['view'][1] = 800;
}
// Define the new proportionate sizes:
$tempsize = ceil($o_dims[1]*$max['thumb'][0]/$o_dims[0]);
if($tall)
{
if($tempsize > $max['thumb'][0])
{
$thumb[0] = $max['thumb'][0];
$thumb[1] = $tempsize;
}
else
{
$thumb[0] = $tempsize;
$thumb[1] = $max['thumb'][1];
}
}
else
{
$thumb[0] = $max['thumb'][0];
$thumb[1] = $tempsize;
}
$tempsize = ceil($o_dims[1]*$max['view'][0]/$o_dims[0]);
if($tall)
{
if($tempsize > $max['view'][0])
{
$view[0] = $max['view'][0];
$view[1] = $tempsize;
}
else
{
$view[0] = $tempsize;
$view[1] = $max['view'][1];
}
}
else
{
$view[0] = $max['view'][0];
$view[1] = $tempsize;
}
// now, just cretae the images ;)
$img_thumb = imagecreatetruecolor($thumb[0], $thumb[1]);
$img_view = imagecreatetruecolor($view[0], $view[1]);
// Copy the old to the new
if(imagecopyresampled($img_thumb, $o_img, 0,0,0,0, $thumb[0], $thumb[1], $o_dims[0], $o_dims[1]) === false)
die('Image resizing for thumbnail failed.');
if(imagecopyresampled($img_view, $o_img, 0,0,0,0, $view[0], $view[1], $o_dims[0], $o_dims[1]) === false)
die('Image resizing for full-view failed.');
// Now to write the images permanently:
if(empty($dest))
$dest = array(
'thumb' => 'images/thumb/',
'view' => 'images/view/',
);
if(empty($names))
$names = array(
'thumb' => 'thumbnail_'.date('H.i.s_m.d.Y').'.jpg',
'view' => 'view_'.date('H.i.s_m.d.Y').'.jpg',
);
$new_thumb = $dest['thumb'].$names['thumb'];
$new_view = $dest['view'].$names['view'];
if(@imagejpeg($img_thumb, $new_thumb, 80)===false)
die('Image saving for thumbnail failed.');
if(@imagejpeg($img_view, $new_view, 80)===false)
die('Image saving for full-view failed.');
// Show our new images:
echo '
<img src="'.$new_thumb.'" alt="Thumbnail" />
<img src="'.$new_view.'" alt="Full-View" />';
}
You'd call it like so:
resizeIt($_FILES['photo']['tmp_name'], array('thumb' => 'thumb_123.jpg', 'view' => 'view_123.jpg'), array('thumb' => 'images/thumbs/', 'view' => 'images/view/'), array('thumb' => array(250, 500), 'view' => array(500, 800)));
// OR ALTERNATIVELY
$names = array('thumb' => 'thumb_'.date('H.i.s_m.d.Y').'.jpg',
'view' => 'view_'.date('H.i.s_m.d.Y').'.jpg');
$dest = array('thumb' => 'images/thumbs/',
'view' => 'images/view/');
$max = array(
'thumb' => array(250, 500),
'view' => array(500, 800)
);
resizeIt($_FILES['photo']['tmp_name'], $names, $dest, $max);