Upload and Resize image in PHP

This post will explain you “How To Save image with Thumbnail”. This tips/scripts will help you to create or save image with different size without losing quality of image.

Server Resources:
Required GD Library
imageMagic Library

Create a Form using HTML

<form action="" method="post" enctype="multipart/form-data" >
<div>
<label>Upload File:</label>
<input type="file" name="filename" />
<small>(jpeg,jpg,png)</small>
</div>
<div>
<input type="submit" name="Submit" value="upload" />
</div>
</form>

Check Form Submission

if (isset($_POST['Submit']) && !empty($_POST['Submit']) && !empty($_FILES['filename']) && is_array($_FILES['filename']) ) {

//Action Need to Perform after Form Submit
.........
........
} else{
//Error Form Doesn't submit
}

//Create An Instance of an upload class
$classObj = new Upload($_FILES['filename']);

//Destination of files
//original Images
$originalDestination = 'uploads';

//Thumbnails Images
$thumbnailDestination = 'uploads/thumbs';

//Image URLS
$originalPix = 'uploads';
$thumbnailPix = 'uploads/thumbs';

//Upload Original Image
$classObj->Process($originalDestination);

if ($classObj->processed) {
// everything was fine !
$suceesMessage .=  '<p>';
$suceesMessage .= '  <b>Original File uploaded with success</b><br />';
$suceesMessage .= '  <img src="'.$originalPix.'/' . $classObj->file_dst_name . '" />';
$info = getimagesize($classObj->file_dst_pathname);
$suceesMessage .= '  File: <a href="'.$originalPix.'/' . $classObj->file_dst_name . '">' . $classObj->file_dst_name . '</a><br/>';
$suceesMessage .= '   (' . $info['mime'] . ' - ' . $info[0] . ' x ' . $info[1] .' -  ' . round(filesize($classObj->file_dst_pathname)/256)/4 . 'KB)';
$suceesMessage .= '</p>';
} else {
// one error occured
$errorMessage .= '<p>';
$errorMessage .= '  <b>File not uploaded to the wanted location</b><br />';
$errorMessage .= '  Error: ' . $classObj->error . '';
$errorMessage .= '</p>';
}

//Upload Thumbnail

$classObj->image_resize         = true;

//SET Image Size
$classObj->image_x              = 300;
//$classObj->image_y              = 300;

//Save in ratio
$classObj->image_ratio_y        = true;

$classObj->process($thumbnailDestination);
if ($classObj->processed) {
// everything was fine !
$suceesMessage .=  '<p>';
$suceesMessage .= '  <b>Original File uploaded with success</b><br />';
$suceesMessage .= '  <img src="'.$thumbnailPix.'/' . $classObj->file_dst_name . '" />';
$info = getimagesize($classObj->file_dst_pathname);
$suceesMessage .= '  File: <a href="'.$thumbnailPix.'/' . $classObj->file_dst_name . '">' . $classObj->file_dst_name . '</a><br/>';
$suceesMessage .= '   (' . $info['mime'] . ' - ' . $info[0] . ' x ' . $info[1] .' -  ' . round(filesize($classObj->file_dst_pathname)/256)/4 . 'KB)';
$suceesMessage .= '</p>';
} else {
// one error occured
$errorMessage .= '<p>';
$errorMessage .= '  <b>File not uploaded to the wanted location</b><br />';
$errorMessage .= '  Error: ' . $classObj->error . '';
$errorMessage .= '</p>';
}

//Remove Temp Data
$classObj-> Clean();

Thats it!!