| 1 |  |  | <?php | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 3 |  |  | class ImageMetaDataExtension extends DataExtension | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 4 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 5 |  |  |     private static $db = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 6 |  |  |         'ExifRead' => 'Boolean', | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  |         'Aperture' => 'Varchar', | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  |         'ShutterSpeed' => 'Varchar', | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |         'TakenAt' => 'Datetime', | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |         'ISO' => 'Int', | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |         'Orientation' => 'Int' | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     ); | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     public static $defaults = array('ExifRead' => false); | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 15 |  |     public function processExifData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 15 |  |         $image    = $this->owner->Image(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 15 |  |         $filename = BASE_PATH . '/' . $image->Filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         // when the image is first saved, the file will still be a temp file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 15 |  |         if ($image->exists()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 2 |  |                 $exif = exif_read_data($filename, null, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 2 |  |                 error_log(print_r($exif, 1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 2 |  |                 if (isset($exif['COMPUTED']['ApertureFNumber'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 2 |  |                     $aperture = $exif['COMPUTED']['ApertureFNumber']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 2 |  |                     $aperture = str_replace('f/', '', $aperture); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 2 |  |                     $this->owner->Aperture = $aperture; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 2 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 2 |  |                 $shutterspeed = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 2 |  |                 if (isset($exif['ExposureTime'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                     $shutterspeed = $exif['ExposureTime']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 2 |  |                 } elseif (isset($exif['EXIF']['ExposureTime'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 2 |  |                     $shutterspeed = $exif['EXIF']['ExposureTime']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 2 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 2 |  |                 $this->owner->ShutterSpeed = $shutterspeed; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 2 |  |                 if (isset($exif['DateTimeOriginal'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                     $this->owner->TakenAt = $exif['DateTimeOriginal']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 2 |  |                 } elseif (isset($exif['EXIF']['DateTimeOriginal'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 2 |  |                     $this->owner->TakenAt = $exif['EXIF']['DateTimeOriginal']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |                 $iso = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |                 if (isset($exif['ISOSpeedRatings'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     $iso = $exif['ISOSpeedRatings']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |                 } elseif (isset($exif['EXIF']['ISOSpeedRatings'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |                     $iso = $exif['EXIF']['ISOSpeedRatings']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 2 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |                 $this->owner->ISO = $iso; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |                 if (isset($exif['GPS'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |                     $gps      = $exif['GPS']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |                     $latarray = $gps['GPSLatitude']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |                     $degrees  = $latarray[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 2 |  |                     $parts    = explode('/', $degrees); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |                     $degrees  = $parts[0] / $parts[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |                     $minutes  = $latarray[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |                     $parts    = explode('/', $minutes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |                     $minutes  = $parts[0] / $parts[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |                     $seconds  = $latarray[2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 2 |  |                     $parts    = explode('/', $seconds); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |                     $seconds  = $parts[0] / $parts[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2 |  |                     $latitude = $degrees + $minutes / 60 + $seconds / 3600; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |                     $lonarray = $gps['GPSLongitude']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  |                     $degrees  = $lonarray[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  |                     $parts    = explode('/', $degrees); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 2 |  |                     $degrees  = $parts[0] / $parts[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |                     $minutes  = $lonarray[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 2 |  |                     $parts    = explode('/', $minutes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 2 |  |                     $minutes  = $parts[0] / $parts[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 2 |  |                     $seconds  = $lonarray[2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |                     $parts    = explode('/', $seconds); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |                     $seconds  = $parts[0] / $parts[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  |                     $longitude        = $degrees + $minutes / 60 + $seconds / 3600; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  |                     $this->owner->Lat = $latitude; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  |                     $this->owner->Lon = $longitude; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 2 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 2 |  |                 $image = $this->owner->Image(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |                 $this->owner->ExifRead = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |                 $this->owner->Orientation = $this->owner->Image()->getOrientation(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |                 $this->owner->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |             catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 error_log($e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 2 |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 | 15 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 15 |  |     public function onAfterWrite() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 15 |  |         parent::onAfterWrite(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 15 |  |         if (!($this->owner->ExifRead)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 15 |  |             $this->processExifData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 15 |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 | 15 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |     public function requireDefaultRecords() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |         parent::requireDefaultRecords(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |         $imagesToProcess = GalleryImage::get()->filter('ExifRead', 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |         foreach ($imagesToProcess as $image) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |             $image->processExifData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |         DB::alteration_message('Updated image metadata where EXIF has not been ' . 'processed', 'changed'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 | 1 |  |     } | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 118 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |  | 
            
                        
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.