Recent Posts

Tricks: Blogger: 05 How to Upload and Resize an Image with PHP



How to Upload and Resize an Image with PHP
I'm really going to show you small bit advance of Upload and Resize an Image with PHP now.Imagine should you need to allow users to upload image files than you have to think little bit about server's space and bandwidth and run a web site. For this particular purpose you have to do some optimization with image files. While uploading is one, picture resize. All major web site do it in exactly the same way. If a user uploads a 5mb image file they store on and resize it there server. It helps them reduce there funds and to seed up there website.

Picture cropping/resizing functionality certainly comes in handy if your website allows users to upload photographs. Furthermore, you also allow users to crop specific portions of uploaded picture and can create different variations of the same image.
Upload and Resize an Image with PHP

Upload and Resize an Image with PHP

Please add below scripts for Upload and Resize an Image with PHP
<?php define ("MAX_SIZE","400"); $errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST") { $image =$_FILES["file"]["name"]; $uploadedfile = $_FILES['file']['tmp_name']; if ($image) { $filename = stripslashes($_FILES['file']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg")
&& ($extension != "png") && ($extension != "gif")) {echo ' Unknown Image extension ';$errors=1; } else{ $size=filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE*1024){ echo "You have exceeded the size limit"; $errors=1;}
if($extension=="jpg" || $extension=="jpeg" ){$uploadedfile = $_FILES['file']['tmp_name'];$src = imagecreatefromjpeg($uploadedfile);}else if($extension=="png"){$uploadedfile = $_FILES['file']['tmp_name'];$src = imagecreatefrompng($uploadedfile);}else {$src = imagecreatefromgif($uploadedfile);}
list($width,$height)=getimagesize($uploadedfile); $newwidth=60;$newheight=($height/$width)*$newwidth;$tmp=imagecreatetruecolor($newwidth,$newheight); $newwidth1=25;$newheight1=($height/$width)*$newwidth1;$tmp1=imagecreatetruecolor($newwidth1,$newheight1); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight, $width,$height); imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1, $width,$height); $filename = "images/". $_FILES['file']['name'];$filename1 = "images/small". $_FILES['file']['name']; imagejpeg($tmp,$filename,100);imagejpeg($tmp1,$filename1,100); imagedestroy($src);imagedestroy($tmp);imagedestroy($tmp1);}}}//If no errors registred, print the success message if(isset($_POST['Submit']) && !$errors) { // mysql_query("update SQL statement "); echo "Image Uploaded Successfully!"; } ?>

Share on Google Plus

About Muhammad Shoaib Khan