Saturday, 30 April 2016

Resized and Saved Uploaded Image in PHP

Image resizing is an important task because if uploaded image size is bigger than requirement and we are not resizing it then it will consume server space and bandwidth which is not good.
So Resizing is necessary .you need to use four PHP GD functions for achieve this.

Full Source Code :


<?php 


$msg="";
if(isset($_POST['submit']))
{
list($width, $height) = getimagesize($_FILES['file']['tmp_name']);
$thumb = imagecreatetruecolor(300, 200); //Add your custom image size
$source = imagecreatefromjpeg($_FILES['file']['tmp_name']);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, 300, 200, $width, $height);
$filename = "small/". $_FILES['file']['name'];

imagejpeg($thumb,$filename,100);
$msg= "Resized";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Girfa : Change Image size using PHP GD function</title>
</head>

<body>
<h1 align="center">Image Resize Demo Using PHP GD Functions</h1><hr /><br />

<center>
<form action=<?php echo $_SERVER['PHP_SELF']?> enctype="multipart/form-data" method="post">
Select Image : <input type="file" name="f1" /><br /><br />

<input type="submit" value="Upload" name="submit" />
<h3 style="color:red"><i><?php echo $msg;?></i></h3>
</form>
</center>
</body>

No comments:

Post a Comment