-
-
Save kharysharpe/10542948 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Original version: | |
written by Jarrod Oberto | |
taken from http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/ | |
Modified From: | |
Radoslav Stankov | |
https://gist.github.com/RStankov/342704 | |
Example usage: | |
include("classes/Resize.php"); | |
$resizer = new Resize('images/cars/large/input.jpg'); | |
$resizer->resizeImage(150, 100, 0); | |
$resizer->saveImage('images/cars/large/output.jpg', 100); | |
*/ | |
class Resize | |
{ | |
private $image; | |
private $width; | |
private $height; | |
private $imageType; | |
private $imageResized; | |
function __construct($fileName) | |
{ | |
$this->image = $this->openImage($fileName); | |
$this->width = imagesx($this->image); | |
$this->height = imagesy($this->image); | |
} | |
private function openImage($file) | |
{ | |
if (!is_file($file)) | |
{ | |
throw new Exception("File {$file} doesn't exists"); | |
} | |
//Get Information | |
$imageInfo = getimagesize($file); | |
if ($imageInfo) | |
{ | |
switch ($imageInfo[2]) | |
{ | |
case IMAGETYPE_JPEG: | |
$this->imageType = IMAGETYPE_JPEG; | |
return imagecreatefromjpeg($file); | |
case IMAGETYPE_GIF: | |
$this->imageType = IMAGETYPE_GIF; | |
return imagecreatefromgif($file); | |
case IMAGETYPE_PNG: | |
$this->imageType = IMAGETYPE_PNG; | |
return imagecreatefrompng($file); | |
} | |
} | |
else | |
{ | |
throw new Exception("File {$file} doesn't appear to be a support format"); | |
} | |
throw new Exception("Invalid image extension for {$file}. Acceptable image types are jpg,jpeg,gif,png"); | |
} | |
public function resizeImage($newWidth, $newHeight, $option = 'auto') | |
{ | |
list($width, $height) = $this->getDimensions($newWidth, $newHeight, $option); | |
$this->imageResized = imagecreatetruecolor($width, $height); | |
imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height); | |
if ($this->imageType == IMAGETYPE_PNG) | |
{ | |
//Preserve Transparency | |
imagesavealpha($this->imageResized, true); | |
imagealphablending($this->imageResized, false); | |
} | |
if ($option == 'crop') | |
{ | |
$this->crop($width, $height, $newWidth, $newHeight); | |
} | |
} | |
private function getDimensions($width, $height, $option) | |
{ | |
switch ($option) | |
{ | |
case 'portrait': return array($this->getSizeByFixedHeight($height), $height); | |
case 'landscape': return array($width, $this->getSizeByFixedWidth($width)); | |
case 'auto': return $this->getSizeByAuto($width, $height); | |
case 'crop': return $this->getOptimalCrop($width, $height); | |
case 'confine': return $this->getSizeByConfinedSpace($width, $height); | |
case 'exact': | |
default: return array($width, $height); | |
} | |
} | |
private function getSizeByFixedHeight($height) | |
{ | |
return ($this->width / $this->height) * $height; | |
} | |
private function getSizeByFixedWidth($width) | |
{ | |
return ($this->height / $this->width) * $width; | |
} | |
private function getSizeByAuto($width, $height) | |
{ | |
if ($this->height < $this->width) | |
{ | |
return array($width, $this->getSizeByFixedWidth($width)); | |
} | |
if ($this->height > $this->width) | |
{ | |
return array($this->getSizeByFixedHeight($height), $height); | |
} | |
if ($height < $width) | |
{ | |
return array($width, $this->getSizeByFixedWidth($width)); | |
} | |
if ($height > $width) | |
{ | |
return array($this->getSizeByFixedHeight($height), $height); | |
} | |
return array($width, $height); | |
} | |
private function getSizeByConfinedSpace($maxwidth, $maxheight) | |
{ | |
// Wriiten by: Andy Schuler | |
// http://code.tutsplus.com/tutorials/image-resizing-made-easy-with-php--net-10362#comment-693773779 | |
if ($newWidth >= $this->width && $newHeight >= $this->height) | |
{ | |
//we're just going to return the original | |
return array('optimalWidth' => $this->width, 'optimalHeight' => $this->height); | |
} | |
if($newWidth >= $this->width && $newHeight < $this->height ) | |
{ | |
//so we are height bound | |
return array('optimalWidth' => $this->getSizeByFixedHeight($newHeight), 'optimalHeight' => $newHeight); | |
} | |
if($newHeight >= $this->height && $newWidth < $this->width ) | |
{ | |
//so we are width bound | |
return array('optimalWidth' => $newWidth, 'optimalHeight' => $this->getSizeByFixedWidth($newWidth)); | |
} | |
return $this->getSizeByAuto($newWidth, $newHeight); | |
} | |
private function getOptimalCrop($width, $height) | |
{ | |
$ratio = min($this->height / $height, $this->width / $width); | |
return array( | |
$this->width / $ratio, | |
$this->height / $ratio | |
); | |
} | |
private function crop($optimalWidth, $optimalHeight, $width, $height) | |
{ | |
$x = ( $optimalWidth / 2) - ( $width / 2 ); | |
$y = ( $optimalHeight / 2) - ( $height / 2 ); | |
$crop = $this->imageResized; | |
$this->imageResized = imagecreatetruecolor($width, $height); | |
if ($this->imageType == IMAGETYPE_PNG) | |
{ | |
//Preserve Transparency | |
imagesavealpha($this->imageResized, true); | |
imagealphablending($this->imageResized, false); | |
} | |
imagecopyresampled($this->imageResized, $crop, 0, 0, $x, $y, $width, $height, $width, $height); | |
} | |
public function saveImage($savePath, $imageQuality = "100") | |
{ | |
switch (pathinfo($savePath, PATHINFO_EXTENSION)) | |
{ | |
case 'jpg': | |
case 'jpeg': | |
if (imagetypes() & IMG_JPG) | |
{ | |
imagejpeg($this->imageResized, $savePath, $imageQuality); | |
} | |
break; | |
case 'gif': | |
if (imagetypes() & IMG_GIF) | |
{ | |
imagegif($this->imageResized, $savePath); | |
} | |
break; | |
case 'png': | |
if (imagetypes() & IMG_PNG) | |
{ | |
// Scale quality from 0-100 to 0-9 | |
// Invert quality setting as 0 is best, not 9 | |
$invertScaleQuality = 9 - round(($imageQuality / 100) * 9); | |
imagepng($this->imageResized, $savePath, $invertScaleQuality); | |
} | |
break; | |
} | |
imagedestroy($this->imageResized); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment