Image upload and resize with php

There is a lot of things that we want to do with in less time and working properly. Lets take an example to resize an image, for doing this first of all we need an image edit software and then open that image and resize and at the last save. That will take a lot of time, I am not talking here about image editing software or how to edit an image. I am talking about how to resize an image with php. Obvisioly if you want to edit an image then Adobe Family’s products are far batter whose provide you a lot of functionality.

Now lets come to the point, Before starting let i tell you a bit about this image resizing class. First of all we need to verify an image means get the file extension and check whether it is an image or not. I allow only .jpeg, .jpg, .gif, .png files. After that we have to check the image size, then set the image width and height according to real image, so that the quality of an image remain same, means not stretch when we resize it. Then simply genrate a new image that’s it.

Here is the class named ‘imagegen.class.php’

class ImageResizerGen{

public $file;
public $max_size; // KB
public $image_width;
public $image_height;

function __construct( $file, $max_size='300', $img_w='200', $img_h='400' ){
$this->file = $file;
$this->max_size = $max_size;
$this->image_width = $img_w;
$this->image_height = $img_h;
}

function _verifyImage( $filename ){

$filename = strtolower($filename);
$extension = strrchr( $filename, '.' );
$auth_exten = array('.jpg', '.jpeg', '.png', '.gif');

if( in_array( $extension, $auth_exten ) ){
return true;
}
else{
return false;
}
}

function _checkImageSize( $filesize ){

// make size in KB
$devide = 1024;
$file_s = $filesize / $devide;
$file_s = round($file_s, 2);

if( $file_s < $this->max_size ){
return true;
}
else{
return false;
}
}

function _imageResizer( $width, $height, $new_width, $new_height ){

if( $width > $height ){
$new_width = $new_width;
$devide = $width / $new_width;
$new_height = floor( $height / $devide );
}
else{
$new_height = $new_height;
$devide = $height / $new_height;
$new_width = floor( $width / $devide );
}

$return = array($new_width, $new_height);
return $return;
}

function _imageGenrator(){

if( is_array($this->file) ){

$file = $this->file;

$filename = $file["my_file"]["name"];
$tmp_name = $file["my_file"]["tmp_name"];
//$file_type = $file["my_file"]["type"];
$file_size = $file["my_file"]["size"];

// Determine File Format
if( $this->_verifyImage($filename) === true ){

// Determine File Size
if( $this->_checkImageSize($file_size) === true ){

$extension = strtolower($filename);
$getExt = strrchr($extension, ".");

// For PNG File with transparnt quality
if( $getExt == '.png' ){
list($width, $height) = getimagesize($tmp_name);

# Get new Width and height from function
list($new_width, $new_height) = $this->_imageResizer( $width, $height, $this->image_width, $this->image_height );

$img = imagecreatetruecolor($new_width, $new_height);
imagealphablending($img, false);
imagesavealpha($img, true);

$new_img = imagecreatefrompng($tmp_name);
imagealphablending($new_img, true);

imagecopyresized($img, $new_img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

$filen = 'imgs/' . $filename;

if( imagepng($img, $filen) ){
echo "PNG Created Successfully!<br />";
imagedestroy($img);
}

}

// For JPG || JPEG File
if($getExt == ".jpg" || $getExt == ".jpeg"){
list($width, $height) = getimagesize($tmp_name);

# Get new Width and height from function
list($new_width, $new_height) = $this->_imageResizer( $width, $height, $this->image_width, $this->image_height );

$img = imagecreatetruecolor($new_width, $new_height);
$new_img = imagecreatefromjpeg($tmp_name);

imagecopyresized($img, $new_img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

$filen = 'imgs/' . $filename;

if( imagejpeg($img, $filen) ){
echo "Image Created Successfully!<br />";
imagedestroy($img);
}
}

// For GIF
if( $getExt == ".gif" ){

list($width, $height) = getimagesize($tmp_name);

# Get new Width and height from function
list($new_width, $new_height) = $this->_imageResizer( $width, $height, $this->image_width, $this->image_height );

$img = imagecreatetruecolor($new_width, $new_height);
$new_img = imagecreatefromgif($tmp_name);

imagecopyresized($img, $new_img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

$filen = 'imgs/' . $filename;

if( imagegif($img, $filen) ){
echo "Gif Created Successfully!<br />";
imagedestroy($img);
}

} // End Gif Section

} // end File Size
else{
echo "File Size Not Supported! <br/ >";
echo "File Size Error: " . round( ($file_size / 1024), 2) . " KB<br />";
}

}
else{
echo "File Not Supported!<br />";
}

}
}
}

 

Here you get the complete class not lets we talking a bit about the methods,

function _verifyImage( $filename ){

$filename = strtolower($filename);
$extension = strrchr( $filename, '.' );
$auth_exten = array('.jpg', '.jpeg', '.png', '.gif');

if( in_array( $extension, $auth_exten ) ){
return true;
}
else{
return false;
}
}

 

The method _verifyImage( $filename ) determine the file extension, whether matching to our extensions or not.

After that we have to determine the file size, if the file size is above to the max size then return false.

function _checkImageSize( $filesize ){

// make size in KB
$devide = 1024;
$file_s = $filesize / $devide;
$file_s = round($file_s, 2);

if( $file_s < $this->max_size ){
return true;
}
else{
return false;
}
}

 

After completing the above methods lets start to generate the new width and height without stretching an image.

function _imageResizer( $width, $height, $new_width, $new_height ){

if( $width > $height ){
$new_width = $new_width;
$devide = $width / $new_width;
$new_height = floor( $height / $devide );
}
else{
$new_height = $new_height;
$devide = $height / $new_height;
$new_width = floor( $width / $devide );
}

$return = array($new_width, $new_height);
return $return;
}

 

Then all then all the image creation logic that you will see on the top of the image. But have you noticed the ‘PNG’ creation part?

if( $getExt == '.png' ){
list($width, $height) = getimagesize($tmp_name);

# Get new Width and height from function
list($new_width, $new_height) = $this->_imageResizer( $width, $height, $this->image_width, $this->image_height );

$img = imagecreatetruecolor($new_width, $new_height);
imagealphablending($img, false);
imagesavealpha($img, true);

$new_img = imagecreatefrompng($tmp_name);
imagealphablending($new_img, true);

imagecopyresized($img, $new_img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

$filen = 'imgs/' . $filename;

if( imagepng($img, $filen) ){
echo "PNG Created Successfully!<br />";
imagedestroy($img);
}

}

 

this part provide the exact same image means if you have an transparent png file then return the exact transparent png file.

Now time to implement this class file, if you want to implement this file to ajax based image upload then you can did it. But i implement this with normal click upload.

include('imagegen.class.php');
if( isset($_POST['get_file']) ){
if( !empty($_FILES["my_file"]["name"]) ){

$imageGen = new ImageResizerGen($_FILES, '300', '400', '400');
$check_img = $imageGen->_imageGenrator();

if( $check_img ){
echo "<script>top.location.href='index.php';</script>";
}

}
else{
echo "Please Select an Image";
}

}

 

<html>
<body>
<table align="center" width="600" border="0" cellspacing="0" cellpadding="4">
<form action="" method="post" enctype="multipart/form-data">
<tr>
<td><label>Select An Image: </label></td>
<td><input type="file" name="my_file" /></td>
<td><input type="submit" name="get_file" value="Upload File" /></td>
</tr>
</form>
</table>
</body>
</html>