Techumber
Home Blog Work

Live Image Resize Script With PHP

Published on September 11, 2012

There are many tools available for Editing image on windows or mac. Even free software available. In this Tutorial Let’s create online image editing tool for image resize. Let’s see how do i resize an image using simple PHP code. Take a look at the demo to know how the final code works. how do i resize an image With PHP

Demo

Download

How it works?

1)First we will take inputs(Image,New Height, New Width) from user. 2)We will pass these data to the php script. 3)Then well use PHP multimedia fucntions like imagecreatetruecolor(), imagecreatefromjpeg(),imagecreatefrompng(), imagecreatefromgif(),imagecopyresampled(),imagejpeg() to create new image with new Width and new Height. 4)Finally we will display new Image to the user or we can provide a download link to download the image. For this application we will create both form and php code on a single page.

index.php

<?php
//Execute only on POST request
if($_POST){
//functin to get image extenction
function getExt($img) {
$pos = strrpos($img,".");
if (!$pos)
return "null";
$len = strlen($img) - $ipos;
$ext = substr($img,$pos+1,$len);
return strtolower($ext);
}
//user input image file
$img = stripslashes($_FILES["img"]["name"]);
//temp image file name
$tempimg = $_FILES['img']['tmp_name'];
//Required new Width
$newWidth= stripslashes($_POST["wt"]);
//Required new Height
$newHeight= stripslashes($_POST["ht"]);
$err=null;
if($img){
$ext = getExt($img);
//Image validations
if ((!$ext)&&($ext!="jpg")&&($ext!="jpeg")&&($ext!="png")&&($ext!= "gif"))
$err="<strong>ho snap!</strong>please enter valid image jpg,jpeg,png or gif";
else if (filesize($tempimg)> 1024*1024)
$err="<strong>ho snap!</strong>File size must be less then 1mb";
else {
//Creating Image object based on extection
if($ext=="jpg" || $ext=="jpeg")
$source = imagecreatefromjpeg($tempimg);
else if($extension=="png")
$source = imagecreatefrompng($tempimg);
else
$source = imagecreatefromgif($tempimg);
}
//To get Orginal image height and width
list($width,$height)=getimagesize($tempimg);
//Creating Temprory blank Image PHP Code
$temp=imagecreatetruecolor($newWidth,$newHeight);
//Copying orginal image to Temproty blank image
imagecopyresampled($temp,$source,0,0,0,0,$newWidth,$newHeight,$width,$height);
$newfile="uploads/". $_FILES['img']['name'];
foreach( glob("uploads/*") as $file ) {
if( basename($file)!=$newfile)
unlink($file);
}
imagejpeg($temp,$newfile,100);
imagedestroy($source);
imagedestroy($temp);
}
else
$err='<strong>ho snap!</strong>Please select image';
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Image Resize Script with PHP-techumber.com</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<div class="container">
<div class="row">
<div class="logo">
<a href="http:/www.techumber.com">
<img src="img/logostd.png" alt="techumber.com logo"/>
</a>
</div>
</div>
<div class="row">
<div class="span6 offset3">
<div class="mini-layout">
<?php
if($err){
echo '<div class="alert alert-error">
'.$err.'
</div>';
}
//To disply newly created file.
if($newfile){
echo '<center><img src="'.$newfile.'"/></center>';
echo '<div class="alert alert-info">
Right click save as to save your image.
</div>';
}
?>
<!-- The form allow user to upload there image and new height,new width-->
<form class="form-horizontal" method="POST" action="" enctype="multipart/form-data">
<div class="control-group">
<label class="control-label" for="img">Choose File</label>
<div class="controls">
<input type="file" id="img" name="img" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputWidth">Width</label>
<div class="controls">
<div class="input-append">
<input type="text" class="span1" id="inputWidth" name="wt" placeholder="width" value=<?php if($newWidth) echo $newWidth; else echo "60"; ?> />
<span class="add-on">PX</span>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputHeight">Height</label>
<div class="controls">
<div class="input-append">
<input type="text" class="span1" id="inputHeight" name="ht" value=<?php if($newHeight) echo $newHeight; else echo "60"; ?> placeholder="Height">
<span class="add-on">PX</span>
</div>
</div>
</div>
<div class="controls">
<button id="btnSubmit" name="btnSubmit" class="btn btn-info" value="Resize">Resize</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>

It’s so simple script. So this is how do i resize an image using PHP script. Hope you like it.