* A new file with Exif functions
[lhc/web/wiklou.git] / includes / Exif.php
1 <?php
2 if (defined('MEDIAWIKI')) {
3 /**
4 * @package MediaWiki
5 * @subpackage Metadata
6 *
7 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
8 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
9 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
10 *
11 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
12 * @bug 1555, 1947
13 */
14
15 /**
16 * @package MediaWiki
17 * @subpackage Metadata
18 */
19 class Exif {
20 /**#@+
21 * @var array
22 */
23
24 /**
25 * Exif tags grouped by category, the tagname itself is the key and the type
26 * is the value, in the case of more than one possible value type they are
27 * seperated by commas.
28 *
29 * @access private
30 */
31 var $mExif;
32
33 /**
34 * A one dimentional array of all Exif tags
35 */
36 var $mFlatExif;
37
38 /**
39 * A one dimentional array of all Exif tags that we'd want to save in
40 * the database or present on a page.
41 */
42 var $mValidExif;
43
44 /**#@-*/
45
46 /**
47 * Constructor
48 */
49 function Exif() {
50 /**#@+
51 * Exif tag type definition
52 */
53 define('BYTE', 1); # An 8-bit unsigned integer.
54 define('ASCII', 2); # An 8-bit byte containing one 7-bit ASCII code. The final byte is terminated with NULL.
55 define('SHORT', 3); # A 16-bit (2-byte) unsigned integer.
56 define('LONG', 4); # A 32-bit (4-byte) unsigned integer.
57 define('RATIONAL', 5); # Two LONGs. The first LONG is the numerator and the second LONG expresses the denominator
58 define('UNDEFINED', 7); # An 8-bit byte that can take any value depending on the field definition
59 define('SLONG', 9); # A 32-bit (4-byte) signed integer (2's complement notation),
60 define('SRATIONAL', 10); # Two SLONGs. The first SLONG is the numerator and the second SLONG is the denominator.
61 /**#@-*/
62
63 /**
64 * Page numbers here refer to pages in the EXIF 2.2 standard
65 *
66 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
67 */
68 $this->mExif = array(
69 # TIFF Rev. 6.0 Attribute Information (p22)
70 'tiff' => array(
71 # Tags relating to image structure
72 'structure' => array(
73 'ImageWidth' => SHORT.','.LONG, # Image width
74 'ImageLength' => SHORT.','.LONG, # Image height
75 'BitsPerSample' => SHORT, # Number of bits per component
76 # "When a primary image is JPEG compressed, this designation is not"
77 # "necessary and is omitted." (p23)
78 'Compression' => SHORT, # Compression scheme
79 'PhotometricInterpretation' => SHORT, # Pixel composition #p23
80 'Orientation' => SHORT, # Orientation of image #p24
81 'SamplesPerPixel' => SHORT, # Number of components
82 'PlanarConfiguration' => SHORT, # Image data arrangement #p24
83 'YCbCrSubSampling' => SHORT, # Subsampling ratio of Y to C #p24
84 'YCbCrPositioning' => SHORT, # Y and C positioning #p24-25
85 'XResolution' => RATIONAL, # Image resolution in width direction
86 'YResolution' => RATIONAL, # Image resolution in height direction
87 'ResolutionUnit' => SHORT, # Unit of X and Y resolution #(p26)
88 ),
89
90 # Tags relating to recording offset
91 'offset' => array(
92 'StripOffsets' => SHORT.','.LONG, # Image data location
93 'RowsPerStrip' => SHORT.','.LONG, # Number of rows per strip
94 'StripByteCounts' => SHORT.','.LONG, # Bytes per compressed strip
95 'JPEGInterchangeFormat' => SHORT.','.LONG, # Offset to JPEG SOI
96 'JPEGInterchangeFormatLength' => SHORT.','.LONG, # Bytes of JPEG data
97 ),
98
99 # Tags relating to image data characteristics
100 'characteristics' => array(
101 'TransferFunction' => SHORT, # Transfer function
102 'WhitePoint' => RATIONAL, # White point chromaticity
103 'PrimaryChromaticities' => RATIONAL, # Chromaticities of primarities
104 'YCbCrCoefficients' => RATIONAL, # Color space transformation matrix coefficients #p27
105 'ReferenceBlackWhite' => RATIONAL # Pair of black and white reference values
106 ),
107
108 # Other tags
109 'other' => array(
110 'DateTime' => ASCII, # File change date and time
111 'ImageDescription' => ASCII, # Image title
112 'Make' => ASCII, # Image input equipment manufacturer
113 'Model' => ASCII, # Image input equipment model
114 'Software' => ASCII, # Software used
115 'Artist' => ASCII, # Person who created the image
116 'Copyright' => ASCII, # Copyright holder
117 ),
118 ),
119
120 # Exif IFD Attribute Information (p30-31)
121 'exif' => array(
122 # Tags relating to version
123 'version' => array(
124 # TODO: NOTE: Nonexistence of this field is taken to mean nonconformance
125 # to the EXIF 2.1 AND 2.2 standards
126 'ExifVersion' => UNDEFINED, # Exif version
127 'FlashpixVersion' => UNDEFINED, # Supported Flashpix version #p32
128 ),
129
130 # Tags relating to Image Data Characteristics
131 'characteristics' => array(
132 'ColorSpace' => SHORT, # Color space information #p32
133 ),
134
135 # Tags relating to image configuration
136 'configuration' => array(
137 'ComponentsConfiguration' => UNDEFINED, # Meaning of each component #p33
138 'CompressedBitsPerPixel' => RATIONAL, # Image compression mode
139 'PixelYDimension' => SHORT.','.LONG, # Valid image width
140 'PixelXDimension' => SHORT.','.LONG, # Valind image height
141 ),
142
143 # Tags relating to related user information
144 'user' => array(
145 'MakerNote' => UNDEFINED, # Manufacturer notes
146 'UserComment' => UNDEFINED, # User comments #p34
147 ),
148
149 # Tags relating to related file information
150 'related' => array(
151 'RelatedSoundFile' => ASCII, # Related audio file
152 ),
153
154 # Tags relating to date and time
155 'dateandtime' => array(
156 'DateTimeOriginal' => ASCII, # Date and time of original data generation #p36
157 'DateTimeDigitized' => ASCII, # Date and time of original data generation
158 'SubSecTime' => ASCII, # DateTime subseconds
159 'SubSecTimeOriginal' => ASCII, # DateTimeOriginal subseconds
160 'SubSecTimeDigitized' => ASCII, # DateTimeDigitized subseconds
161 ),
162
163 # Tags relating to picture-taking conditions (p31)
164 'conditions' => array(
165 'ExposureTime' => RATIONAL, # Exposure time
166 'FNumber' => RATIONAL, # F Number
167 'ExposureProgram' => SHORT, # Exposure Program #p38
168 'SpectralSensitivity' => ASCII, # Spectral sensitivity
169 'ISOSpeedRatings' => SHORT, # ISO speed rating
170 'OECF' => UNDEFINED, # Optoelectronic conversion factor
171 'ShutterSpeedValue' => SRATIONAL, # Shutter speed
172 'ApertureValue' => RATIONAL, # Aperture
173 'BrightnessValue' => SRATIONAL, # Brightness
174 'ExposureBiasValue' => SRATIONAL, # Exposure bias
175 'MaxApertureValue' => RATIONAL, # Maximum land aperture
176 'SubjectDistance' => RATIONAL, # Subject distance
177 'MeteringMode' => SHORT, # Metering mode #p40
178 'LightSource' => SHORT, # Light source #p40-41
179 'Flash' => SHORT, # Flash #p41-42
180 'FocalLength' => RATIONAL, # Lens focal length
181 'SubjectArea' => SHORT, # Subject area
182 'FlashEnergy' => RATIONAL, # Flash energy
183 'SpatialFrequencyResponse' => UNDEFINED, # Spatial frequency response
184 'FocalPlaneXResolution' => RATIONAL, # Focal plane X resolution
185 'FocalPlaneYResolution' => RATIONAL, # Focal plane Y resolution
186 'FocalPlaneResolutionUnit' => SHORT, # Focal plane resolution unit
187 'SubjectLocation' => SHORT, # Subject location
188 'ExposureIndex' => RATIONAL, # Exposure index
189 'SensingMethod' => SHORT, # Sensing method #p46
190 'FileSource' => UNDEFINED, # File source #p47
191 'SceneType' => UNDEFINED, # Scene type #p47
192 'CFAPattern' => UNDEFINED, # CFA pattern
193 'CustomRendered' => SHORT, # Custom image processing #p48
194 'ExposureMode' => SHORT, # Exposure mode #p48
195 'WhiteBalance' => SHORT, # White Balance #p49
196 'DigitalZoomRatio' => RATIONAL, # Digital zoom ration
197 'FocalLengthIn35mmFilm' => SHORT, # Focal length in 35 mm film
198 'SceneCaptureType' => SHORT, # Scene capture type #p49
199 'GainControl' => RATIONAL, # Scene control #p49-50
200 'Contrast' => SHORT, # Contrast #p50
201 'Saturation' => SHORT, # Saturation #p50
202 'Sharpness' => SHORT, # Sharpness #p50
203 'DeviceSettingDescription' => UNDEFINED, # Desice settings description
204 'SubjectDistanceRange' => SHORT, # Subject distance range #p51
205 ),
206
207 'other' => array(
208 'ImageUniqueID' => ASCII, # Unique image ID
209 ),
210 ),
211
212 # GPS Attribute Information (p52)
213 'gps' => array(
214 'GPSVersionID' => BYTE, # GPS tag version
215 'GPSLatitudeRef' => ASCII, # North or South Latitude #p52-53
216 'GPSLatitude' => RATIONAL, # Latitude
217 'GPSLongitudeRef' => ASCII, # East or West Longitude #p53
218 'GPSLongitude' => RATIONAL, # Longitude
219 'GPSAltitudeRef' => BYTE, # Altitude reference
220 'GPSAltitude' => RATIONAL, # Altitude
221 'GPSTimeStamp' => RATIONAL, # GPS time (atomic clock)
222 'GPSSatellites' => ASCII, # Satellites used for measurement
223 'GPSStatus' => ASCII, # Receiver status #p54
224 'GPSMeasureMode' => ASCII, # Measurement mode #p54-55
225 'GPSDOP' => RATIONAL, # Measurement precision
226 'GPSSpeedRef' => ASCII, # Speed unit #p55
227 'GPSSpeed' => RATIONAL, # Speed of GPS receiver
228 'GPSTrackRef' => ASCII, # Reference for direction of movement #p55
229 'GPSTrack' => RATIONAL, # Direction of movement
230 'GPSImgDirectionRef' => ASCII, # Reference for direction of image #p56
231 'GPSImgDirection' => RATIONAL, # Direction of image
232 'GPSMapDatum' => ASCII, # Geodetic survey data used
233 'GPSDestLatitudeRef' => ASCII, # Reference for latitude of destination #p56
234 'GPSDestLatitude' => RATIONAL, # Latitude destination
235 'GPSDestLongitudeRef' => ASCII, # Reference for longitude of destination #p57
236 'GPSDestLongitude' => RATIONAL, # Longitude of destination
237 'GPSDestBearingRef' => ASCII, # Reference for bearing of destination #p57
238 'GPSDestBearing' => RATIONAL, # Bearing of destination
239 'GPSDestDistanceRef' => ASCII, # Reference for distance to destination #p57-58
240 'GPSDestDistance' => RATIONAL, # Distance to destination
241 'GPSProcessingMethod' => UNDEFINED, # Name of GPS processing method
242 'GPSAreaInformation' => UNDEFINED, # Name of GPS area
243 'GPSDateStamp' => ASCII, # GPS date
244 'GPSDifferential' => SHORT, # GPS differential correction
245 ),
246 );
247
248 $this->makeFlatExifTags();
249 $this->makeValidExifTags();
250 }
251
252 /**
253 * Get the raw list of exiftags
254 *
255 * @access private
256 * @return array
257 */
258 function getExif() {
259 return $this->mExif;
260 }
261
262 /**
263 * Generate a flat list of the exif tags
264 */
265 function makeFlatExifTags() {
266 $exif = $this->getExif();
267 array_walk($exif, array(&$this, 'callback')); // note the reference
268 }
269
270 /**
271 * A callback function used by makeFlatExifTags()
272 */
273 function callback($val, $key) {
274 if (gettype($val) === 'array')
275 array_walk($val, array(&$this, 'callback'));
276 else
277 $this->mFlatExif[$key] = $val;
278 }
279
280 /**
281 * Produce a list of all Exif tags appropriate for user output
282 *
283 * Produce a list of all tags that we want to show in output, in order not to
284 * output binary gibberish such as raw thumbnails we strip all tags
285 * with the datatype of UNDEFINED.
286 *
287 * @todo We might actually want to display some of the UNDEFINED
288 * stuff, but we strip it for now.
289 */
290 function makeValidExifTags() {
291 foreach($this->mFlatExif as $key => $val) {
292 if (strpos( $val, (string)UNDEFINED ) !== false)
293 continue;
294 $this->mValidExif[] = $key;
295 }
296 }
297
298 /**
299 * The version of the output format
300 *
301 * Before the actual metadata information is saved in the database we
302 * strip some of it since we don't want to save things like thumbnails
303 * which usually accompany Exif data. This value gets saved in the
304 * database along with the actual Exif data, and if the version in the
305 * database doesn't equal the value returned by this function the Exif
306 * data is regenerated.
307 *
308 * @return int
309 */
310 function version() {
311 return 1; // We don't need no bloddy constants!
312 }
313 }
314 } // MEDIAWIKI