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

heimrichhannot / contao-utils-bundle / 6196454422

15 Sep 2023 09:38AM UTC coverage: 22.144% (-0.008%) from 22.152%
6196454422

push

github

vvohh
fix displaying image caption

2 of 2 new or added lines in 1 file covered. (100.0%)

1196 of 5401 relevant lines covered (22.14%)

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
        } elseif (file_exists($image)) {
×
100
            $fileModel = FilesModel::findByPath($image);
×
101
        }
102

103
        $template = new FrontendTemplate();
×
104
        Controller::addImageToTemplate($template, $data, null, null, $fileModel);
×
105

106
        return $template->getData();
×
107

108
        /*
109
        if (empty($imageData)) {
110
            return [];
111
        }
112

113
        $result = array_merge($imageData, $data);
114

115
        if (isset($imageData['picture']) && isset($data['picture'])) {
116
            $result['picture'] = array_merge($imageData['picture'], $data['picture']);
117
        }
118

119
        return $result;
120
        */
121
    }
122

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

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

138
        if (!isset($meta[$GLOBALS['TL_LANGUAGE']]['caption'])) {
×
139
            return null;
×
140
        }
141

142
        return $meta[$GLOBALS['TL_LANGUAGE']]['caption'];
×
143
    }
144

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

158
        return $file->width;
×
159
    }
160

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

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

171
        if (empty($galleryObjects)) {
×
172
            return [];
×
173
        }
174

175
        return $this->container->get('huh.utils.template')->renderTwigTemplate($template, $galleryObjects);
×
176
    }
177

178
    public function getImageSize($size)
179
    {
180
        $result = [];
×
181

182
        $size = \is_array($size) ? $size : StringUtil::deserialize($size, true);
×
183

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

193
        return $result;
×
194
    }
195
}
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

© 2025 Coveralls, Inc