Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/getgrav/grav into 2.0
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.lock
  • Loading branch information
mahagr committed May 16, 2017
2 parents 4f27ff1 + d11099c commit 41c130c
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
## mm/dd/2017

1. [](#new)
* Added support for a single array field in the forms
* Added EXIF support with automatic generation of Page Media metafiles
* Added Twig function to get EXIF data on any image file
* Added `Pages::baseUrl()`, `Pages::homeUrl()` and `Pages::url()` functions
* Added `base32_encode`, `base32_decode`, `base64_encode`, `base64_decode` Twig filters
* Added `Debugger::getCaller()` to figure out where the method was called from
Expand All @@ -23,6 +26,7 @@
* Groups selection pre-filled in user form
* Improve error handling in `Folder::move()`
* Added extra parameter for `Twig::processSite()` to include custom context
* Updated RocketTheme Toolbox vendor library
1. [](#bugfix)
* Fix to force route/redirect matching from the start of the route by default [#1446](https://github.com/getgrav/grav/issues/1446)
* Edit check for valid slug [#1459](https://github.com/getgrav/grav/issues/1459)
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"ext-curl": "*",
"ext-zip": "*",
"league/climate": "^3.2",
"antoligy/dom-string-iterators": "^1.0"
"antoligy/dom-string-iterators": "^1.0",
"miljar/php-exif": "^0.6.3"
},
"require-dev": {
"codeception/codeception": "^2.1",
Expand Down
82 changes: 69 additions & 13 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions system/blueprints/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,17 @@ form:
validate:
type: bool

media.auto_metadata_exif:
type: toggle
label: PLUGIN_ADMIN.ENABLE_AUTO_METADATA
help: PLUGIN_ADMIN.ENABLE_AUTO_METADATA_HELP
highlight: 0
options:
1: PLUGIN_ADMIN.YES
0: PLUGIN_ADMIN.NO
validate:
type: bool



media.allowed_fallback_types:
Expand Down
3 changes: 2 additions & 1 deletion system/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,11 @@ images:
auto_fix_orientation: false # Automatically fix the image orientation based on the Exif data

media:
enable_media_timestamp: false # Enable media timetsamps
enable_media_timestamp: false # Enable media timestamps
upload_limit: 0 # Set maximum upload size in bytes (0 is unlimited)
unsupported_inline_types: [] # Array of supported media types to try to display inline
allowed_fallback_types: [] # Array of allowed media types of files found if accessed via Page route
auto_metadata_exif: false # Automatically create metadata files from Exif data where possible

session:
enabled: true # Enable Session support
Expand Down
1 change: 1 addition & 0 deletions system/src/Grav/Common/Grav.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Grav extends Container
'Grav\Common\Service\PageServiceProvider',
'Grav\Common\Service\OutputServiceProvider',
'browser' => 'Grav\Common\Browser',
'exif' => 'Grav\Common\Helpers\Exif',
'Grav\Common\Service\StreamsServiceProvider',
'Grav\Common\Service\ConfigServiceProvider',
'inflector' => 'Grav\Common\Inflector',
Expand Down
27 changes: 27 additions & 0 deletions system/src/Grav/Common/Helpers/Exif.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* @package Grav.Common.Helpers
*
* @copyright Copyright (C) 2014 - 2017 RocketTheme, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Helpers;

use Grav\Common\Grav;

class Exif
{
public $reader;

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')) {
throw new \Exception('Please enable the Exif extension for PHP or disable Exif support in Grav system configuration');
}
}
}
}
26 changes: 25 additions & 1 deletion system/src/Grav/Common/Page/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@

namespace Grav\Common\Page;

use Grav\Common\Grav;
use Grav\Common\Page\Medium\AbstractMedia;
use Grav\Common\Page\Medium\GlobalMedia;
use Grav\Common\Page\Medium\MediumFactory;
use RocketTheme\Toolbox\File\File;
use Symfony\Component\Yaml\Yaml;

class Media extends AbstractMedia
{
protected static $global;

protected $path;

protected $standard_exif = ['FileSize', 'MimeType', 'height', 'width'];

/**
* @param $path
*/
Expand Down Expand Up @@ -58,6 +63,8 @@ public function offsetGet($offset)
*/
protected function init()
{
$config = Grav::instance()['config'];
$exif = Grav::instance()['exif'];

// Handle special cases where page doesn't exist in filesystem.
if (!is_dir($this->path)) {
Expand All @@ -71,7 +78,7 @@ protected function init()
/** @var \DirectoryIterator $info */
foreach ($iterator as $path => $info) {
// Ignore folders and Markdown files.
if (!$info->isFile() || $info->getExtension() == 'md' || $info->getBasename()[0] === '.') {
if (!$info->isFile() || $info->getExtension() === 'md' || $info->getBasename()[0] === '.') {
continue;
}

Expand Down Expand Up @@ -116,6 +123,23 @@ protected function init()
continue;
}

// 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')) {
$file_path = $types['base']['file'];
$meta = $exif->reader->read($file_path);

if ($meta) {
$meta_path = $file_path . '.meta.yaml';
$meta_data = $meta->getData();
$meta_trimmed = array_diff_key($meta_data, array_flip($this->standard_exif));
if ($meta_trimmed) {
$file = File::instance($meta_path);
$file->save(Yaml::dump($meta_trimmed));
$types['meta']['file'] = $meta_path;
}
}
}

if (!empty($types['meta'])) {
$medium->addMetaFile($types['meta']['file']);
}
Expand Down
18 changes: 17 additions & 1 deletion system/src/Grav/Common/Page/Medium/Medium.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class Medium extends Data implements RenderableInterface
*/
protected $styleAttributes = [];

/**
* @var array
*/
protected $metadata = [];

/**
* Construct.
*
Expand Down Expand Up @@ -77,14 +82,25 @@ public function meta()
return new Data($this->items);
}

/**
* Returns an array containing just the metadata
*
* @return array
*/
public function metadata()
{
return $this->metadata;
}

/**
* Add meta file for the medium.
*
* @param $filepath
*/
public function addMetaFile($filepath)
{
$this->merge((array)CompiledYamlFile::instance($filepath)->content());
$this->metadata = (array)CompiledYamlFile::instance($filepath)->content();
$this->merge($this->metadata);
}

/**
Expand Down
26 changes: 25 additions & 1 deletion system/src/Grav/Common/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public function getFunctions()
new \Twig_SimpleFunction('redirect_me', [$this, 'redirectFunc']),
new \Twig_SimpleFunction('range', [$this, 'rangeFunc']),
new \Twig_SimpleFunction('isajaxrequest', [$this, 'isAjaxFunc']),
new \Twig_SimpleFunction('exif', [$this, 'exifFunc']),
];
}

Expand All @@ -137,7 +138,7 @@ public function getFunctions()
*/
public function fieldNameFilter($str)
{
$path = explode('.', $str);
$path = explode('.', rtrim($str, '.'));

return array_shift($path) . ($path ? '[' . implode('][', $path) . ']' : '');
}
Expand Down Expand Up @@ -956,4 +957,27 @@ public function isAjaxFunc()
!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
}

/**
* Get's the Exif data for a file
*
* @param $image
* @param bool $raw
* @return mixed
*/
public function exifFunc($image, $raw = false)
{
if (file_exists($image)) {

$exif_data = $this->grav['exif']->reader->read($image);

if ($exif_data) {
if ($raw) {
return $exif_data->getRawData();
} else {
return $exif_data->getData();
}
}
}
}
}
Loading

0 comments on commit 41c130c

Please sign in to comment.