|
| 1 | +<?php |
| 2 | + |
| 3 | +use Intervention\Image\ImageManagerStatic as Image; |
| 4 | + |
| 5 | +class ImageHelper{ |
| 6 | + private $image; |
| 7 | + private $quality; |
| 8 | + |
| 9 | + private $originalPath; |
| 10 | + private $path; |
| 11 | + private $extension; |
| 12 | + |
| 13 | + private $handler; |
| 14 | + private $operations = []; |
| 15 | + |
| 16 | + /** @return ImageHelper */ |
| 17 | + public static function fromCMS($handler){ |
| 18 | + return new ImageHelper(str_replace('/admin/', 'admin/', $handler->src())); |
| 19 | + } |
| 20 | + |
| 21 | + /** @return ImageHelper */ |
| 22 | + public static function fromPath($path){ |
| 23 | + return new ImageHelper($path); |
| 24 | + } |
| 25 | + |
| 26 | + public function __construct($path, $quality = 65){ |
| 27 | + $this->quality = $quality; |
| 28 | + $this->originalPath = $path; |
| 29 | + $this->extension = pathinfo($path, PATHINFO_EXTENSION); |
| 30 | + $pathPieces = explode('/', rtrim($path, '/')); |
| 31 | + $this->path = substr(array_pop($pathPieces), 0, -strlen($this->extension)-1) . 'q' . $quality; |
| 32 | + } |
| 33 | + |
| 34 | + /** @return ImageHelper */ |
| 35 | + public function resize($width, $height, $preventUpsize = true){ |
| 36 | + if(!$width && !$height) throw new Exception("Either width or height has to be defined"); |
| 37 | + |
| 38 | + $this->path .= '_resize' . ($width?$width:'a') . 'x' . ($height?$height:'a'); |
| 39 | + $this->operations[] = function($im) use ($width, $height, $preventUpsize){ |
| 40 | + return $im->resize($width, $height, function ($constraint) use($preventUpsize) { |
| 41 | + $constraint->aspectRatio(); |
| 42 | + if($preventUpsize) $constraint->upsize(); |
| 43 | + }); |
| 44 | + }; |
| 45 | + return $this; |
| 46 | + } |
| 47 | + |
| 48 | + /** @return ImageHelper */ |
| 49 | + public function grayscale() { |
| 50 | + $this->path .= '_grayscale'; |
| 51 | + $this->operations[] = function($im) { |
| 52 | + return $im->greyscale(); |
| 53 | + }; |
| 54 | + return $this; |
| 55 | + } |
| 56 | + |
| 57 | + /** @return ImageHelper */ |
| 58 | + public function multiply($r,$g,$b){ |
| 59 | + $this->path .= '_multiply'.$r.'_'.$g.'_'.$b; |
| 60 | + $this->operations[] = function($im) use($r,$g,$b) { |
| 61 | + $srcPath = 'cache/tmp/'.uniqid().'.'.$this->extension; |
| 62 | + $im->save($this->pathToDir($srcPath)); |
| 63 | + |
| 64 | + switch($this->extension) { |
| 65 | + case 'gif': |
| 66 | + $src = imagecreatefromgif($this->pathToDir($srcPath)); |
| 67 | + break; |
| 68 | + case 'png': |
| 69 | + $src = imagecreatefrompng($this->pathToDir($srcPath)); |
| 70 | + break; |
| 71 | + case 'jpg': |
| 72 | + case 'jpeg': |
| 73 | + $src = imagecreatefromjpeg($this->pathToDir($srcPath)); |
| 74 | + break; |
| 75 | + default: |
| 76 | + throw new Exception("Uknown extension type: " . $this->extension); |
| 77 | + } |
| 78 | + |
| 79 | + $filter_r=$r; |
| 80 | + $filter_g=$g; |
| 81 | + $filter_b=$b; |
| 82 | + $imagex = imagesx($src); |
| 83 | + $imagey = imagesy($src); |
| 84 | + for ($x = 0; $x <$imagex; ++$x) { |
| 85 | + for ($y = 0; $y <$imagey; ++$y) { |
| 86 | + $rgb = imagecolorat($src, $x, $y); |
| 87 | + $TabColors = imagecolorsforindex ( $src , $rgb ); |
| 88 | + $color_r = floor($TabColors['red']*$filter_r/255); |
| 89 | + $color_g = floor($TabColors['green']*$filter_g/255); |
| 90 | + $color_b = floor($TabColors['blue']*$filter_b/255); |
| 91 | + $newcol = imagecolorallocate($src, $color_r,$color_g,$color_b); |
| 92 | + imagesetpixel($src, $x, $y, $newcol); |
| 93 | + } |
| 94 | + } |
| 95 | + return Image::make($src); |
| 96 | + }; |
| 97 | + return $this; |
| 98 | + } |
| 99 | + |
| 100 | + public function size(){ |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + public function src(){ |
| 105 | + $finalPath = 'cache/images/' . $this->path . '.' . $this->extension; |
| 106 | + $filePath = $this->pathToDir($finalPath); |
| 107 | + |
| 108 | + |
| 109 | + if(!file_exists($filePath)){ |
| 110 | + //If not in cache -> apply operations |
| 111 | + $this->image = Image::make($this->pathToDir($this->originalPath)); |
| 112 | + foreach($this->operations as $operation){ |
| 113 | + $this->image = $operation($this->image); |
| 114 | + } |
| 115 | + $this->image->save($filePath); |
| 116 | + } |
| 117 | + |
| 118 | + return '/' . $finalPath; |
| 119 | + } |
| 120 | + |
| 121 | + private function pathToDir($path){ |
| 122 | + return getcwd() . '/' . $path; |
| 123 | + } |
| 124 | + |
| 125 | + public function __toString(){ |
| 126 | + return $this->src(); |
| 127 | + } |
| 128 | +} |