getThumbnails example

$searchCriteria = new Criteria();
        $searchCriteria->setLimit(1);
        $searchCriteria->addFilter(new EqualsFilter('media.id', $media->getId()));
        $searchCriteria->addAssociation('mediaFolder.configuration.mediaThumbnailSizes');

        $mediaResult = $this->mediaRepository->search($searchCriteria$this->context);

        /** @var MediaEntity $updatedMedia */
        $updatedMedia = $mediaResult->getEntities()->first();

        $thumbnails = $updatedMedia->getThumbnails();
        static::assertInstanceOf(MediaThumbnailCollection::class$thumbnails);
        static::assertEquals(2, $thumbnails->count());

        foreach ($thumbnails as $thumbnail) {
            $thumbnailPath = $this->urlGenerator->getRelativeThumbnailUrl(
                $media,
                $thumbnail
            );

            $folder = $updatedMedia->getMediaFolder();
            static::assertInstanceOf(MediaFolderEntity::class$folder);
            
if (!$blog instanceof Blog) {
                        break;
                    }
                    $medias = $blog->getMedias();
                    $media = array_shift($medias);
                    if (!$media instanceof Media) {
                        break;
                    }

                    $element->getData()->set('blog', $blog);
                    $element->getData()->set('image', $media);
                    $element->getData()->set('images', $media->getThumbnails());
                    break;
                }

                $products = $collection->getBatchResult()->get($key);
                shuffle($products);

                $product = reset($products);

                if (!$product instanceof ListProduct || !$product->getCover() instanceof Media) {
                    break;
                }

                
$input = new StringInput('');
        $output = new BufferedOutput();

        $this->runCommand($this->thumbnailCommand, $input$output);

        $string = $output->fetch();
        static::assertMatchesRegularExpression('/.*Generated\s*2.*/', $string);
        static::assertMatchesRegularExpression('/.*Skipped\s*' . \count($this->initialMediaIds) . '.*/', $string);

        $medias = $this->getNewMediaEntities();
        foreach ($medias as $updatedMedia) {
            $thumbnails = $updatedMedia->getThumbnails();
            static::assertNotNull($thumbnails);
            static::assertEquals(
                2,
                $thumbnails->count()
            );

            foreach ($thumbnails as $thumbnail) {
                $this->assertThumbnailExists($updatedMedia$thumbnail);
            }
        }
    }

    
foreach ($media as $mediaEntity) {
            if (!$mediaEntity->hasFile()) {
                continue;
            }

            if ($mediaEntity->isPrivate()) {
                $privatePaths[] = $this->urlGenerator->getRelativeMediaUrl($mediaEntity);
            } else {
                $publicPaths[] = $this->urlGenerator->getRelativeMediaUrl($mediaEntity);
            }

            if (!$mediaEntity->getThumbnails()) {
                continue;
            }

            foreach ($mediaEntity->getThumbnails()->getIds() as $id) {
                $thumbnails[] = ['id' => $id];
            }
        }

        $this->performFileDelete($context$publicPaths, Visibility::PUBLIC);
        $this->performFileDelete($context$privatePaths, Visibility::PRIVATE);

        
$this->productRepository->create([$data]$context);
        $criteria = new Criteria([$data['id']]);
        $criteria->addAssociation('cover');
        $results = $this->productRepository->search($criteria$context);

        /** @var ProductEntity $product */
        $product = $results->first();

        static::assertNotNull($product, 'Product has not been created.');
        static::assertNotNull($product->getCover(), 'Cover was not fetched.');
        static::assertNotNull($product->getCover()->getMedia(), 'Media for cover was not fetched.');
        $mediaThumbnailCollection = $product->getCover()->getMedia()->getThumbnails();
        static::assertNotNull($mediaThumbnailCollection);
        static::assertCount(3, $mediaThumbnailCollection->getElements(), 'Thumbnails were not fetched or is incomplete.');
    }

    public function testAddTranslationsAssociation(): void
    {
        $repo = $this->getContainer()->get('category.repository');

        $id = Uuid::randomHex();

        $cats = [
            [
private readonly EntityRepository $mediaFolderRepository
    ) {
    }

    public function generate(MediaCollection $collection, Context $context): int
    {
        $delete = [];

        $generate = [];

        foreach ($collection as $media) {
            if ($media->getThumbnails() === null) {
                throw MediaException::thumbnailAssociationNotLoaded();
            }

            if (!$this->mediaCanHaveThumbnails($media$context)) {
                $delete = [...$delete, ...$media->getThumbnails()->getIds()];

                continue;
            }

            $mediaFolder = $media->getMediaFolder();
            if ($mediaFolder === null) {
                
$mediaEntity->setFileExtension('png');
        $mediaEntity->setFileName($mediaId . '-134578345');
        $mediaEntity->setThumbnails(new MediaThumbnailCollection());

        $mediaLoadedEvent = new EntityLoadedEvent($this->getContainer()->get(MediaDefinition::class)[$mediaEntity]$context);
        $subscriber->addUrls($mediaLoadedEvent);

        static::assertStringEndsWith(
            $mediaEntity->getFileName() . '.' . $mediaEntity->getFileExtension(),
            $mediaEntity->getUrl()
        );
        static::assertEquals([]$mediaEntity->getThumbnails()->getElements());
    }

    public function testItAddsThumbnailUrl(): void
    {
        $subscriber = $this->getContainer()->get(MediaLoadedSubscriber::class);
        $context = Context::createDefaultContext();

        $mediaId = '34556f108ab14969a0dcf9d9522fd7df';
        $mimeType = 'image/png';

        $thumbnailEntity = new MediaThumbnailEntity();
        
 else {
            $msg->withContext($this->context);
        }

        $this->handler->__invoke($msg);

        $criteria = new Criteria([$media->getId()]);
        $criteria->addAssociation('thumbnails');

        /** @var MediaEntity $media */
        $media = $this->mediaRepository->search($criteria$this->context)->get($media->getId());
        $mediaThumbnailCollection = $media->getThumbnails();
        static::assertNotNull($mediaThumbnailCollection);
        static::assertEquals(2, $mediaThumbnailCollection->count());

        foreach ($mediaThumbnailCollection as $thumbnail) {
            static::assertTrue(
                ($thumbnail->getWidth() === 300 && $thumbnail->getHeight() === 300)
                || ($thumbnail->getWidth() === 150 && $thumbnail->getHeight() === 150)
            );

            $path = $this->urlGenerator->getRelativeThumbnailUrl($media$thumbnail);
            static::assertTrue(
                

        static::assertNull($strategy->generatePathCacheBuster($this->getMediaWithManufacturer()));
        static::assertSame('1293894181', $strategy->generatePathCacheBuster($this->getPngWithoutExtension()));
    }

    private function assertFilenameGenerator(PathnameStrategyInterface $strategy): void
    {
        $jpg = $this->getJpg();
        $mediaWithThumbnail = $this->getMediaWithThumbnail();

        static::assertSame('jpgFileWithExtension.jpg', $strategy->generatePhysicalFilename($jpg));
        static::assertSame('jpgFileWithExtension_200x200.jpg', $strategy->generatePhysicalFilename($jpg$mediaWithThumbnail->getThumbnails()->first()));
    }
}
public function testThumbnailsAreConvertedToStructWhenFetchedFromDb(): void
    {
        $this->setFixtureContext($this->context);
        $media = $this->getMediaWithThumbnail();

        $criteria = $this->getIdCriteria($media->getId());
        $searchResult = $this->repository->search($criteria$this->context);
        $fetchedMedia = $searchResult->getEntities()->get($media->getId());

        static::assertInstanceOf(MediaEntity::class$fetchedMedia);
        static::assertInstanceOf(MediaThumbnailCollection::class$fetchedMedia->getThumbnails());

        $persistedThumbnail = $fetchedMedia->getThumbnails()->first();
        static::assertInstanceOf(MediaThumbnailEntity::class$persistedThumbnail);
        static::assertEquals(200, $persistedThumbnail->getWidth());
        static::assertEquals(200, $persistedThumbnail->getHeight());
    }

    public function testDeleteMediaWithTags(): void
    {
        $media = $this->getEmptyMedia();

        


        $mediaID = $this->Request()->getParam('mediaID');
        $query = $this->getMedia($mediaID)->getQuery();
        /** @var Media[] $mediaArray */
        $mediaArray = $query->getResult();
        $data = $query->getArrayResult();
        $data = $data[0];
        $media = $mediaArray[0];

        if ($media->getType() === Media::TYPE_IMAGE) {
            $thumbnails = $media->getThumbnails();
            $data['path'] = $thumbnails['153x153'];
        }

        $this->View()->assign(['success' => true, 'data' => $data, 'total' => 1]);
    }

    /** * Controller action which allows to request the media * data over a mediaId parameter or over the path property of a media model. * * @retrun void */
$document = $documentRepository->search($criteria$context);
        });
        static::assertNotNull($document);
        static::assertEquals(1, $document->count());
        $document = $document->get($documentId);
        static::assertInstanceOf(DocumentEntity::class$document);
        $media = $document->getDocumentMediaFile();
        static::assertInstanceOf(MediaEntity::class$media);
        static::assertEquals($mediaId$media->getId());
        static::assertEquals('', $media->getUrl());
        // currently there shouldn't be loaded any thumbnails for private media, but if, the urls should be blank         foreach ($media->getThumbnails() ?? [] as $thumbnail) {
            static::assertEquals('', $thumbnail->getUrl());
        }
    }

    public function testPublicMediaIsReadable(): void
    {
        $mediaId = Uuid::randomHex();

        $this->mediaRepository->create(
            [
                [
                    
/** * @return array */
    public function convertMediaStruct(?Media $media = null)
    {
        if (!$media instanceof Media) {
            return [];
        }

        $thumbnails = [];

        foreach ($media->getThumbnails() as $thumbnail) {
            $thumbnails[] = [
                'source' => $thumbnail->getSource(),
                'retinaSource' => $thumbnail->getRetinaSource(),
                'sourceSet' => $this->getSourceSet($thumbnail),
                'maxWidth' => $thumbnail->getMaxWidth(),
                'maxHeight' => $thumbnail->getMaxHeight(),
                'attributes' => $thumbnail->getAttributes(),
            ];
        }

        $data = [
            
try {
            $renamedFiles = $this->renameFile(
                $this->urlGenerator->getRelativeMediaUrl($currentMedia),
                $this->urlGenerator->getRelativeMediaUrl($updatedMedia),
                $this->getFileSystem($currentMedia)
            );
        } catch (\Exception) {
            throw MediaException::couldNotRenameFile($currentMedia->getId()(string) $currentMedia->getFileName());
        }

        foreach ($currentMedia->getThumbnails() ?? [] as $thumbnail) {
            try {
                $renamedFiles = [...$renamedFiles, ...$this->renameThumbnail($thumbnail$currentMedia$updatedMedia)];
            } catch (\Exception) {
                $this->rollbackRenameAction($currentMedia$renamedFiles);
            }
        }

        $updateData = [
            'id' => $updatedMedia->getId(),
            'fileName' => $updatedMedia->getFileName(),
            'uploadedAt' => $updatedMedia->getUploadedAt(),
        ];
public function addUrls(EntityLoadedEvent $event): void
    {
        /** @var MediaEntity $media */
        foreach ($event->getEntities() as $media) {
            if (!$media->hasFile() || $media->isPrivate()) {
                continue;
            }

            $media->setUrl($this->urlGenerator->getAbsoluteMediaUrl($media));

            foreach ($media->getThumbnails() ?? [] as $thumbnail) {
                $this->addThumbnailUrl($thumbnail$media);
            }
        }
    }

    public function unserialize(EntityLoadedEvent $event): void
    {
        /** @var MediaEntity $media */
        foreach ($event->getEntities() as $media) {
            if ($media->getMediaTypeRaw()) {
                $media->setMediaType(unserialize($media->getMediaTypeRaw()));
            }
Home | Imprint | This part of the site doesn't use cookies.