Added: Incorrect image orientation

This commit is contained in:
RainLoop Team 2015-10-29 23:02:58 +03:00
parent 217207883f
commit e3b62d24d8
87 changed files with 10461 additions and 21 deletions

View file

@ -0,0 +1,101 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Metadata;
use Imagine\Exception\InvalidArgumentException;
abstract class AbstractMetadataReader implements MetadataReaderInterface
{
/**
* {@inheritdoc}
*/
public function readFile($file)
{
if (stream_is_local($file)) {
if (!is_file($file)) {
throw new InvalidArgumentException(sprintf('File %s does not exist.', $file));
}
return new MetadataBag(array_merge(array('filepath' => realpath($file), 'uri' => $file), $this->extractFromFile($file)));
}
return new MetadataBag(array_merge(array('uri' => $file), $this->extractFromFile($file)));
}
/**
* {@inheritdoc}
*/
public function readData($data)
{
return new MetadataBag($this->extractFromData($data));
}
/**
* {@inheritdoc}
*/
public function readStream($resource)
{
if (!is_resource($resource)) {
throw new InvalidArgumentException('Invalid resource provided.');
}
return new MetadataBag(array_merge($this->getStreamMetadata($resource), $this->extractFromStream($resource)));
}
/**
* Gets the URI from a stream resource
*
* @param resource $resource
*
* @return string|null The URI f ava
*/
private function getStreamMetadata($resource)
{
$metadata = array();
if (false !== $data = @stream_get_meta_data($resource)) {
$metadata['uri'] = $data['uri'];
if (stream_is_local($resource)) {
$metadata['filepath'] = realpath($data['uri']);
}
}
return $metadata;
}
/**
* Extracts metadata from a file
*
* @param $file
*
* @return array An associative array of metadata
*/
abstract protected function extractFromFile($file);
/**
* Extracts metadata from raw data
*
* @param $data
*
* @return array An associative array of metadata
*/
abstract protected function extractFromData($data);
/**
* Extracts metadata from a stream
*
* @param $resource
*
* @return array An associative array of metadata
*/
abstract protected function extractFromStream($resource);
}

View file

@ -0,0 +1,42 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Metadata;
/**
* Default metadata reader
*/
class DefaultMetadataReader extends AbstractMetadataReader
{
/**
* {@inheritdoc}
*/
protected function extractFromFile($file)
{
return array();
}
/**
* {@inheritdoc}
*/
protected function extractFromData($data)
{
return array();
}
/**
* {@inheritdoc}
*/
protected function extractFromStream($resource)
{
return array();
}
}

View file

@ -0,0 +1,114 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Metadata;
use Imagine\Exception\InvalidArgumentException;
use Imagine\Exception\NotSupportedException;
/**
* Metadata driven by Exif information
*/
class ExifMetadataReader extends AbstractMetadataReader
{
public function __construct()
{
if (!self::isSupported()) {
throw new NotSupportedException('PHP exif extension is required to use the ExifMetadataReader');
}
}
public static function isSupported()
{
return function_exists('exif_read_data');
}
/**
* {@inheritdoc}
*/
protected function extractFromFile($file)
{
if (false === $data = @file_get_contents($file)) {
throw new InvalidArgumentException(sprintf('File %s is not readable.', $file));
}
return $this->doReadData($data);
}
/**
* {@inheritdoc}
*/
protected function extractFromData($data)
{
return $this->doReadData($data);
}
/**
* {@inheritdoc}
*/
protected function extractFromStream($resource)
{
if (0 < ftell($resource)) {
$metadata = stream_get_meta_data($resource);
if ($metadata['seekable']) {
rewind($resource);
}
}
return $this->doReadData(stream_get_contents($resource));
}
/**
* Extracts metadata from raw data, merges with existing metadata
*
* @param string $data
*
* @return MetadataBag
*/
private function doReadData($data)
{
if (substr($data, 0, 2) === 'II') {
$mime = 'image/tiff';
} else {
$mime = 'image/jpeg';
}
return $this->extract('data://' . $mime . ';base64,' . base64_encode($data));
}
/**
* Performs the exif data extraction given a path or data-URI representation.
*
* @param string $path The path to the file or the data-URI representation.
*
* @return MetadataBag
*/
private function extract($path)
{
if (false === $exifData = @exif_read_data($path, null, true, false)) {
return array();
}
$metadata = array();
$sources = array('EXIF' => 'exif', 'IFD0' => 'ifd0');
foreach ($sources as $name => $prefix) {
if (!isset($exifData[$name])) {
continue;
}
foreach ($exifData[$name] as $prop => $value) {
$metadata[$prefix.'.'.$prop] = $value;
}
}
return $metadata;
}
}

View file

@ -0,0 +1,97 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Metadata;
/**
* An interface for Image Metadata
*/
class MetadataBag implements \ArrayAccess, \IteratorAggregate, \Countable
{
/** @var array */
private $data;
public function __construct(array $data = array())
{
$this->data = $data;
}
/**
* Returns the metadata key, default value if it does not exist
*
* @param string $key
* @param mixed|null $default
*
* @return mixed
*/
public function get($key, $default = null)
{
return array_key_exists($key, $this->data) ? $this->data[$key] : $default;
}
/**
* {@inheritdoc}
*/
public function count()
{
return count($this->data);
}
/**
* {@inheritdoc}
*/
public function getIterator()
{
return new \ArrayIterator($this->data);
}
/**
* {@inheritdoc}
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->data);
}
/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value)
{
$this->data[$offset] = $value;
}
/**
* {@inheritdoc}
*/
public function offsetUnset($offset)
{
unset($this->data[$offset]);
}
/**
* {@inheritdoc}
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* Returns metadata as an array
*
* @return array An associative array
*/
public function toArray()
{
return $this->data;
}
}

View file

@ -0,0 +1,48 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Metadata;
use Imagine\Exception\InvalidArgumentException;
interface MetadataReaderInterface
{
/**
* Reads metadata from a file.
*
* @param $file The path to the file where to read metadata.
*
* @throws InvalidArgumentException In case the file does not exist.
*
* @return MetadataBag
*/
public function readFile($file);
/**
* Reads metadata from a binary string.
*
* @param $data The binary string to read.
*
* @return MetadataBag
*/
public function readData($data);
/**
* Reads metadata from a stream.
*
* @param $resource The stream to read.
*
* @throws InvalidArgumentException In case the resource is not valid.
*
* @return MetadataBag
*/
public function readStream($resource);
}