Upload and Re-size Images
<form role="form" method="post" action="uppropic.php" enctype="multipart/form-data">
<div class="box-body">
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" id="exampleInputFile" name="file">
<p class="help-block"></p>
</div>
</div><!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Upload</button>
</div>
</form>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<?php
define ("MAX_SIZE","4000");
$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 = pathinfo($image, PATHINFO_EXTENSION);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg")
&& ($extension != "png") && ($extension != "gif"))
{
echo ' Unknown Image extension ';
$errors=1;
}
else
{
$size=filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
echo "You have exceeded the size limit";
$errors=1;
}
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);
}
list($width,$height)=getimagesize($uploadedfile);
$newwidth=250;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=100;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
$newwidth2=50;
$newheight2=($height/$width)*$newwidth2;
$tmp2=imagecreatetruecolor($newwidth2,$newheight2);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,
$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,
$width,$height);
imagecopyresampled($tmp2,$src,0,0,0,0,$newwidth2,$newheight2,
$width,$height);
$uxi=rand(9,999);
//$cid=$uxi.$cid;
$mypphoto="propic-".$uxi.$cid.".".$extension;
$filename = "propic/".$mypphoto;
$filename1 = "propic/100/".$mypphoto;
$filename2 = "propic/50/".$mypphoto;
imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);
imagejpeg($tmp2,$filename2,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
imagedestroy($tmp2);
}
}
}
//If no errors registred, print the success message
if (($extension != "jpg") && ($extension != "jpeg")
&& ($extension != "png") && ($extension != "gif"))
{
header("location:./?error22");
}
else
{
$sql2="UPDATE users SET photo='$mypphoto' WHERE id = '$cid'";
$result2=mysql_query($sql2);
if($result2)
{
if($userphoto=="dpic.png")
{
$userphoto="";
}
else
{
if(file_exists("propic/$userphoto"))
{
unlink("propic/$userphoto");
}
if(file_exists("propic/50/$userphoto"))
{
unlink("propic/50/$userphoto");
}
if(file_exists("propic/100/$userphoto"))
{
unlink("propic/100/$userphoto");
}
}
header("location:./");
//header("location:./");
}
else
{
header("location:./?error");
//echo "Error...";
}
}
?>