Skip to content

Commit

Permalink
Merge pull request #13 from fumikage/feat/libraryImage
Browse files Browse the repository at this point in the history
feat: add getImage function for Twig component
  • Loading branch information
Lucanis authored Jan 8, 2025
2 parents fc3c477 + 9241537 commit 70873cb
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Twig/LibraryImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace TheliaLibrary\Twig;

use TheliaBlocks\Service\JsonBlockService;
use TheliaLibrary\Model\Base\LibraryImageQuery;
use TheliaLibrary\Service\LibraryImageService;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class LibraryImage extends AbstractExtension
{
public function __construct(
private JsonBlockService $jsonBlockService,
private LibraryImageService $libraryImageService
) {
}

public function getFunctions(): array
{
return [
new TwigFunction('getImages', [$this, 'getImages']),

];
}

public function getImages(string $itemType ,int $itemId,int $width,int $height, string $format) {
$query = LibraryImageQuery::create();

$images = $query
->useLibraryItemImageQuery()
->filterByItemType($itemType)
->filterByItemId($itemId)
->endUse();



$results = [];

foreach($images as $image) {

$imageUrl = $this->libraryImageService->getImagePublicUrl(
$image,
$width,
$height,
$format
);

$results[] = [
'URL' => $imageUrl,
'ID' => $image->getId(),
];
}
return $results;


}



}

0 comments on commit 70873cb

Please sign in to comment.