Resize image on upload php
Posted
by
blasteralfred
on Stack Overflow
See other posts from Stack Overflow
or by blasteralfred
Published on 2011-01-06T06:14:43Z
Indexed on
2011/01/06
10:53 UTC
Read the original article
Hit count: 233
Hi,
I have a php script for image upload as below
<?php
$LibID = $_POST[name];
define ("MAX_SIZE","10000");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
$image=$_FILES['image']['name'];
if ($image)
{
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg"))
{
echo '<h1>Unknown extension!</h1>';
$errors=1;
exit();
}
else
{
$size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
exit();
}
$image_name=$LibID.'.'.$extension;
$newname="uimages/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>image upload unsuccessfull!</h1>';
$errors=1;
exit();
}}}
?>
which uploads the image file to a folder "uimages" in the root. I have made changes in the html file for the compact display of the image by defining "max-height" and "max-width". But i want to resize the image file on upload. The image file may have a maximum width of 100px and maximum height of 150px. The image proportions must be constrained. That is, the image may be smaller than the above dimensions, but, it should not exceed the limit. How can I make this possible??
Thanks in advance :)
blasteralfred..
© Stack Overflow or respective owner