This tutorial tell you how to upload and resize image for thumbnail in php.
Just copy the code bellow and save it as "resize_image.php" then open it in your server e.g. localhost.
<?php
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// By using the global PHP $_FILES array you can upload files from a client computer to the remote server.
$image = $_FILES["file"]["name"];
//get the name of the temporary copy of the file stored on the server.
$uploadedfile = $_FILES['file']['tmp_name'];
//get the type of the uploaded file.
$image_type =$_FILES["file"]["type"];
//imagecreatefrompng — Create a new image from file or URL because we are not sure user inputed file is png or with an other extention therefore we will use if else condition.
if($image_type=='image/png' || $image_type=='image/x-png')
{
$src = imagecreatefrompng($uploadedfile);
}
elseif($image_type=='image/gif')
{
$src = imagecreatefromgif($uploadedfile);
}
elseif($image_type=='image/jpeg' || $image_type=='image/jpg' || $image_type == 'image/pjpeg')
{
$src = imagecreatefromjpeg($uploadedfile);
}
//getimagesize() this function — Get the size of an image which user inputed. we need orignal size for resizing the image in "imagecopyresampled" function.
list($width,$height)=getimagesize($uploadedfile);
//We have get orignal height and width by using list($width,$height)=getimagesize($uploadedfile);
//Now set your own width and height which you want.
$new_width=100;
$new_height=100;
// Bellow function Create a new true color image so that quality of your image don't change.
$image_p=imagecreatetruecolor($new_width,$new_height);
// Turn off alpha blending and set alpha flag // otherwise png's images background color will be black.
imagealphablending($image_p, false);
imagesavealpha($image_p, true);
//Fucntion bellow will resize image
imagecopyresampled($image_p,$src,0,0,0,0,$new_width,$new_height,$width,$height);
$filename = "images/". $_FILES['file']['name'];
if(move_uploaded_file($_FILES["file"]["tmp_name"], $filename))
{
if($image_type=='image/png' || $image_type=='image/x-png')
imagepng($image_p,$filename,9) ;
else
imagejpeg($image_p,$filename,100);
echo 'Image uploaded';
imagedestroy($image_p);
//file has uploaded successfully, store its name in data base. see this link http://b2atutorials.blogspot.com/2012/05/upload-image-into-folder-and-save-its.html
}
}
?>
<html>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="submit">
</form>
</body>
</html>
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// By using the global PHP $_FILES array you can upload files from a client computer to the remote server.
$image = $_FILES["file"]["name"];
//get the name of the temporary copy of the file stored on the server.
$uploadedfile = $_FILES['file']['tmp_name'];
//get the type of the uploaded file.
$image_type =$_FILES["file"]["type"];
//imagecreatefrompng — Create a new image from file or URL because we are not sure user inputed file is png or with an other extention therefore we will use if else condition.
if($image_type=='image/png' || $image_type=='image/x-png')
{
$src = imagecreatefrompng($uploadedfile);
}
elseif($image_type=='image/gif')
{
$src = imagecreatefromgif($uploadedfile);
}
elseif($image_type=='image/jpeg' || $image_type=='image/jpg' || $image_type == 'image/pjpeg')
{
$src = imagecreatefromjpeg($uploadedfile);
}
//getimagesize() this function — Get the size of an image which user inputed. we need orignal size for resizing the image in "imagecopyresampled" function.
list($width,$height)=getimagesize($uploadedfile);
//We have get orignal height and width by using list($width,$height)=getimagesize($uploadedfile);
//Now set your own width and height which you want.
$new_width=100;
$new_height=100;
// Bellow function Create a new true color image so that quality of your image don't change.
$image_p=imagecreatetruecolor($new_width,$new_height);
// Turn off alpha blending and set alpha flag // otherwise png's images background color will be black.
imagealphablending($image_p, false);
imagesavealpha($image_p, true);
//Fucntion bellow will resize image
imagecopyresampled($image_p,$src,0,0,0,0,$new_width,$new_height,$width,$height);
$filename = "images/". $_FILES['file']['name'];
if(move_uploaded_file($_FILES["file"]["tmp_name"], $filename))
{
if($image_type=='image/png' || $image_type=='image/x-png')
imagepng($image_p,$filename,9) ;
else
imagejpeg($image_p,$filename,100);
echo 'Image uploaded';
imagedestroy($image_p);
//file has uploaded successfully, store its name in data base. see this link http://b2atutorials.blogspot.com/2012/05/upload-image-into-folder-and-save-its.html
}
}
?>
<html>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="submit">
</form>
</body>
</html>
thanks ............
ReplyDeleteVery nice.This is very useful to me ya!!
ReplyDelete