public
Last active

  • Download Gist
Resize.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
<?php
 
/*
 
Original version:
written by Jarrod Oberto
taken from http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/
 
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 $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");
}
 
switch(pathinfo($file, PATHINFO_EXTENSION)){
case 'jpg':
case 'jpeg': return imagecreatefromjpeg($file);
case 'gif': return imagecreatefromgif($file);
case 'png': return imagecreatefrompng($file);
}
 
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 ($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 '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 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);
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);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.