Image Upload


This is a simple way to upload pictures using PHP.While uploading the images can be resized.

<?php
error_reporting(0);

$change="";

//set the maximum size of the images can be uploaded
define ("MAX_SIZE","400");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i)
{
return "";
}
$l = strlen($str) – $i;
$ext = substr($str,$i 1,$l);
return $ext;
}

$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$image =$_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];

if ($image)
{
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
//set the file types which are acceptable
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")&& ($extension != "bmp")&& ($extension != "TIFF"))
{
$change='Unknown Image Extension';
$errors=1;
}
else
{
$size=filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
$change='You have exceeded the size limit!';
$errors=1;
}
else
{

if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}

echo $scr;
list($width,$height)=getimagesize($uploadedfile);

//set the file size according to your choice
$newwidth=100;
$newheight= 80;
$tmp=imagecreatetruecolor($newwidth,$newheight);

//the original image
$originalWidth=$width;
$newheight1=$height;
$tmp1=imagecreatetruecolor($originalWidth,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$originalWidth,$originalHeight1,$width,$height);

//set the name of the file
$filename = "small_". $_FILES['file']['name'];
$filename1 = $_FILES['file']['name'];

imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
}
}

//If no errors registred, print the success
if(isset($_POST['Submit']) && !$errors)
{
$change='Image Uploaded Successfully!';
}

?>

HTML Code

<html>
<head>
<title>Picture Upload</title>
</head>
<body>

<table border="0" cellspacing="3" cellpadding="15" align="center">
<tbody>
<form method="post" action="" enctype="multipart/form-data" name="form">
<tr>
<td><?php echo $change; ?> </td>
</tr>
<tr>
<td><img src="<?php echo $filename; ?>" /> &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;</td>
</tr>
<tr>
<td> <input size="25" name="file" type="file" /></td>
</tr>

<tr>
<td>Image maximum size&nbsp;&nbsp;<b>400 </b>&nbsp;&nbsp;kb</td>
</tr>
<tr>
<td><input type="submit" value="Upload" name="Submit"/></td>
</tr>
</tbody>
</form>
</table>

</body>
</html>

Advertisements
Published in: on November 6, 2009 at 5:39 am  Leave a Comment  

The URI to TrackBack this entry is: https://nimesha1984.wordpress.com/2009/11/06/image-upload-2/trackback/

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: