mirror of
https://github.com/getgrav/grav.git
synced 2026-03-04 11:31:43 +01:00
Various checks for Exif existence before using
This commit is contained in:
10
CHANGELOG.md
10
CHANGELOG.md
@@ -1,5 +1,13 @@
|
||||
# v1.3.0-rc.2
|
||||
## 05/17/2017
|
||||
|
||||
1. [](#new)
|
||||
* Added new `media` and `vardump` Twig functions
|
||||
1. [](#improved)
|
||||
* Put in various checks to ensure Exif is available before trying to use
|
||||
|
||||
# v1.3.0-rc.1
|
||||
## 15/16/2017
|
||||
## 05/16/2017
|
||||
|
||||
1. [](#new)
|
||||
* Added support for a single array field in the forms
|
||||
|
||||
@@ -16,12 +16,21 @@ class Exif
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
if (function_exists('exif_read_data') && class_exists('\PHPExif\Reader\Reader')) {
|
||||
$this->reader = \PHPExif\Reader\Reader::factory(\PHPExif\Reader\Reader::TYPE_NATIVE);
|
||||
} else {
|
||||
if (Grav::instance()['config']->get('system.media.auto_metadata_exif')) {
|
||||
if (Grav::instance()['config']->get('system.media.auto_metadata_exif')) {
|
||||
if (function_exists('exif_read_data') && class_exists('\PHPExif\Reader\Reader')) {
|
||||
$this->reader = \PHPExif\Reader\Reader::factory(\PHPExif\Reader\Reader::TYPE_NATIVE);
|
||||
} else {
|
||||
throw new \Exception('Please enable the Exif extension for PHP or disable Exif support in Grav system configuration');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getReader()
|
||||
{
|
||||
if ($this->reader) {
|
||||
return $this->reader;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ class Media extends AbstractMedia
|
||||
protected function init()
|
||||
{
|
||||
$config = Grav::instance()['config'];
|
||||
$exif = Grav::instance()['exif'];
|
||||
$exif_reader = isset(Grav::instance()['exif']) ? Grav::instance()['exif']->getReader() : false;
|
||||
|
||||
// Handle special cases where page doesn't exist in filesystem.
|
||||
if (!is_dir($this->path)) {
|
||||
@@ -124,9 +124,9 @@ class Media extends AbstractMedia
|
||||
}
|
||||
|
||||
// Read/store Exif metadata as required
|
||||
if (!empty($types['base']) && $medium->get('mime') === 'image/jpeg' && empty($types['meta']) && $config->get('system.media.auto_metadata_exif')) {
|
||||
if (!empty($types['base']) && $medium->get('mime') === 'image/jpeg' && empty($types['meta']) && $config->get('system.media.auto_metadata_exif') && $exif_reader) {
|
||||
$file_path = $types['base']['file'];
|
||||
$meta = $exif->reader->read($file_path);
|
||||
$meta = $exif_reader->read($file_path);
|
||||
|
||||
if ($meta) {
|
||||
$meta_path = $file_path . '.meta.yaml';
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
namespace Grav\Common\Twig;
|
||||
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Page\Media;
|
||||
use Grav\Common\Utils;
|
||||
use Grav\Common\Markdown\Parsedown;
|
||||
use Grav\Common\Markdown\ParsedownExtra;
|
||||
@@ -109,6 +110,7 @@ class TwigExtension extends \Twig_Extension
|
||||
new \Twig_simpleFunction('authorize', [$this, 'authorize']),
|
||||
new \Twig_SimpleFunction('debug', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]),
|
||||
new \Twig_SimpleFunction('dump', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]),
|
||||
new \Twig_SimpleFunction('vardump', [$this, 'vardumpFunc']),
|
||||
new \Twig_SimpleFunction('evaluate', [$this, 'evaluateStringFunc'], ['needs_context' => true, 'needs_environment' => true]),
|
||||
new \Twig_SimpleFunction('evaluate_twig', [$this, 'evaluateTwigFunc'], ['needs_context' => true, 'needs_environment' => true]),
|
||||
new \Twig_SimpleFunction('gist', [$this, 'gistFunc']),
|
||||
@@ -126,6 +128,8 @@ class TwigExtension extends \Twig_Extension
|
||||
new \Twig_SimpleFunction('range', [$this, 'rangeFunc']),
|
||||
new \Twig_SimpleFunction('isajaxrequest', [$this, 'isAjaxFunc']),
|
||||
new \Twig_SimpleFunction('exif', [$this, 'exifFunc']),
|
||||
new \Twig_SimpleFunction('media', [$this, 'mediaFunc']),
|
||||
|
||||
];
|
||||
}
|
||||
|
||||
@@ -967,15 +971,27 @@ class TwigExtension extends \Twig_Extension
|
||||
*/
|
||||
public function exifFunc($image, $raw = false)
|
||||
{
|
||||
if (file_exists($image)) {
|
||||
if (isset($this->grav['exif'])) {
|
||||
|
||||
$exif_data = $this->grav['exif']->reader->read($image);
|
||||
/** @var UniformResourceLocator $locator */
|
||||
$locator = $this->grav['locator'];
|
||||
|
||||
if ($exif_data) {
|
||||
if ($raw) {
|
||||
return $exif_data->getRawData();
|
||||
} else {
|
||||
return $exif_data->getData();
|
||||
if ($locator->isStream($image)) {
|
||||
$image = $locator->findResource($image);
|
||||
}
|
||||
|
||||
$exif_reader = $this->grav['exif']->getReader();
|
||||
|
||||
if (file_exists($image) && $this->config->get('system.media.auto_metadata_exif') && $exif_reader) {
|
||||
|
||||
$exif_data = $exif_reader->read($image);
|
||||
|
||||
if ($exif_data) {
|
||||
if ($raw) {
|
||||
return $exif_data->getRawData();
|
||||
} else {
|
||||
return $exif_data->getData();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user