Image Re sizer Class Code

/*
 This class usage is to resize images while preserving its original proportions.
 You can use it to resize the original image or to create a smaller copy of it (thumb).
 There are four parameters as you can see: filename, maximum width and maximum height.
 The 4rd one is an optional; if you want to create a copy (thumb) while preserving the original image file you need to pass the path of the new image.
 Please note that if you leave the 4rd parameter empty the original image will be overwritten.
 Please note also that this class is dealing only with jpg, gif and png formats and you have to grant the
 neccessery permissions in order to write the image file to the file system.

 Example usage:
 $imageResizer = new ImageResizer('./pics/vector_flower.jpg', 90, 90, './pics/vector_flower.jpg_small.jpg');
 Or if you want to overwrite the original file:
 $imageResizer = new ImageResizer('./pics/vector_flower.jpg', 90, 90);

 * ImageReizer - The ImageReizer class:
 * ImageReizer::__construct($fileName, $maxWidth, $maxHeight, $newFileName ='') - The constructor.
 * ImageReizer::getPath() - Get the image path.
 * ImageReizer::getNewPath() - Get the new path (thumb).
 *
 By Ran Argaman.
 http://www.ranweb.net
 */
class ImageResizer {
private $fileName;
private $maxWidth;
private $maxHeight;
private $width;
private $height;
private $newWidth;
private $newHeight;
private $percentage;
private $newFileName;
private $extension;

function __construct($fileName, $maxWidth, $maxHeight, $newFileName ='') {
//Setting properties.
$this -> fileName = $fileName;
$this -> maxWidth = $maxWidth;
$this -> maxHeight = $maxHeight;
//If there is a new file name.
if($newFileName != '')
$this -> newFileName = $newFileName;

//Check if the file exists.
if(!file_exists($this -> fileName)) {
throw new Exception('The file doesn\'t exist.');
} else {//Check if there is a need for resize.
list($this -> width, $this -> height) = getimagesize($this -> fileName);
if($this -> width < $this -> maxWidth && $this -> height < $this -> maxHeight) {
throw new Exception('The image is too small for this kind of resize.');
}
$this -> extension = strtolower(end(explode('.', $this -> fileName)));
$extensions = array('jpg', 'jpeg', 'gif', 'png');
if(!in_array(strtolower($this -> extension), $extensions)) {
throw new Exception('The file is not in a supported format.');
}
}

//If there are no errors.
if($this -> err == '') {
//Calculating precentage.
$this -> calculatePercentage();
//Doing the resize and overwrtiing or creating the file.
$this -> doResize();
}
}

private function calculatePercentage() {
$per1 = $this -> maxWidth / $this -> width;
$per2 = $this -> maxHeight / $this -> height;
$per = 1;

if($per1 < $per2)
$this -> percentage = $per1;
else
$this -> percentage = $per2;

}

private function doResize() {
$this -> newWidth = $this -> width * $this -> percentage;
$this -> newHeight = $this -> height * $this -> percentage;
$imagenew = imagecreatetruecolor($this -> newWidth , $this -> newHeight);
switch($this ->extension) {
case 'jpg' :

case 'jpeg' :
$imagetmp = imagecreatefromjpeg($this -> fileName);
break;
case 'gif' :
$imagetmp = imagecreatefromgif($this -> fileName);
break;
case 'png' :
$imagetmp = imagecreatefrompng($this -> fileName);
break;
}
imagecopyresampled($imagenew, $imagetmp, 0, 0, 0, 0, $this -> newWidth , $this -> newHeight, $this -> width, $this -> height);
if($this -> newFileName == '')
imagejpeg($imagenew, $this -> fileName, 100);
else
imagejpeg($imagenew, $this -> newFileName, 100);
}

public function getPath() {
return ($this -> fileName);
}

public function getNewPath() {
if($this -> newFileName == '') {
return ($this -> fileName);
} else {
return ($this -> newFileName);
}
}
public function getOriginalDimensions(){
return array('width' => $this->width, 'height' => $this ->height);
}
public function getNewDimensions(){
return array('width' => $this->newWidth, 'height' => $this ->newHeight);
}

public function __call($name, $args) {
throw new Exception('Undefined method ' . $name . '() called');
}

}