PHP Code:
<?php
if (isset($_POST["submit"]))
{
$uploadpath = "Images/";
$uploadpath = $uploadpath.basename($_FILES["imgfile"]["name"]);
if (!move_uploaded_file($_FILES["imgfile"]["tmp_name"], $uploadpath))
die("There was an error uploading the file, please try again!");
$image_name = "Images/".$_FILES["imgfile"]["name"];
list($width,$height) = getimagesize($image_name);
$new_image_name = "Images/thumb_".$_FILES["imgfile"]["name"];
if ($width > $height)
{
$ratio = (250/$width);
$new_width = round($width*$ratio);
$new_height = round($height*$ratio);
}
else
{
$ratio = (250/$height);
$new_width = round($width*$ratio);
$new_height = round($height*$ratio);
}
$image_p = imagecreatetruecolor($new_width,$new_height);
$img_ext = $_FILES['imgfile']['type'];
if (img_ext == "image/jpg" || img_ext == "image/jpeg") {
$image = imagecreatefromjpg($image_name);
} else if ($img_ext == "image/png") {
$image = imagecreatefrompng($image_name);
} else if ($img_ext == "image/gif") {
$image = imagecreatefromgif($image_name);
} else {
die('this is no valid image resource');
}
imagecopyresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height);
imagejpeg($image_p,$new_image_name,100);
echo("Image uploaded!");
}
echo <<<TEXT
<form method="post" enctype="multipart/form-data">
<h1>Image Uploading</h1>
Please select a picture to upload:<br />
<input type="file" name="imgfile" />
<br />
<input type="submit" name="submit" value="Add Image" />
</form>
TEXT;
?>