I don't recommend using the HTML width and height attributes for images, it's very low quality and you're wasting bandwith because the real file size is still bloated.
You can look at
http://www.sitepoint.com/forums/showthread.php?t=482679
or modify this example to suit your needs.
PHP Code:
<?php
function uploadImage($userfile,$userfile_name,$userfile_size,$userfile_type,$new_width,$new_height,$extension,$dir,$rel) {
// validate image properties
if (!$userfile_name)
{
$error = 1;
$errormessage = "No image uploaded.";
}
elseif (!$userfile_size)
{
$error = 1;
$errormessage = "There was a problem uploading this image.<br/>";
$errormessage .= "The image may be too large.";
}
elseif ($userfile_type != 'image/gif'&&$userfile_type != 'image/jpeg'&&$userfile_type != 'image/pjpeg'&&$userfile_type != 'image/png')
{
$error = 1;
$errormessage = "Images must be of type 'gif' or 'jpeg' or 'png'<br/>";
$errormessage .= "This image is of type ".$userfile_type.".";
}
// check for errors
if ($error)
{
echo "error uploading image<br><br>$errormessage";die();
}
//upload original image
$upfile = "$rel$dir".$userfile_name;
copy ($userfile, $upfile);
$userfile_array = explode(".",$userfile_name);
//resize image
if ($userfile_type == 'image/gif') {
$im = imagecreatefromgif($upfile);
} elseif ($userfile_type == 'image/png') {
$im = imagecreatefrompng($upfile);
} else {
$im = imagecreatefromjpeg($upfile);
}
// new width & height
$max_width = $new_width;
$max_height = $new_height;
// original width & height
$width = ImageSX($im);
$height = ImageSY($im);
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if (($width <= $max_width) && ($height <= $max_height)) {
$dst_width = $width;
$dst_height = $height;
} elseif (($x_ratio * $height) < $max_height) {
$dst_height = ceil($x_ratio * $height);
$dst_width = $max_width;
} else {
$dst_width = ceil($y_ratio * $width);
$dst_height = $max_height;
}
$dst = imagecreatetruecolor($dst_width, $dst_height);
ImageCopyResampled($dst, $im,
0,0,0,0,
$dst_width, $dst_height,
$width, $height);
$file = $dir.$userfile_array[0].$extension;
if ($userfile_type == 'image/gif')
{
$file = $file.'.gif';
ImageGIF($dst, $rel.$file, -1);
}
elseif ($userfile_type == 'image/png')
{
$file = $file.'.png';
//echo $file;die();
ImagePNG($dst, $rel.$file, -1);
}
else
{
$file = $file.'.jpeg';
ImageJPEG($dst, $rel.$file, -1);
}
ImageDestroy($im);
ImageDestroy($dst);
// delete original image
unlink($upfile);
return $file;
}
?>
thumbnail scripts, atleast the two that I know of are extremely simple to use, lightbox 2 looks great and i'd recommend it.
Bookmarks