/**
  * Get orientation of an image from the supplied Image object.
  * NOTE: This function doesn't read EXIF data of the file to detect orientation
  * data.
  *
  * @param Image $image
  *
  * @return ImageOrientation
  */
 protected function getImageOrientation(Image $image)
 {
     if (!$image->isHydrated()) {
         throw new ImageManagerException(ImageManager::ERR_NOT_HYDRATED);
     }
     $img = new \Imagick();
     $img->readImageBlob($image->getData());
     $width = $img->getImageWidth();
     $height = $img->getImageHeight();
     if ($width >= $height) {
         return ImageOrientation::LANDSCAPE();
     }
     return ImageOrientation::PORTRAIT();
 }
Beispiel #2
0
 /**
  * {@inheritdoc}
  */
 public static function deserialise($json)
 {
     if (empty($json)) {
         throw new InvalidImageMetadataException();
     }
     $object_data = json_decode($json, true);
     // MIME-type is an minimum requirement for metadata
     if (!isset($object_data['mimetype'])) {
         throw new InvalidImageMetadataException();
     }
     $instance = new static();
     $instance->setMimeType($object_data['mimetype'])->setFormat(isset($object_data['format']) ? ImageFormat::memberByValue($object_data['format']) : null)->setResolution(isset($object_data['resolution']) ? ImageDimensions::deserialise($object_data['resolution']) : null)->setOrientation(isset($object_data['orientation']) ? ImageOrientation::memberByValue($object_data['orientation']) : null)->setDimensions(isset($object_data['dimensions']) ? ImageDimensions::deserialise($object_data['dimensions']) : null);
     return $instance;
 }
 public function testMetadataRetrieval()
 {
     $inspector = new ImageInspector();
     $fn = __DIR__ . '/../Resources/image.png';
     $im = new ImageManager(new Filesystem(new LocalAdapter(static::$tmp_dir . 'remote')), new EphemeralCachePool(), [], true);
     $image = $im->loadFromFile($fn, self::TEST_KEY);
     $im->push($image);
     $metadata = $inspector->getImageMetadata($image);
     $this->assertEquals($metadata->getFormat(), ImageFormat::PNG());
     $this->assertEquals($metadata->getDimensions(), new ImageDimensions(300, 300));
     $this->assertEquals($metadata->getResolution(), new ImageDimensions(72, 72));
     $this->assertEquals($metadata->getOrientation(), ImageOrientation::LANDSCAPE());
 }