Here is a tested example. I didn't create this and I don't remember where I found it on the web. I tweaked it a little for my needs. I included the form I used for a point of reference.
<form name="myForm" action="uploadpic.php" method="post" enctype="multipart/form-data" />
<input name="userfile" type="file" size="40" />
<input type="submit">
</form>
Here is the contents of uploadpic.php
function resizeImage($originalImage,$toWidth,$toHeight){ //BEGINNING OF RESIZE FUNCTION//
// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
$xscale=$width/$toWidth;
$yscale=$height/$toHeight;
// Recalculate new size with default ratio
if ($yscale>$xscale){
$new_width = round($width * (1/$yscale));
$new_height = round($height * (1/$yscale));
}
else {
$new_width = round($width * (1/$xscale));
$new_height = round($height * (1/$xscale));
}
// Resize the original image
$imageResized = imagecreatetruecolor($new_width, $new_height);
if (preg_match("/.jpg/i", "$originalImage")) {
$imageTmp = imagecreatefromjpeg ($originalImage);
} elseif (preg_match("/.png/i", "$originalImage")) {
$imageTmp = imagecreatefrompng ($originalImage);
} elseif (preg_match("/.gif/i", "$originalImage")) {
$imageTmp = imagecreatefromgif ($originalImage);
}
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$newfilename = "resize_".$_FILES['userfile']['name'];
$path = "images/" .$newfilename;
imagejpeg ($imageResized, $path);
unlink($originalImage); // deletes original file
} //END OF RESIZE FUNCTION//
@extract($_POST);
if (@is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
$filetype = $_FILES['userfile']['type'];
$filesize = $_FILES['userfile']['size'];
$tmpfilename = $_FILES['userfile']['tmp_name'];
move_uploaded_file ($_FILES['userfile']['tmp_name'], "images/" . $_FILES['userfile']['name']) or die ("Move failed" . $filename);
$path = "images/" . $_FILES['userfile']['name'];
chmod($path, 0777);
resizeImage($path,300,300);
}
?>
<img src="images/resize_<? echo $_FILES['userfile']['name'] ?>">
Edited by bandbo_chad on 05/31/07 06:50 AM (server time).