Class for processing image (resizing the image size to create thumb nails and small images from actual image without loosing the aspect ratio of the image)
This Class resizes the image without using any external library like Image magic. The aspect ratio of the image is maintained in the resized image. This maintains the quality of the image.
<?php
/**
**Image Processing class for resizing the image once the parameters are recorded. The image is resized but not saved yet.
**The image saving is done at the end where the object is created and called from
**Creating true color template for the new image to maintain the quality of the image
**/
class imageProcessing{
var $imageSizeX;
var $imageSizeY;
var $resizeX;
var $resizeY;
var $reduction;
var $fileName;
var $msg;
var $image;
var $imageType;
/**
** Class constructor to initialize the processing
**/
function imageProcessing($imgName,$red=300){
$this->reduction=$red;
if(is_file($imgName)){
if(file_exists($imgName)){
$this->fileName=$imgName;
$this->getSize();
$this->setSize();
print $this->imageSizeX."==>".$this->resizeX."<br>";
print $this->imageSizeY."==>".$this->resizeY."<br>";
$this->resizeIt();
$this->saveImage($imgName);
}else{
$this->errorState(0);
}
}else{
$this->errorState(2);
}
}
/**
** Function to set target size for the final image
** This size is based on the aspect ratio of the actual image to maintain the image clarity
**/
function setSize(){
if($this->imageSizeY<=$this->imageSizeX){
$ratio=$this->imageSizeY/$this->imageSizeX;
$this->resizeX=$this->reduction;
$this->resizeY=round($ratio*$this->reduction);
}else{
$ratio=$this->imageSizeX/$this->imageSizeY;
$this->resizeY=$this->reduction;
$this->resizeX=round($ratio*$this->reduction);
}
}
/**
** Function to identify the image and set the image quality for JPEG images
**
**/
function saveImage($imgName){
switch ($this->imageType){
case "gif":
imagegif($this->image,$imgName);
break;
case "jpg":
imagejpeg($this->image,$imgName,100);
break;
case "png":
imagepng($this->image,$imgName);
break;
}
}
/**
**Function for fetching the parameters of the image. Identifying image type.
**/
function getSize(){
$imgParams=getimagesize($this->fileName);
$this->imageSizeX=$imgParams[0];
$this->imageSizeY=$imgParams[1];
switch($imgParams[2]){
case 1:
$this->imageType="gif";
$this->image = imagecreatefromgif($this->fileName);
break;
case 2:
$this->imageType="jpg";
$this->image = imagecreatefromjpeg($this->fileName);
break;
case 3:
$this->imageType="png";
$this->image = imagecreatefrompng($this->fileName);
break;
default:
$this->errorState(1);
}
}
/**
**Function for resizing the image once the parameters are recorded. The image is resized but not saved yet.
**Creating true color template for the new image to maintain the quality of the image
**/
function resizeIt(){
/**
**Function for error and displaying error messages.
**/
function errorState($err){
switch ($err){
case 0:
$this->msg="File Not Found.";
break;
case 1:
$this->msg="Invalid File Type.";
break;
case 2:
$this->msg="Invalid Input.";
break;
case 3:
$this->msg="Could Not Save.";
break;
case 4:
$this->msg="Could not Create Image.";
break;
case 5:
$this->msg="Could not Create Blank File.";
break;
case 6:
$this->msg="Unable to Destroy Old File.";
break;
}
print $this->msg;
exit();
}
}
/***
** Object creation and processing the image
***/
$img=new imageProcessing("Copy of DSC00083.jpg"); // replace with the image name or the variable containing the file name
?>