| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ElfSundae\Support\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ElfSundae\Image\Filters\Resize; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use ElfSundae\Support\Facades\Support; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\HttpFoundation\File\UploadedFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | trait ImageStorage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     use AssetHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * Store image file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @param  mixed  $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @param  string|null  $identifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @return string|null  The stored path | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     protected function storeImageFile($file, $identifier = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         if ($file instanceof UploadedFile && ! $file->isValid()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |             $image = app('image')->make($file) | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |                 ->filter($this->getImageFilter($identifier)) | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |                 ->encode( | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |                     $this->getImageEncodingFormat($identifier), | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |                     $this->getImageEncodingQuality($identifier) | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         } catch (Exception $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $path = trim(trim($this->getImageDirectory($identifier), '/').'/'.trim($this->getImageFilename($image, $identifier), '/'), '/'); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         if ($path && $this->getFilesystem($identifier)->put($path, $image)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             return $path; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * Get image filter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @see http://image.intervention.io/api/filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param  string|null  $identifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     protected function getImageFilter($identifier = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         return (new Resize)->width(1024); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Get image encoding format. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @see http://image.intervention.io/api/encode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param  string|null  $identifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     protected function getImageEncodingFormat($identifier = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * Get image encoding quality. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @see http://image.intervention.io/api/encode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param  string|null  $identifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     protected function getImageEncodingQuality($identifier = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return 90; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * Get image output directory. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param  string|null  $identifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     protected function getImageDirectory($identifier = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return 'images/'.$this->getImageNestedDirectory($identifier); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * Get a "nested" image directory. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @param  string|null  $identifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     protected function getImageNestedDirectory($identifier = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return dechex((int) date('Y') - 2000).'/'.dechex((int) date('W')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * Get image output filename. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param  \Intervention\Image\Image  $image | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @param  string|null  $identifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     protected function getImageFilename($image, $identifier = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         return md5($image).Support::extension($image, '.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |  |