PHP Image Resizer Class

class imageResizer {
var $newImage;

function getSquareImage($size, $image) {
//getting the path and resetting to the new path of the image
$oldPath = explode(‘/’, $image);
$t = sizeof($oldPath) – 1;
$i = 0;
while ($i < $t) {
$newPath = $newPath . $oldPath[$i] . ‘/’;
$i++;
}
$filename = str_replace(‘.gif’, ”, end($oldPath));
$filename = str_replace(‘.jpg’, ”, $filename);
$filename = str_replace(‘.png’, ”, $filename);
$filename = str_replace(‘.JPG’, ”, $filename);
$filename = str_replace(‘.jpeg’, ”, $filename);
$newPath = $newPath . ‘cache/’ . $filename . ‘_sq’. $size .’.jpg’;

//if that image does not exist already (we haven’t resized it before):
if(!is_file($newPath)) {
list($width, $height) = getimagesize($image);

//check if the image has the right propotions or do:
if ($width > $size || $height > $size) {
if ($width > $height) $ratio = $size / $width;
else $ratio = $size / $height;

$new_width = round($width * $ratio);
$new_height = round($height * $ratio);

$newImg = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg($image);
imagecopyresampled($newImg, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($newImg, $newPath);

$this -> newImage = $newPath;
}
//If the original image is the correct size:
else $this -> newImage = $image;
}
//set this orignal imaget the image returned:
else  $this -> newImage =  $newPath;
return $this -> newImage;
}

function getImageDimensions($sizeX, $sizeY, $image) {
//getting the path and resetting to the new path of the image
$oldPath = explode(‘/’, $image);
$t = sizeof($oldPath) – 1;
$i = 0;
while ($i < $t) {
$newPath = $newPath . $oldPath[$i] . ‘/’;
$i++;
}
$filename = str_replace(‘.gif’, ”, end($oldPath));
$filename = str_replace(‘.jpg’, ”, $filename);
$filename = str_replace(‘.png’, ”, $filename);
$filename = str_replace(‘.JPG’, ”, $filename);
$filename = str_replace(‘.jpeg’, ”, $filename);
$newPath = $newPath . ‘cache/’ . $filename . ‘_’. $sizeX .’x’. $sizeY .’.jpg’;

//if that image does not exist already (we haven’t resized it befoe):
if(!is_file($newPath)) {
list($width, $height) = getimagesize($image);

//check if the image has the right propotions or do:
if ($width > $sizeX || $height > $sizeY) {
if ($width > $height) $ratio = $sizeX / $width;
else $ratio = $sizeY / $height;

$new_width = round($width * $ratio);
$new_height = round($height * $ratio);

$newImg = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg($image);
imagecopyresampled($newImg, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($newImg, $newPath);

$this -> newImage = $newPath;
}
//If the original image is the correct size:
else $this -> newImage = $image;
}
//set this orignal imaget the image returned:
else  $this -> newImage =  $newPath;
return $this -> newImage;
}

}

PHP Image Downloader Class

a simple PHP class with just one method to download images from a remote server. checks for supported types (gif, jpg, png) and will throw an exception if the file is not supported.

usage:

  1. create instance if ImageDownloader
  2. call function downloadImageFrom with parameters: url, destination in local file-system, image name, image type
explanation of download image:
  1. check if the image type is supported
  2. get width and height of original image
  3. create a new image on local machine with width and height
  4. check the filetype and load correct image from imagecreatefrom~ function, parsing the image remote url
  5. resample the image
  6. save the image as the correct filetype using img~ function, parsing the image in memory and the new filename

code:

class ImageDownloader {
var $supported = array(“png”,”jpg”,”gif”);
function downloadImageFrom($url, $to, $fn, $img_type) {
if (in_array($img_type, $this -> supported)) {
list($width, $height) = getimagesize($url);
$newImg = imagecreatetruecolor($width, $height);
$imageTmp = ”;
if ($img_type == ‘png’) {
$imageTmp = imagecreatefrompng($url);
}
elseif ($img_type == ‘jpg’) {
$imageTmp = imagecreatefromjpeg($url);
}
elseif ($img_type == ‘gif’) {
$imageTmp = imagecreatefromgif($url);
}
if ($imageTmp != ”) {
imagecopyresampled($newImg, $imageTmp, 0, 0, 0, 0, $width, $height, $width, $height);
$newPath = $to . $fn . ‘.’ . $img_type;
$this -> imgLoc = $newPath;
if ($img_type == ‘jpg’) {
imagejpeg($newImg, $newPath);
}
elseif ($img_type == ‘gif’) {
imagegif($newImg, $newPath);
}
elseif ($img_type == ‘png’) {
imagepng($newImg, $newPath);
}
}
}
else {
throw new Exception(‘Not supported file-type’);
}
}