/** * {@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(); }
/** * {@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); }
/** * {@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; }