Monday, 15 April 2013

Resize Image or Crop image Using GD Library in Php | PHP Tutorials for Beginners

Image resize or image cropping is one of the important part during any website building. To make our uploaded image fix exactly in same diamention in front end we need to resize or crop that image.
In this tutorial of Php code for beginners we will show you the example of code which resize the uploaded image using GD library.


This is the html code for uploading the image for resizing. After selecting image you will click on submit button. When submit button will click our next code will check for image and prosess them accordingly.
<html>
<head>
<title>Resize image in php</title>
</head>
<body>
 <form name = "form" action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST" enctype="multipart/form-data">
<table>
<tr>
<td><label>Insert Image to resize:-</label></td>
<td><input type = "file" name = "file" id = "file"></td>
</tr>
<tr>
<td colspan = 2>
<input type = "submit" value = "submit" name = "submit">
</td>
</tr>
</table>
 </form>
</body>
</html>

Now when you click on submit our php code check whether submit button is clicked or not? As soon as he find submit is clicked the following code run to prosses the image for resizing. 
<?php
if(isset($_POST['submit'])){
$error = 0;
$uploadfile = 'images/'.basename($_FILES['file']['name']);
// $uploadfile is directory name where original image will save. You can unlink it after resizing the image.

$resize =  'resize/';
//It is you resize image directory.

$file_name = basename($_FILES['file']['name']);
$filecheck = basename($_FILES['file']['name']);
$ext = strtolower(substr($filecheck, strrpos($filecheck, '.') + 1));
if (!(($ext == "jpg" || $ext == "gif" || $ext == "png") && ($_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/png") && ($_FILES["file"]["size"] < 2120000))){
echo "File Not Supported";
$error = 1;
die();
}
// In above code we are checking the type and size of image. If it is not image file the error messege will appear.

if($_FILES['file']['name']!="")
{
ini_set('memory_limit','32M');
//Here we set the memory limit.

if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) 
{
echo $message= "Error in uploading a file due to the size of image.\n";
$error = 1;
}
else 
{
$info = pathinfo($uploadfile);
$img = imagecreatefromjpeg( $uploadfile );
$width = imagesx( $img );
$height = imagesy( $img );
$new_width = 100; //New Width for Image.
$new_height = 100; //New Height for Image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
imagejpeg( $tmp_img, "{$resize}{$file_name}" );
// All processing is done and your resized image will store from temporary directory to your resize image directory
}
}
if($error==0) 
{
  echo "Image Uploaded Successfully.";
  exit;
}
}    
?>

Here is the Complete code for you to resize you image according to the height width set by you.
<?php
if(isset($_POST['submit'])){
$error = 0;
$uploadfile = 'images/'.basename($_FILES['file']['name']);
$resize =  'resize/';
$file_name = basename($_FILES['file']['name']);
$filecheck = basename($_FILES['file']['name']);
$ext = strtolower(substr($filecheck, strrpos($filecheck, '.') + 1));
if (!(($ext == "jpg" || $ext == "gif" || $ext == "png") && ($_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/png") && ($_FILES["file"]["size"] < 2120000))){
echo "File Not Supported";
$error = 1;
die();
}

if($_FILES['file']['name']!="")
{
ini_set('memory_limit','32M');

if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) 
{
echo $message= "Error in uploading a file due to the size of image.\n";
$error = 1;
}
else 
{
$info = pathinfo($uploadfile);
$img = imagecreatefromjpeg( $uploadfile );
$width = imagesx( $img );
$height = imagesy( $img );
$new_width = 100; 
$new_height = 100;
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height,     $width, $height );
imagejpeg( $tmp_img, "{$resize}{$file_name}" );
}
}
if($error==0) 
{
  echo "Image Uploaded Successfully.";
  exit;
}
}    
?>
<html>
<head>
<title>Resize image in php</title>
</head>
<body>
 <form name = "form" action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST" enctype="multipart/form-data">
<table>
<tr>
<td><label>Insert Image to resize:-</label></td>
<td><input type = "file" name = "file" id = "file"></td>
</tr>
<tr>
<td colspan = 2>
<input type = "submit" value = "submit" name = "submit">
</td>
</tr>
</table>
 </form>
</body>
</html>

Hope this php tutorial is useful for you. Keep following www.phpcodeforbeginner.blogspot.in for more help.

4 comments:

  1. Thanks for this useful post. For more visit: examanswer24.com

    ReplyDelete
  2. hi madam. iam beginer in php development . ineed php datagrid pls help me

    ReplyDelete
  3. Its been good to read..I like the way that you have explained..
    best dissertation topics

    ReplyDelete
  4. Thanks for the code, we used the script for our VIRTUAL GIRLFRIEND website

    ReplyDelete