• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

heimrichhannot / contao-utils-bundle / 6106487958

07 Sep 2023 07:01AM UTC coverage: 22.169% (-0.01%) from 22.181%
6106487958

push

github

web-flow
Merge pull request #68 from heimrichhannot/fix/php8-warning

Fixed: PHP8 warning and minor refactoring

22 of 22 new or added lines in 2 files covered. (100.0%)

1196 of 5395 relevant lines covered (22.17%)

1.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Twig/ImageExtension.php
1
<?php
2

3
/*
4
 * Copyright (c) 2023 Heimrich & Hannot GmbH
5
 *
6
 * @license LGPL-3.0-or-later
7
 */
8

9
namespace HeimrichHannot\UtilsBundle\Twig;
10

11
use Contao\Controller;
12
use Contao\FilesModel;
13
use Contao\FrontendTemplate;
14
use Contao\StringUtil;
15
use Contao\Validator;
16
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
17
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
18
use Twig\Extension\AbstractExtension;
19
use Twig\TwigFilter;
20

21
class ImageExtension extends AbstractExtension implements ContainerAwareInterface
22
{
23
    use ContainerAwareTrait;
24

25
    /**
26
     * Get list of twig filters.
27
     *
28
     * @return array|\Twig_SimpleFilter[]
29
     */
30
    public function getFilters()
31
    {
32
        return [
×
33
            new TwigFilter('image', [$this, 'getImage']),
×
34
            new TwigFilter('image_caption', [$this, 'getImageCaption']),
×
35
            new TwigFilter('image_width', [$this, 'getImageWidth']),
×
36
            new TwigFilter('image_data', [$this, 'getImageData']),
×
37
            new TwigFilter('image_gallery', [$this, 'getImageGallery']),
×
38
            new TwigFilter('image_size', [$this, 'getImageSize']),
×
39
        ];
×
40
    }
41

42
    /**
43
     * Get image based on given path/uuid.
44
     *
45
     * @param mixed        $image    File path/uuid
46
     * @param string|array $size     Array or serialized string containing [width, height, imageSize-ID]
47
     * @param array        $data     Add image data here [href => 'URL', class => 'img css class']…
48
     * @param string       $template Use custom image template
49
     *
50
     * @throws \Psr\Cache\InvalidArgumentException
51
     * @throws \Twig_Error_Loader
52
     * @throws \Twig_Error_Runtime
53
     * @throws \Twig_Error_Syntax
54
     *
55
     * @return string Image html element with given size
56
     */
57
    public function getImage($image, $size = null, array $data = [], string $template = 'image.html.twig'): string
58
    {
59
        $imageData = $this->getImageData($image, $size, $data);
×
60

61
        if (empty($imageData)) {
×
62
            return '';
×
63
        }
64

65
        return $this->container->get('huh.utils.template')->renderTwigTemplate($template, $imageData);
×
66
    }
67

68
    /**
69
     * Get image data based on given path/uuid.
70
     *
71
     * @param mixed        $image File path/uuid
72
     * @param string|array $size  Array or serialized string containing [width, height, imageSize-ID]
73
     * @param array        $data  Add image data here [href => 'URL', class => 'img css class']…
74
     *
75
     * @return array Image data
76
     */
77
    public function getImageData($image, $size = null, array $data = []): array
78
    {
79
        // $imageData = [];
80

81
        $data['singleSRC'] = $image;
×
82
        $size = $data['size'] = \is_array($size) ? $size : StringUtil::deserialize($size);
×
83

84
        // remove empty imageSize passed in
85
        if (empty($size) || (\is_array($size) && empty(array_filter($size)))) {
×
86
            unset($data['size']);
×
87
        }
88

89
        // $this->container->get('huh.utils.image')->addToTemplateData('image', 'addImage', $imageData, $data);
90

91
        $fileModel = null;
×
92

93
        if (Validator::isUuid($image))
×
94
        {
95
            if (!$fileModel = FilesModel::findByUuid($image))
×
96
                return [];
×
97
            $data['singleSRC'] = $fileModel->path;
×
98
        }
99

100
        $template = new FrontendTemplate();
×
101
        Controller::addImageToTemplate($template, $data, null, null, $fileModel);
×
102

103
        return $template->getData();
×
104

105
        /*
106
        if (empty($imageData)) {
107
            return [];
108
        }
109

110
        $result = array_merge($imageData, $data);
111

112
        if (isset($imageData['picture']) && isset($data['picture'])) {
113
            $result['picture'] = array_merge($imageData['picture'], $data['picture']);
114
        }
115

116
        return $result;
117
        */
118
    }
119

120
    /**
121
     * Get image caption based on given path/uuid.
122
     *
123
     * @param mixed $image File path/uuid
124
     *
125
     * @return string|null Image caption if available, else null
126
     */
127
    public function getImageCaption($image): ?string
128
    {
129
        if (null === ($file = $this->container->get('huh.utils.file')->getFileFromUuid($image))) {
×
130
            return null;
×
131
        }
132

133
        $meta = StringUtil::deserialize($file->getModel()->meta, true);
×
134

135
        if (!isset($meta[$GLOBALS['TL_LANGUAGE']]['caption'])) {
×
136
            return null;
×
137
        }
138

139
        return $meta[$GLOBALS['TL_LANGUAGE']]['caption'];
×
140
    }
141

142
    /**
143
     * Get image width based on given path/uuid.
144
     *
145
     * @param mixed $image File path/uuid
146
     *
147
     * @return string|null Image caption if available, else null
148
     */
149
    public function getImageWidth($image): ?string
150
    {
151
        if (null === ($file = $this->container->get('huh.utils.file')->getFileFromUuid($image))) {
×
152
            return null;
×
153
        }
154

155
        return $file->width;
×
156
    }
157

158
    public function getImageGallery($images, string $template = 'image_gallery.html.twig'): string
159
    {
160
        $galleryArray = \is_array($images) ? $images : StringUtil::deserialize($images, true);
×
161
        $galleryObjects = [];
×
162

163
        foreach ($galleryArray as $k => $v) {
×
164
            $galleryObjects['imageGallery'][$k] = $this->getImageData($v);
×
165
            $galleryObjects['imageGallery'][$k]['alt'] = $this->container->get('huh.utils.file')->getFileFromUuid($v)->name;
×
166
        }
167

168
        if (empty($galleryObjects)) {
×
169
            return [];
×
170
        }
171

172
        return $this->container->get('huh.utils.template')->renderTwigTemplate($template, $galleryObjects);
×
173
    }
174

175
    public function getImageSize($size)
176
    {
177
        $result = [];
×
178

179
        $size = \is_array($size) ? $size : StringUtil::deserialize($size, true);
×
180

181
        if (isset($size[2]) && $size[2] &&
×
182
            null !== ($imageSize = $this->container->get('huh.utils.model')->findModelInstanceByPk('tl_image_size', $size[2]))) {
×
183
            $result['width'] = $imageSize->width;
×
184
            $result['height'] = $imageSize->height;
×
185
        } else {
186
            $result['width'] = $size[0];
×
187
            $result['height'] = $size[1];
×
188
        }
189

190
        return $result;
×
191
    }
192
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc