Resize Images In PHP After or With Upload (.jpg, .png, .gif)

If you are looking for a tutorial for resizing an image of three most popular formats: JPG, PNG, GIF, then this tutorial will help you..
If you are developing a website like flickr or fotolog.....
It will help you to save server space...

Tutorial goes as follows:




 if(isset($_POST['upload']))
 {
$title=$_POST['title'];
$description=$_POST['description'];
$max_filesize =262144; 
$file_path = 'images/';
$filename = $_FILES['file']['name'];
$filename = stripslashes($_FILES['file']['name']);
$extension=substr($filename, strrpos($filename,'.') + 1);//getting extension of the file (jpg, png, gif)
$extension=strtolower($extension);
$file_src=$file_path.$filename;
if($_FILES['file']['size'] > $max_filesize) // checking filesize
{
echo "The file you attempted to upload is too large.";
}
if ($filename) 
  {
 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) // cheking file extension
  {
echo " This file type is not allowed! ";
  }
else{
mysql_query("INSERT INTO photos (tilte,description) VALUES ('$title','$description')"); // inserting details in table
if (move_uploaded_file($_FILES['file']['tmp_name'], $file_src)) {  
  echo "file uploaded successfully!";
}



if($extension=="jpg" || $extension=="jpeg" )
{
$src = imagecreatefromjpeg($file_src); // creating image from jpg or jpeg
}
else if($extension=="png")
{
$src = imagecreatefrompng($file_src); // creating image from png


}
else 
{
$src = imagecreatefromgif($file_src); // creating image gif
}
list($width,$height)=getimagesize($file_src);
if($width>=800 and $height>=600) // checking image height and width
{
$newwidth=800; //seting new width
$newheight=600; // seting new height
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagejpeg($tmp,$file_src,100);
imagedestroy($src);
imagedestroy($tmp);
}
  }
  }
?>

No comments:

Post a Comment