How to Resize and Upload an Image in PHP

0
513

uploadresizePicImage

In this tutorial, I will teach you how to resize and upload an image in php. .  This method can control the size of the images when uploading it on your website. It also optimize your web pages and lessen the load of time for a faster browsing experience.

Lets begin:

  1. Create the landing page of your project and name it “index.php”.  Copy  this following code and paste it into file.

[php]
<!– extension bootstrap css –>
<link rel=”stylesheet” type=”text/css” href=”css/bootstrap.min.css”>

<h1 align=”center”>Upload and Resize an Image</h1>

<form action=”upload.php” method=”POST” enctype=”multipart/form-data” >
<div class=”container”>
<div class=”row”>
<div class=”col-md-6″>
<div class=”col-md-12″>
<label class=”col-md-3″>Upload Image</label>
<input id=”photo” name=”photo” type=”file”>
</div>
<div class=”col-md-12″>
<label class=”col-md-3″>Choose size</label>
<select name=”size_option”>
<option>Small</option>
<option>Medium</option>
<option>Large</option>
</select>
<input type=”submit” id=”submit” name=”submit” value=”upload”>
</div>

</div>
</div>
<hr/>
<div class=”row”>
<div class=”col-md-12″>
<?php
if (isset($_GET[‘image’])) {
/* displaying images */

$small_path = “images/small/”.$_GET[‘image’];
$medium_path =”images/medium/”.$_GET[‘image’];
$large_path = “images/large/”.$_GET[‘image’];

echo “<img src='”.$small_path.”‘ alt=’small’/> “;
echo “<img src='”.$medium_path .”‘ alt=’medium’/> “;
echo “<img src='”.$large_path.”‘ alt=’large’/>”;
}
?>
</div>
</div>
</div>

</form>

[/php]

  1. Create a method that will resize and upload the image. Name it “upload.php”. Copy  this following code and paste it into file.

[php]

<?php

if(isset($_POST[‘submit’])){

/* set the name of the file */
$file_name = $_FILES[‘photo’][‘name’];
/* set the size of the file */
$file_type = $_FILES[‘photo’][‘type’];

/* Getting the original file size */
list($orig_width, $orig_height) = getimagesize($_FILES[‘photo’][‘tmp_name’]);
if ($_POST[‘size_option’]==’Small’) {
/* set the image path*/
$path = ‘images/small/’.$file_name;
$n_width=100;
$n_height=100;

}elseif ($_POST[‘size_option’]==’Medium’) {
/* set the image path*/
$path = ‘images/medium/’.$file_name;
$n_width=200;
$n_height=200;

}elseif ($_POST[‘size_option’]==’Large’) {
/* set the image path*/
$path = ‘images/large/’.$file_name;
$n_width=300;
$n_height=300;

}

/* Calculate the new size of an image */

if ($orig_height < $orig_width){
// *** Image to be resized is wider (landscape)

$orig_ratio = $orig_height / $orig_width;
$n_height = $n_width * $orig_ratio;
}elseif ($orig_height > $orig_width){
// *** Image to be resized is taller (portrait)

$orig_ratio = $orig_width / $orig_height ;
$n_width = $n_height * $orig_ratio;
}else {
// *** Image to be resized is a square

if ($n_height < $n_width) {

$orig_ratio = $orig_height / $orig_width;
$n_height = $n_width * $orig_ratio;
} else if ($n_height > $n_width) {
$orig_ratio = $orig_width / $orig_height ;
$n_width = $n_height * $orig_ratio;
}

}

/* Getting the binary data from the image */
$img_String = file_get_contents($_FILES[‘photo’][‘tmp_name’]);
/* create an image from the string */
$create_image = imagecreatefromstring($img_String);

$tmp = imagecreatetruecolor($n_width, $n_height);
/* copy and resize the image */
imagecopyresampled($tmp, $create_image, 0, 0, 0, 0, $n_width, $n_height, $orig_width, $orig_height);
/* cheking the file extension*/
switch ($file_type) {

case ‘image/jpeg’:
/* save the image in the corresponding path*/
imagejpeg($tmp, $path , 100);
break;
case ‘image/png’:
/* save the image in the corresponding path*/
imagepng($tmp, $path ,0);

break;
case ‘image/gif’:
/* save the image in the corresponding path*/
imagegif($tmp, $path ,0);
break;

default:
/* save the image in the corresponding path*/
imagejpeg($tmp, $path , 100);
break;
}
/* destroy the raw image */
imagedestroy($create_image);
imagedestroy($tmp);

/* return to the index */
header(‘Location: index.php?image=’.$file_name);
exit;

}
?>

[/php]

 

For all students who need programmer for your thesis system or anyone who needs a sourcecode in any programming languages.  You can contact me @ :

Email – [email protected]

Mobile No. – 09305235027 – tnt

 

Dowload the sourcecode here

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.