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 :


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'];

$msg= "Resized";

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Girfa : Change Image size using PHP GD function</title>

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

<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>