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

heimrichhannot / contao-utils-bundle / 6809777185

09 Nov 2023 09:22AM UTC coverage: 22.835%. Remained the same
6809777185

push

github

koertho
deprecate twig filters and tests

2 of 32 new or added lines in 8 files covered. (6.25%)

4 existing lines in 4 files now uncovered.

1255 of 5496 relevant lines covered (22.83%)

1.55 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 [
×
NEW
33
            new TwigFilter('image', [$this, 'getImage'], ['deprecated' => true]),
×
NEW
34
            new TwigFilter('image_caption', [$this, 'getImageCaption'], ['deprecated' => true]),
×
NEW
35
            new TwigFilter('image_width', [$this, 'getImageWidth'], ['deprecated' => true]),
×
NEW
36
            new TwigFilter('image_data', [$this, 'getImageData'], ['deprecated' => true]),
×
NEW
37
            new TwigFilter('image_gallery', [$this, 'getImageGallery'], ['deprecated' => true]),
×
NEW
38
            new TwigFilter('image_size', [$this, 'getImageSize'], ['deprecated' => true]),
×
UNCOV
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

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

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

88
        $fileModel = null;
×
89

90
        if (Validator::isUuid($image))
×
91
        {
92
            if (!$fileModel = FilesModel::findByUuid($image))
×
93
                return [];
×
94
            $data['singleSRC'] = $fileModel->path;
×
95

96
        } elseif (file_exists($image)) {
×
97
            $fileModel = FilesModel::findByPath($image);
×
98
        }
99

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

103
        if (isset($fileModel->copyright) && isset($template->picture)) {
×
104
            $picture = $template->picture;
×
105
            $picture['copyright'] = $fileModel->copyright;
×
106
            $template->picture = $picture;
×
107
        }
108

109
        $templateData = $template->getData();
×
110
        // merge the rest of the custom configs
111
        $templateData = array_merge($templateData, $data);
×
112

113
        return $templateData;
×
114
    }
115

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

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

131
        if (!isset($meta[$GLOBALS['TL_LANGUAGE']]['caption'])) {
×
132
            return null;
×
133
        }
134

135
        return $meta[$GLOBALS['TL_LANGUAGE']]['caption'];
×
136
    }
137

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

151
        return $file->width;
×
152
    }
153

154
    public function getImageGallery($images, string $template = 'image_gallery.html.twig'): string
155
    {
156
        $galleryArray = \is_array($images) ? $images : StringUtil::deserialize($images, true);
×
157
        $galleryObjects = [];
×
158

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

164
        if (empty($galleryObjects)) {
×
165
            return [];
×
166
        }
167

168
        return $this->container->get('huh.utils.template')->renderTwigTemplate($template, $galleryObjects);
×
169
    }
170

171
    public function getImageSize($size)
172
    {
173
        $result = [];
×
174

175
        $size = \is_array($size) ? $size : StringUtil::deserialize($size, true);
×
176

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

186
        return $result;
×
187
    }
188
}
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