error(500, 'Missing EXIF library. Make sure your PHP installation includes the EXIF library.'); } } /* returns the replacing title of the file or null on none replacement */ public function elementNamesEvent(Element $element) { if (empty(Lonely::model()->imageInfo_notitle) && $element instanceof Image && $element->getMime() == 'image/jpeg') { $location = $element->getLocation(); if ($location !== '') { /* cached value */ if (isset($this->_tmp_names[$location]) && $this->_tmp_names[$location] !== "") { return $this->_tmp_names[$location]; } return $this->_tmp_names[$location] = $this->getJpegName($location); } } return null; } protected function getJpegName($location) { /* IPTC */ if (getimagesize($location, $info) && isset($info['APP13']) && ($iptc = iptcparse($info['APP13'])) && isset($iptc['2#105'][0]) && ($name = trim($iptc['2#105'][0])) !== '') { return \LonelyGallery\utf8ify($name); } /* EXIF */ if ($exif = exif_read_data($location)) { if (isset($exif['ImageDescription']) && ($name = trim($exif['ImageDescription'])) !== '') { return \LonelyGallery\utf8ify($name); } else if (isset($exif['Title']) && ($name = trim($exif['Title'])) !== '') { return \LonelyGallery\utf8ify($name); } } return null; } /* returns an array of information about the image */ public function fileInfoEvent(File $file) { if ($file instanceof Image) { switch ($file->getMime()) { case 'image/jpeg': return $this->getMetadata($file); break; case 'image/png': return $this->getFiledata($file); break; } } return array(); } /* returns the EXIF data */ protected function getFiledata(Image $file) { $metadata = array(); $info = @getimagesize($file->getLocation()); /* resolution */ if (!empty($info[0]) && !empty($info[1])) { $metadata['Resolution'] = $info[0].' x '.$info[1].' px'; } /* filesize */ $metadata['Filesize'] = round(filesize($file->getLocation()) * 0.001, 2).' kB'; return $metadata; } /* checks a value */ public static function isEmpty($value) { return !(is_scalar($value) ? trim($value) : $value); } /* returns the EXIF data */ protected function getMetadata(Image $file) { $metadata = array(); $location = $file->getLocation(); /* load data */ /* EXIF */ if (!($exif = exif_read_data($location))) { $exif = array(); } /* IPTC */ if (($gis = getimagesize($location, $data)) && isset($data['APP13'])) { if (!($iptc = iptcparse($data['APP13']))) { $iptc = array(); } } /* description */ if (isset($iptc['2#120'][0]) && !self::isEmpty($iptc['2#120'][0])) { // caption $metadata[] = \LonelyGallery\utf8ify($iptc['2#120'][0]); } else if (isset($exif['ImageDescription']) && !self::isEmpty($exif['ImageDescription'])) { $metadata[] = \LonelyGallery\utf8ify($exif['ImageDescription']); } else if (isset($exif['Subject']) && !self::isEmpty($exif['Subject'])) { $metadata[] = \LonelyGallery\utf8ify($exif['Subject']); } /* photographer */ if (isset($iptc['2#080'][0]) && !self::isEmpty($iptc['2#080'][0])) { // by-line $metadata['Photographer'] = \LonelyGallery\utf8ify($iptc['2#080'][0]).(empty($iptc['2#085'][0]) ? '' : ' ('.\LonelyGallery\utf8ify($exif['2#085'][0]).')'); } else if (isset($exif['Artist']) && !self::isEmpty($exif['Artist'])) { $metadata['Photographer'] = \LonelyGallery\utf8ify($exif['Artist']); } else if (isset($exif['Author']) && !self::isEmpty($exif['Author'])) { $metadata['Photographer'] = \LonelyGallery\utf8ify($exif['Author']); } /* copyright */ if (isset($iptc['2#116'][0]) && !self::isEmpty($iptc['2#116'][0])) { $metadata['Copyright'] = \LonelyGallery\utf8ify($iptc['2#116'][0]); } else if (isset($exif['Copyright']) && !self::isEmpty($exif['Copyright'])) { $metadata['Copyright'] = \LonelyGallery\utf8ify($exif['Copyright']); } /* time */ if (isset($exif['DateTimeOriginal']) && !self::isEmpty($exif['DateTimeOriginal'])) { $metadata['Date and time'] = @date('j M Y, H:i', strtotime($exif['DateTimeOriginal'])); } else if (isset($iptc['2#055'][0]) && !self::isEmpty($iptc['2#055'][0])) { if (isset($iptc['2#060'][0]) && !self::isEmpty($iptc['2#060'][0])) { $metadata['Date and time'] = @date('j M Y, H:i', strtotime($exif['2#055'][0].(empty($iptc['2#060'][0]) ? '' : $exif['2#060'][0]))); } else { $metadata['Date'] = @date('j M Y', strtotime($exif['2#055'][0])); } } /* location */ $loc = array(); foreach (array('2#090', '2#095', '2#101') as $l) { if (isset($iptc[$l][0]) && !self::isEmpty($iptc[$l][0])) { $loc[] = \LonelyGallery\utf8ify($iptc[$l][0]); } } if (count($loc)) { $metadata['Location'] = implode(', ', $loc); } /* keywords */ if (isset($iptc['2#025'][0])) { $keywords = array(); foreach ($iptc['2#025'] as $keyword) { $keyword = trim(\LonelyGallery\utf8ify($keyword)); if ($keyword !== '') { $keywords[] = $keyword; } } if (count($keywords)) { $metadata['Keywords'] = implode(', ', $keywords); } } /* resolution */ if (!empty($exif['COMPUTED']['Width']) && !empty($exif['COMPUTED']['Height'])) { $metadata['Resolution'] = $exif['COMPUTED']['Width'].' x '.$exif['COMPUTED']['Height'].' px'; } else if (!empty($info[0]) && !empty($info[1])) { $metadata['Resolution'] = $info[0].' x '.$info[1].' px'; } /* filesize */ if (isset($exif['FileSize']) && !self::isEmpty($exif['FileSize'])) { $metadata['Filesize'] = round($exif['FileSize'] * 0.001, 2).' kB'; } else { $metadata['Filesize'] = round(filesize($location) * 0.001, 2).' kB'; } if (!empty(Lonely::model()->imageInfo_technical)) { /* camera model */ if (isset($exif['Model']) && !self::isEmpty($exif['Model'])) { $metadata['Camera'] = $exif['Model']; if (isset($exif['Make']) && !self::isEmpty($exif['Make'])) { $metadata['Camera'] .= ' ('.$exif['Make'].')'; } } /* exposure time */ if (isset($exif['ExposureTime']) && !self::isEmpty($exif['ExposureTime'])) { $metadata['Exposure time'] = $exif['ExposureTime'].' s'; } /* aperture */ if (!empty($exif['COMPUTED']['ApertureFNumber'])) { $metadata['Aperture'] = $exif['COMPUTED']['ApertureFNumber']; } /* ISO */ if (isset($exif['ISOSpeedRatings']) && !self::isEmpty($exif['ISOSpeedRatings'])) { $metadata['ISO'] = $exif['ISOSpeedRatings']; } /* flash */ if (isset($exif['Flash'])) { $metadata['Flash'] = ($exif['Flash'] & 1) ? 'yes' : 'no'; } } return $metadata; } } ?>