/** * Internal method to create the size * @param ImageReferencedElement $entity * @param EntityManager $em */ protected function createImageSize(ImageReferencedElement $entity, EntityManager $em) { $imageId = $entity->getImageId(); $fileStorage = $this->container['cms.file_storage']; /* @var $fileStorage \Supra\Package\Cms\FileStorage\FileStorage */ $image = $fileStorage->findImage($imageId); if ($image === null) { $this->container->getLogger()->warn("Image [{$imageId}] was not found inside the file storage." . " Maybe another file storage must be configured for the image size creator listener?"); return false; } $width = $entity->getWidth(); $height = $entity->getHeight(); // No dimensions if ($width > 0 && $height > 0 || $entity->isCropped()) { if ($entity->isCropped()) { $sizeName = $fileStorage->createImageVariant($image, $width, $height, $entity->getCropLeft(), $entity->getCropTop(), $entity->getCropWidth(), $entity->getCropHeight()); } else { $sizeName = $fileStorage->createResizedImage($image, $width, $height); } $entity->setSizeName($sizeName); // Maybe could update to real width/height inside image metadata? // $size = $image->getImageSize($sizeName); // $entity->setWidth($size->getWidth()); // $entity->setHeight($size->getHeight()); // Recalculate the changeset because of changed size name field $class = $em->getClassMetadata(ImageReferencedElement::CN()); $unitOfWork = $em->getUnitOfWork(); $unitOfWork->recomputeSingleEntityChangeSet($class, $entity); } }
/** * {@inheritDoc} */ public function reverseTransform($value) { $metadataCollection = $this->property->getMetadata(); // @FIXME: absolutely not performance-wise $metadataCollection->clear(); if ($value && is_array($value)) { // @TODO: some input data validation is needed. foreach (array_values($value) as $key => $itemData) { if (!isset($itemData['image']) || !is_array($itemData['image'])) { throw new TransformationFailedException(sprintf('Missing image data for item [%s]', $key)); } $imageData = $itemData['image']; if (empty($imageData['id'])) { throw new TransformationFailedException(sprintf('Image ID is missing for element [%s].', $key)); } $image = $this->getFileStorage()->findImage($imageData['id']); if ($image === null) { throw new TransformationFailedException(sprintf('Image [%s] not found in file storage.', $imageData['id'])); } $element = new ImageReferencedElement(); $element->fillFromArray($imageData); if (!empty($itemData['title'])) { $element->setTitle($itemData['title']); } if (!empty($itemData['description'])) { $element->setDescription($itemData['description']); } $metadataCollection->set($key, new BlockPropertyMetadata($key, $this->property, $element)); } } return null; }
/** * @param array $data * @return null */ protected function reverseTransformImageData(array $data) { $element = new ImageReferencedElement(); // @TODO: some data validation must happen here. $element->fillFromArray($data); $this->getMediaMetadata()->setReferencedElement($element); }
public function reverseTransform($value) { if (empty($value)) { $this->property->getMetadata()->remove('image'); return null; } $metadata = $this->property->getMetadata(); if (!$metadata->offsetExists('image')) { $metadata->set('image', new BlockPropertyMetadata('image', $this->property)); } $metaItem = $metadata->get('image'); /* @var $metaItem BlockPropertyMetadata */ $element = new ImageReferencedElement(); // @TODO: some data validation must happen here. $element->fillFromArray($value); $metaItem->setReferencedElement($element); return null; }
/** * @param ImageReferencedElement $element * @return null|string */ protected function handleImageElement(ImageReferencedElement $element) { $imageId = $element->getImageId(); $fileStorage = $this->container['cms.file_storage']; /* @var $fileStorage \Supra\Package\Cms\FileStorage\FileStorage */ $image = $fileStorage->findImage($imageId); if ($image === null) { return null; } $imageSize = $image->findImageSize($element->getSizeName()); if ($imageSize === null) { return null; } $tag = new HtmlTag('img'); $width = $imageSize->isCropped() ? $imageSize->getCropWidth() : $imageSize->getWidth(); $tag->setAttribute('width', $width); $height = $imageSize->isCropped() ? $imageSize->getCropHeight() : $imageSize->getCropHeight(); $tag->setAttribute('height', $height); $tag->setAttribute('alt', trim($element->getAlternateText())); $tag->setAttribute('src', $fileStorage->getWebPath($image, $imageSize)); return $tag; }
/** * Parse supra.image * * @param ImageReferencedElement $imageData * @return null|HtmlTag */ protected function parseSupraImage(ImageReferencedElement $imageData) { $imageId = $imageData->getImageId(); $fileStorage = $this->container['cms.file_storage']; /* @var $fileStorage \Supra\Package\Cms\FileStorage\FileStorage */ $image = $fileStorage->findImage($imageId); if ($image === null) { return null; } $sizeName = $imageData->getSizeName(); $size = $image->findImageSize($sizeName); if ($size === null) { $this->container->getLogger()->warn("Image [{$imageId}] size [{$sizeName}] not found."); return null; } $tag = new HtmlTag('img'); if ($size->isCropped()) { $width = $size->getCropWidth(); $height = $size->getCropHeight(); } else { $width = $size->getWidth(); $height = $size->getHeight(); } $src = $fileStorage->getWebPath($image, $size); $tag->setAttribute('src', $src); $align = $imageData->getAlign(); if (!empty($align)) { if ($align === 'left') { $tag->addClass('pull-left'); } else { if ($align === 'right') { $tag->addClass('pull-right'); } else { if ($align === 'center') { $tag->addClass('center-block'); $tag->setAttribute('style', "width: {$width}px;"); } } } } if (!empty($width)) { $tag->setAttribute('width', $width); } if (!empty($height)) { $tag->setAttribute('height', $height); } $title = trim($imageData->getTitle()); if (!empty($title)) { $tag->setAttribute('title', $title); } $tag->setAttribute('alt', trim($imageData->getAlternateText())); return $tag; }