Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| Resize | |
0.00% |
0 / 59 |
|
0.00% |
0 / 10 |
1122 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| openImage | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
42 | |||
| resizeImage | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getDimensions | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
56 | |||
| getSizeByFixedHeight | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSizeByFixedWidth | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSizeByAuto | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| getOptimalCrop | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| crop | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| saveImage | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
72 | |||
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | |
| 5 | Original version: |
| 6 | written by Jarrod Oberto |
| 7 | taken from http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/ |
| 8 | |
| 9 | Example usage: |
| 10 | include("classes/Resize.php"); |
| 11 | $resizer = new Resize('images/cars/large/input.jpg'); |
| 12 | $resizer->resizeImage(150, 100, 0); |
| 13 | $resizer->saveImage('images/cars/large/output.jpg', 100); |
| 14 | |
| 15 | */ |
| 16 | |
| 17 | Class Resize { |
| 18 | private $image; |
| 19 | private $width; |
| 20 | private $height; |
| 21 | private $imageResized; |
| 22 | |
| 23 | function __construct($fileName){ |
| 24 | $this->image = $this->openImage($fileName); |
| 25 | $this->width = imagesx($this->image); |
| 26 | $this->height = imagesy($this->image); |
| 27 | } |
| 28 | |
| 29 | private function openImage($file){ |
| 30 | if (!is_file($file)){ |
| 31 | throw new Exception("File {$file} doesn't exists"); |
| 32 | } |
| 33 | |
| 34 | switch(pathinfo($file, PATHINFO_EXTENSION)){ |
| 35 | case 'jpg': |
| 36 | case 'jpeg': return imagecreatefromjpeg($file); |
| 37 | case 'gif': return imagecreatefromgif($file); |
| 38 | case 'png': return imagecreatefrompng($file); |
| 39 | } |
| 40 | |
| 41 | throw new Exception("Invalid image extension for {$file}. Acceptable image types are jpg,jpeg,gif,png"); |
| 42 | } |
| 43 | |
| 44 | public function resizeImage($newWidth, $newHeight, $option='auto'){ |
| 45 | list($width, $height) = $this->getDimensions($newWidth, $newHeight, $option); |
| 46 | |
| 47 | $this->imageResized = imagecreatetruecolor($width, $height); |
| 48 | imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height); |
| 49 | |
| 50 | if ($option == 'crop'){ |
| 51 | $this->crop($width, $height, $newWidth, $newHeight); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private function getDimensions($width, $height, $option){ |
| 56 | switch ($option){ |
| 57 | case 'portrait': return array($this->getSizeByFixedHeight($height), $height); |
| 58 | case 'landscape': return array($width, $this->getSizeByFixedWidth($width)); |
| 59 | case 'auto': return $this->getSizeByAuto($width, $height); |
| 60 | case 'crop': return $this->getOptimalCrop($width, $height); |
| 61 | case 'exact': |
| 62 | default: return array($width, $height); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private function getSizeByFixedHeight($height){ |
| 67 | return ($this->width / $this->height) * $height; |
| 68 | } |
| 69 | |
| 70 | private function getSizeByFixedWidth($width){ |
| 71 | return ($this->height / $this->width) * $width; |
| 72 | } |
| 73 | |
| 74 | private function getSizeByAuto($width, $height){ |
| 75 | if ($this->height < $this->width){ |
| 76 | return array($width, $this->getSizeByFixedWidth($width)); |
| 77 | } |
| 78 | |
| 79 | if ($this->height > $this->width){ |
| 80 | return array($this->getSizeByFixedHeight($height), $height); |
| 81 | } |
| 82 | |
| 83 | if ($height < $width){ |
| 84 | return array($width, $this->getSizeByFixedWidth($width)); |
| 85 | } |
| 86 | |
| 87 | if ($height > $width){ |
| 88 | return array($this->getSizeByFixedHeight($height), $height); |
| 89 | } |
| 90 | |
| 91 | return array($width, $height); |
| 92 | } |
| 93 | |
| 94 | private function getOptimalCrop($width, $height){ |
| 95 | $ratio = min($this->height / $height, $this->width / $width); |
| 96 | return array( |
| 97 | $this->width / $ratio, |
| 98 | $this->height / $ratio |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | private function crop($optimalWidth, $optimalHeight, $width, $height){ |
| 103 | $x = ( $optimalWidth / 2) - ( $width /2 ); |
| 104 | $y = ( $optimalHeight / 2) - ( $height /2 ); |
| 105 | |
| 106 | $crop = $this->imageResized; |
| 107 | |
| 108 | $this->imageResized = imagecreatetruecolor($width , $height); |
| 109 | imagecopyresampled($this->imageResized, $crop, 0, 0, $x, $y, $width, $height , $width, $height); |
| 110 | } |
| 111 | |
| 112 | public function saveImage($savePath, $imageQuality="100"){ |
| 113 | switch(pathinfo($savePath, PATHINFO_EXTENSION)){ |
| 114 | case 'jpg': |
| 115 | case 'jpeg': |
| 116 | if (imagetypes() & IMG_JPG){ |
| 117 | imagejpeg($this->imageResized, $savePath, $imageQuality); |
| 118 | } |
| 119 | break; |
| 120 | |
| 121 | case 'gif': |
| 122 | if (imagetypes() & IMG_GIF){ |
| 123 | imagegif($this->imageResized, $savePath); |
| 124 | } |
| 125 | break; |
| 126 | |
| 127 | case 'png': |
| 128 | if (imagetypes() & IMG_PNG){ |
| 129 | // Scale quality from 0-100 to 0-9 |
| 130 | // Invert quality setting as 0 is best, not 9 |
| 131 | $invertScaleQuality = 9 - round(($imageQuality/100) * 9); |
| 132 | imagepng($this->imageResized, $savePath, $invertScaleQuality); |
| 133 | } |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | imagedestroy($this->imageResized); |
| 138 | } |
| 139 | } |