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.
How it works?
- First we will take inputs(Image,New Height, New Width) from user.
- We will pass these data to the php script.
- After that we will set new width and height acoording to user input which fits well to generate new image.
- Then we will use PHP multimedia fucntions like imagecreatetruecolor(), imagecreatefromjpeg(),imagecreatefrompng(), imagecreatefromgif(),imagecopyresampled(),imagejpeg() to create new image with new Width and new Height.
- 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.
<?php $err = $newfile = $newWidth = $newHeight = null; //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) - $pos; $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 { // Increase memory limit to support larger files ini_set('memory_limit', '32M'); //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 // Code by Vijay Rami Start // Proportionally resize the image to the // max sizes specified above $x_ratio = $newWidth / $width; $y_ratio = $newHeight / $height; if (($width <= $newWidth) && ($height <= $newHeight)) { $tn_width = $width; $tn_height = $height; } elseif (($x_ratio * $height) < $newHeight) { $tn_height = ceil($x_ratio * $height); $tn_width = $newWidth; } else { $tn_width = ceil($y_ratio * $width); $tn_height = $newHeight; } // Code by Vijay Rami Ends $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="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 and may be helpful to someone.
0 Comments