Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function createVariation(ImageFormat $output_format, $quality, ImageDimensions $dimensions = null, ImageCropDimensions $crop_dimensions = null)
 {
     try {
         $img = new InterventionImage($this->data);
     } catch (\Intervention\Image\Exception\InvalidImageDataStringException $e) {
         throw new BadImageException('Bad image data', 0, $e);
     } catch (\Intervention\Image\Exception\ImageNotFoundException $e) {
         throw new BadImageException('Not an image', 0, $e);
     }
     if ($dimensions) {
         if ($dimensions->getGrab()) {
             $img->grab($dimensions->getWidth(), $dimensions->getHeight());
         } else {
             $img->resize($dimensions->getWidth(), $dimensions->getHeight(), $dimensions->getMaintainRatio(), $dimensions->canUpscale());
         }
     }
     return $img->encode($output_format->key(), $quality);
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function createVariation(ImageFormat $output_format, $quality, ImageDimensions $dimensions = null, ImageCropDimensions $crop_dimensions = null)
 {
     $src = $this->getTempFile($this->data);
     $img = new \Imagick();
     $img->setResolution($this->resolution, $this->resolution);
     $img->readImage($src);
     $img->setIteratorIndex(0);
     // Flatten images here helps the encoder to get rid of the black background
     // that appears on encoded image files.
     $img = $img->flattenImages();
     $img->setImageFormat((string) $output_format->value());
     $img->setImageCompressionQuality($quality);
     if (null !== $crop_dimensions) {
         $img->cropImage($crop_dimensions->getWidth(), $crop_dimensions->getHeight(), $crop_dimensions->getX(), $crop_dimensions->getY());
     }
     if (null !== $dimensions) {
         $img->resizeImage($dimensions->getWidth() ?: 0, $dimensions->getHeight() ?: 0, $this->filter, 1, false);
     }
     return $img->getImageBlob();
 }
Ejemplo n.º 3
0
 /**
  * Test to see if we can determine the image format.
  *
  * @param string $data
  *
  * @return ImageFormat|null
  */
 public function getImageFormat(&$data)
 {
     if (!$data || strlen($data) < 5) {
         return null;
     }
     // JPEG: FF D8
     if (ord($data[0]) == 0xff && ord($data[1]) == 0xd8) {
         return ImageFormat::JPEG();
     }
     // PNG: 89 50 4E 47
     if (ord($data[0]) == 0x89 && substr($data, 1, 3) == 'PNG') {
         return ImageFormat::PNG();
     }
     // GIF87a: 47 49 46 38 37 61
     // GIF89a: 47 49 46 38 39 61
     if (substr($data, 0, 6) == 'GIF87a' || substr($data, 0, 6) == 'GIF89a') {
         return ImageFormat::GIF();
     }
     return null;
 }
Ejemplo n.º 4
0
 /**
  * @param Image $image
  *
  * @return ImageMetadata
  */
 public function getImageMetadata(Image $image)
 {
     if (!$image->isHydrated()) {
         throw new ImageManagerException(ImageManager::ERR_NOT_HYDRATED);
     }
     if ($image instanceof ImageVariation) {
         throw new ImageManagerException(self::ERR_SOURCE_IMAGE);
     }
     $metadata = new ImageMetadata();
     $data_inspector = new DataInspector();
     $data = $image->getData();
     if ($data_inspector->isPdf($data)) {
         $format = ImageFormat::PDF();
     } else {
         $format = $data_inspector->getImageFormat($data);
     }
     $metadata->setMimetype($data_inspector->guessMimeType($data))->setFormat($format)->setResolution($this->getImageResolution($image))->setOrientation($this->getImageOrientation($image))->setDimensions($this->getImageDimensions($image));
     return $metadata;
 }
Ejemplo n.º 5
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());
 }