| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace NwLaravel\FileStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Intervention\Image\ImageManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Intervention\Image\Image; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | class ImagineGd implements Imagine | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |      * @var ImageManager | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     protected $manager; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * @var Image | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     protected $image; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * Construct | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @param string       $path | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @param ImageManager $manager | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function __construct($path, ImageManager $manager) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->manager = $manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->image = $this->manager->make($path); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * Filesize | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @return int | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function filesize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         return $this->image->filesize(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * Define Resize | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @param int     $width | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @param int     $height | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @param boolean $force | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @return Imagine | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function resize($width, $height, $force = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $width = intval($width); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $height = intval($height); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $callback = function () {}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if ($width > 0 || $height > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             // AutoScale - aspectRatio | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             if (!$force) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 $callback = function ($constraint) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                     $constraint->aspectRatio(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                     $constraint->upsize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $width = $width?:null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $height = $height?:null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $this->image->resize($width, $height, $callback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * Opacity | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * @return Imagine | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function opacity($opacity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $opacity = intval($opacity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if ($opacity > 0 && $opacity < 100) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->image->opacity($opacity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * Watermark | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * @param string  $path | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * @param string  $position | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      * @param integer $opacity | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      * @return Imagine | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function watermark($path, $position = 'center', $opacity = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if ($this->isImage($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $watermark = $this->manager->make($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $width = $this->image->width(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $height = $this->image->height(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             if ($watermark->width() > $width || $watermark->height() > $height) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $watermark->resize($width, $height, function ($constraint) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                     $constraint->aspectRatio(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                     $constraint->upsize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             if (!is_null($opacity) && $opacity >= 0 && $opacity <= 100) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 $watermark->opacity($opacity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             $this->image->insert($watermark, $position); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |      * Crop | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |      * @param integer $width | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |      * @param integer $height | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * @param integer $x | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      * @param integer $y | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |      * @return binary | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     public function crop($width, $height, $x, $y) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $this->image->crop($width, $height, $x, $y); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         return $this; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      * Rotate Image | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |      * @param integer $angle | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |      * @return binary | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     public function rotate($angle) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $angle = intval($angle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         if ($angle > -360 && $angle < 360) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             $this->image->rotate($angle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         return $this; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |      * Strip Profiles | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |      * @param string $except | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |      * @return this | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |      * @todo Pendente para Criaçãio | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     public function stripProfiles() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         return $this; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 169 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |      * Is Image | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |      * @param string $path | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |      * @return boolean | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     protected function isImage($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         return (bool) ($path && is_file($path) && strpos(mime_content_type($path), 'image/')===0); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 181 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |      * Encode | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |      * @param string  $format | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |      * @param integer $quality | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  |      * @return binary | 
            
                                                                        
                            
            
                                    
            
            
                | 190 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     public function encode($format = null, $quality = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         return $this->image->encode($format, $quality); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 194 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 196 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 197 |  |  |      * Save | 
            
                                                                        
                            
            
                                    
            
            
                | 198 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 199 |  |  |      * @param string  $path | 
            
                                                                        
                            
            
                                    
            
            
                | 200 |  |  |      * @param integer $quality | 
            
                                                                        
                            
            
                                    
            
            
                | 201 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 202 |  |  |      * @return binary | 
            
                                                                        
                            
            
                                    
            
            
                | 203 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |     public function save($path, $quality = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         $this->image = $this->image->save($path, $quality); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         return $this; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 209 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 210 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 211 |  |  |  |