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