vendor/imagine/imagine/src/Image/Metadata/AbstractMetadataReader.php line 28

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Imagine package.
  4.  *
  5.  * (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Imagine\Image\Metadata;
  11. use Imagine\Exception\InvalidArgumentException;
  12. use Imagine\File\Loader;
  13. use Imagine\File\LoaderInterface;
  14. /**
  15.  * Base class for the default metadata readers.
  16.  */
  17. abstract class AbstractMetadataReader implements MetadataReaderInterface
  18. {
  19.     /**
  20.      * {@inheritdoc}
  21.      *
  22.      * @see \Imagine\Image\Metadata\MetadataReaderInterface::readFile()
  23.      */
  24.     public function readFile($file)
  25.     {
  26.         $loader $file instanceof LoaderInterface $file : new Loader($file);
  27.         return new MetadataBag(array_merge($this->getStreamMetadata($loader), $this->extractFromFile($loader)));
  28.     }
  29.     /**
  30.      * {@inheritdoc}
  31.      *
  32.      * @see \Imagine\Image\Metadata\MetadataReaderInterface::readData()
  33.      */
  34.     public function readData($data$originalResource null)
  35.     {
  36.         if (null !== $originalResource) {
  37.             return new MetadataBag(array_merge($this->getStreamMetadata($originalResource), $this->extractFromData($data)));
  38.         }
  39.         return new MetadataBag($this->extractFromData($data));
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      *
  44.      * @see \Imagine\Image\Metadata\MetadataReaderInterface::readStream()
  45.      */
  46.     public function readStream($resource)
  47.     {
  48.         if (!is_resource($resource)) {
  49.             throw new InvalidArgumentException('Invalid resource provided.');
  50.         }
  51.         return new MetadataBag(array_merge($this->getStreamMetadata($resource), $this->extractFromStream($resource)));
  52.     }
  53.     /**
  54.      * Gets the URI from a stream resource.
  55.      *
  56.      * @param resource|\Imagine\File\LoaderInterface $resource
  57.      *
  58.      * @return array
  59.      */
  60.     private function getStreamMetadata($resource)
  61.     {
  62.         $metadata = array();
  63.         if ($resource instanceof LoaderInterface) {
  64.             $metadata['uri'] = $resource->getPath();
  65.             if ($resource->isLocalFile()) {
  66.                 $metadata['filepath'] = realpath($resource->getPath());
  67.             }
  68.         } elseif (false !== $data = @stream_get_meta_data($resource)) {
  69.             if (isset($data['uri'])) {
  70.                 $metadata['uri'] = $data['uri'];
  71.                 if (stream_is_local($resource)) {
  72.                     $metadata['filepath'] = realpath($data['uri']);
  73.                 }
  74.             }
  75.         }
  76.         return $metadata;
  77.     }
  78.     /**
  79.      * Extracts metadata from a file.
  80.      *
  81.      * @param string|\Imagine\File\LoaderInterface $file
  82.      *
  83.      * @return array An associative array of metadata
  84.      */
  85.     abstract protected function extractFromFile($file);
  86.     /**
  87.      * Extracts metadata from raw data.
  88.      *
  89.      * @param $data
  90.      *
  91.      * @return array An associative array of metadata
  92.      */
  93.     abstract protected function extractFromData($data);
  94.     /**
  95.      * Extracts metadata from a stream.
  96.      *
  97.      * @param $resource
  98.      *
  99.      * @return array An associative array of metadata
  100.      */
  101.     abstract protected function extractFromStream($resource);
  102. }