Hello, I am trying to resize an image proportionally. I want the width to be 300 and the height to be proportional and then upload it onto my website. I found a script to resize it, however i still have problems uploading it to my site because the script was made to just save the image after you resize it. I have posted the script I found below. Thanks, Jason <?php if($_SERVER['REQUEST_METHOD'] == 'POST') { if($_FILES['image']) { preg_match('/\.([A-Za-z]+?)$/', $_FILES['image']['name'], $matches); $matches[1] = strtolower($matches[1]); if($matches[1] == 'png' && function_exists('imagecreatefrompng') || $matches[1] == 'jpg' && function_exists('imagecreatefromjpeg') || $matches[1] == 'jpeg' && function_exists('imagecreatefromjpeg') || $matches[1] == 'gif' && function_exists('imagecreatefromgif') || $matches[1] == 'bmp' && function_exists('imagecreatefromwbmp')) { list($owidth, $oheight) = getimagesize($_FILES['image']['tmp_name']); if($_POST['resizeby'] == 'height') { $nheight = ($_POST['size']>1)?$_POST['size']:600; $nwidth = $nheight / $oheight * $owidth; $resized = imagecreatetruecolor($nwidth, $nheight); } else { $nwidth = ($_POST['size']>1)?$_POST['size']:800; $nheight = $nwidth / $owidth * $oheight; $resized = imagecreatetruecolor($nwidth, $nheight); } if($matches[1] == 'png') $original = imagecreatefrompng($_FILES['image']['tmp_name']); if($matches[1] == 'jpg' || $matches[1] == 'jpeg') $original = imagecreatefromjpeg($_FILES['image']['tmp_name']); if($matches[1] == 'gif') $original = imagecreatefromgif($_FILES['image']['tmp_name']); if($matches[1] == 'bmp') $original = imagecreatefromwbmp($_FILES['image']['tmp_name']); imagecopyresampled($resized, $original, 0, 0, 0, 0, $nwidth, $nheight, $owidth, $oheight); header('Content-Disposition: attachment; filename="'.$_FILES['image']['name'].'"'); header('Content-type: image/'.(($matches[1] == 'jpg')?'jpeg':$matches[1])); if($matches[1] == 'png') imagepng($resized); if($matches[1] == 'jpg' || $matches[1] == 'jpeg') imagejpeg($resized); if($matches[1] == 'gif') imagegif($resized); if($matches[1] == 'bmpg') imagewbmp($resized); exit(); } else $error = 'File type not supported!'; } else $error = 'No image uploaded!'; } ?> <head> <title>Resize Image</title> </head> <body> <h1>Resize Image</h1> <?php if($error) print '<p>'.$error.'</p>'; ?> <form action="" method="post" enctype="multipart/form-data" > <p>Upload your JPG, JPEG, GIF, PNG or BMP image: <br /> <input type="file" name="image" /></p> <p>Choose a height or width you want your image to be: <br /> <select name="resizeby"> <option value="width" selected="true">Width:</option> <option value="height">Height:</option> </select> <input type="text" name="size" id="size" value="800" /> pixels</p> <p><input type="submit" value="Submit" id="submit" /></p> </form> </body> </html> Code (markup):
i changed it for you to get the idea. <?php $dir="./tmp"; $fname=$_FILES[image][name]; $fileout="$dir/$fname"; if($_SERVER['REQUEST_METHOD'] == 'POST') { if($_FILES['image']) { preg_match('/\.([A-Za-z]+?)$/', $_FILES['image']['name'], $matches); $matches[1] = strtolower($matches[1]); if($matches[1] == 'png' && function_exists('imagecreatefrompng') || $matches[1] == 'jpg' && function_exists('imagecreatefromjpeg') || $matches[1] == 'jpeg' && function_exists('imagecreatefromjpeg') || $matches[1] == 'gif' && function_exists('imagecreatefromgif') || $matches[1] == 'bmp' && function_exists('imagecreatefromwbmp')) { list($owidth, $oheight) = getimagesize($_FILES['image']['tmp_name']); if($_POST['resizeby'] == 'height') { $nheight = ($_POST['size']>1)?$_POST['size']:600; $nwidth = $nheight / $oheight * $owidth; $resized = imagecreatetruecolor($nwidth, $nheight); } else { $nwidth = ($_POST['size']>1)?$_POST['size']:800; $nheight = $nwidth / $owidth * $oheight; $resized = imagecreatetruecolor($nwidth, $nheight); } if($matches[1] == 'png') $original = imagecreatefrompng($_FILES['image']['tmp_name']); if($matches[1] == 'jpg' || $matches[1] == 'jpeg') $original = imagecreatefromjpeg($_FILES['image']['tmp_name']); if($matches[1] == 'gif') $original = imagecreatefromgif($_FILES['image']['tmp_name']); if($matches[1] == 'bmp') $original = imagecreatefromwbmp($_FILES['image']['tmp_name']); imagecopyresampled($resized, $original, 0, 0, 0, 0, $nwidth, $nheight, $owidth, $oheight); if($matches[1] == 'png') imagepng($resized,$fileout); if($matches[1] == 'jpg' || $matches[1] == 'jpeg') imagejpeg($resized,$fileout); if($matches[1] == 'gif') imagegif($resized,$fileout); if($matches[1] == 'bmpg') imagewbmp($resized,$fileout); echo"saved as $fileout"; exit(); } else $error = 'File type not supported!'; } else $error = 'No image uploaded!'; } ?> <head> <title>Resize Image</title> </head> <body> <h1>Resize Image</h1> <?php if($error) print '<p>'.$error.'</p>'; ?> <form action="" method="post" enctype="multipart/form-data" > <p>Upload your JPG, JPEG, GIF, PNG or BMP image: <br /> <input type="file" name="image" /></p> <p>Choose a height or width you want your image to be: <br /> <select name="resizeby"> <option value="width" selected="true">Width:</option> <option value="height">Height:</option> </select> <input type="text" name="size" id="size" value="800" /> pixels</p> <p><input type="submit" value="Submit" id="submit" /></p> </form> </body> </html> Code (markup):