The script needs the GD library to function very will.
upload.php:
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
$newwidth = $_POST[imgwidth];
$filename = $_FILES['uploadfile']['name'];
$uploadedfile = $_FILES['uploadfile']['tmp_name'];
$image_type = strstr($filename, '.');
switch($image_type) {
case '.jpg':$src = imagecreatefromjpeg($uploadedfile);break;
case '.png':$src = imagecreatefrompng($uploadedfile);break;
case '.gif':$src = imagecreatefromgif($uploadedfile);break;
default:echo("Error Invalid Image Type");die;break;
}
list($width,$height)=getimagesize($uploadedfile);
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = "images/". $_FILES['uploadfile']['name'];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
echo "<script>alert('Image is resized to 100 px width
successfully');window.location='upload.php?img=$filename';</script>";
}
?>
<table border="1" align="center" cellpadding="10" cellspacing="0"
style="font-family:arial;font-size:14;">
<form action="upload.php" method="post"
enctype="multipart/form-data">
<tr>
<td colspan="2" align="left"><strong>Image Resizing in
PHP</strong></td>
</tr>
<tr>
<td>New Width:</td>
<td><input name="imgwidth" type="text" value="100" size="6"></td>
</tr>
<tr>
<td>Dir Path:</td>
<td><input type="text" name="dirpath" value="images"
disabled></td>
</tr>
<tr>
<td>Upload</td>
<td><input type="file" name="uploadfile"></td>
</tr>
<tr align="center">
<td colspan="2"> <input name="submit" type="submit" value="Save">
</td>
</tr>
</form>
</table>
<center>
<? if(isset($_REQUEST[img])) {?>
Resized Image is <img src="<?=$_REQUEST[img]?>">
<? }?>
</center>