|
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
|
1 |
|
public function processExifData() |
|
18
|
|
|
{ |
|
19
|
1 |
|
$image = $this->owner->Image(); |
|
20
|
1 |
|
$filename = BASE_PATH . '/' . $image->Filename; |
|
21
|
|
|
|
|
22
|
|
|
// when the image is first saved, the file will still be a temp file |
|
23
|
1 |
|
if ($image->exists()) { |
|
24
|
|
|
try { |
|
25
|
1 |
|
$exif = exif_read_data($filename, null, true); |
|
26
|
1 |
|
$aperture = $exif['COMPUTED']['ApertureFNumber']; |
|
27
|
1 |
|
$aperture = str_replace('f/', '', $aperture); |
|
28
|
1 |
|
$this->owner->Aperture = $aperture; |
|
29
|
|
|
|
|
30
|
1 |
|
$shutterspeed = ''; |
|
31
|
1 |
|
if (isset($exif['ExposureTime'])) { |
|
32
|
|
|
$shutterspeed = $exif['ExposureTime']; |
|
33
|
|
|
} else { |
|
34
|
1 |
|
$shutterspeed = $exif['EXIF']['ExposureTime']; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
$this->owner->ShutterSpeed = $shutterspeed; |
|
38
|
1 |
|
if (isset($exif['DateTimeOriginal'])) { |
|
39
|
|
|
$this->owner->TakenAt = $exif['DateTimeOriginal']; |
|
40
|
|
|
} else { |
|
41
|
1 |
|
$this->owner->TakenAt = $exif['EXIF']['DateTimeOriginal']; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
$iso = ''; |
|
45
|
1 |
|
if (isset($exif['ISOSpeedRatings'])) { |
|
46
|
|
|
$iso = $exif['ISOSpeedRatings']; |
|
47
|
|
|
} else { |
|
48
|
1 |
|
$iso = $exif['EXIF']['ISOSpeedRatings']; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
$this->owner->ISO = $iso; |
|
52
|
|
|
|
|
53
|
1 |
|
if (isset($exif['GPS'])) { |
|
54
|
1 |
|
$gps = $exif['GPS']; |
|
55
|
1 |
|
$latarray = $gps['GPSLatitude']; |
|
56
|
1 |
|
$degrees = $latarray[0]; |
|
57
|
1 |
|
$parts = explode('/', $degrees); |
|
58
|
1 |
|
$degrees = $parts[0] / $parts[1]; |
|
59
|
1 |
|
$minutes = $latarray[1]; |
|
60
|
1 |
|
$parts = explode('/', $minutes); |
|
61
|
1 |
|
$minutes = $parts[0] / $parts[1]; |
|
62
|
1 |
|
$seconds = $latarray[2]; |
|
63
|
1 |
|
$parts = explode('/', $seconds); |
|
64
|
1 |
|
$seconds = $parts[0] / $parts[1]; |
|
65
|
1 |
|
$latitude = $degrees + $minutes / 60 + $seconds / 3600; |
|
66
|
1 |
|
$lonarray = $gps['GPSLongitude']; |
|
67
|
1 |
|
$degrees = $lonarray[0]; |
|
68
|
1 |
|
$parts = explode('/', $degrees); |
|
69
|
1 |
|
$degrees = $parts[0] / $parts[1]; |
|
70
|
1 |
|
$minutes = $lonarray[1]; |
|
71
|
1 |
|
$parts = explode('/', $minutes); |
|
72
|
1 |
|
$minutes = $parts[0] / $parts[1]; |
|
73
|
1 |
|
$seconds = $lonarray[2]; |
|
74
|
1 |
|
$parts = explode('/', $seconds); |
|
75
|
1 |
|
$seconds = $parts[0] / $parts[1]; |
|
76
|
|
|
|
|
77
|
1 |
|
$longitude = $degrees + $minutes / 60 + $seconds / 3600; |
|
78
|
1 |
|
$this->owner->Lat = $latitude; |
|
79
|
1 |
|
$this->owner->Lon = $longitude; |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
$image = $this->owner->Image(); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
1 |
|
$this->owner->ExifRead = true; |
|
85
|
1 |
|
$this->owner->Orientation = $this->owner->Image()->getOrientation(); |
|
86
|
1 |
|
$this->owner->write(); |
|
87
|
|
|
} |
|
88
|
1 |
|
catch (Exception $e) { |
|
89
|
|
|
error_log($e->getMessage()); |
|
90
|
|
|
} |
|
91
|
1 |
|
} |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
public function onAfterWrite() |
|
95
|
|
|
{ |
|
96
|
1 |
|
parent::onAfterWrite(); |
|
97
|
|
|
|
|
98
|
1 |
|
if (!($this->owner->ExifRead)) { |
|
99
|
1 |
|
$this->processExifData(); |
|
100
|
1 |
|
} |
|
101
|
1 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function requireDefaultRecords() |
|
104
|
|
|
{ |
|
105
|
|
|
parent::requireDefaultRecords(); |
|
106
|
|
|
$imagesToProcess = GalleryImage::get()->filter('ExifRead', 0); |
|
107
|
|
|
foreach ($imagesToProcess as $image) { |
|
108
|
|
|
$image->processExifData(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
DB::alteration_message('Updated image metadata where EXIF has not been ' . 'processed', 'changed'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
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.