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

dg / texy / 22262381750

25 Jan 2026 11:44PM UTC coverage: 92.367% (-0.7%) from 93.057%
22262381750

push

github

dg
cs

9 of 11 new or added lines in 8 files covered. (81.82%)

161 existing lines in 23 files now uncovered.

2384 of 2581 relevant lines covered (92.37%)

0.92 hits per line

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

82.61
/src/Texy/Modules/ImageModule.php
1
<?php
2

3
/**
4
 * This file is part of the Texy! (https://texy.nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Texy\Modules;
11

12
use Texy;
13
use Texy\Helpers;
14
use Texy\Image;
15
use Texy\Patterns;
16
use function explode, getimagesize, is_file, is_int, min, round, rtrim, str_contains, trim;
17

18

19
/**
20
 * Images module.
21
 */
22
final class ImageModule extends Texy\Module
23
{
24
        /** root of relative images (http) */
25
        public ?string $root = 'images/';
26

27
        /** root of linked images (http) */
28
        public ?string $linkedRoot = 'images/';
29

30
        /** physical location of images on server */
31
        public ?string $fileRoot = null;
32

33
        /** left-floated images CSS class */
34
        public ?string $leftClass = null;
35

36
        /** right-floated images CSS class */
37
        public ?string $rightClass = null;
38

39
        /** default alternative text */
40
        public ?string $defaultAlt = '';
41

42
        /** @var array<string, Image> image references */
43
        private array $references = [];
44

45

46
        public function __construct(Texy\Texy $texy)
1✔
47
        {
48
                $this->texy = $texy;
1✔
49

50
                $texy->allowed['image/definition'] = true;
1✔
51
                $texy->addHandler('image', $this->solve(...));
1✔
52
                $texy->addHandler('beforeParse', $this->beforeParse(...));
1✔
53

54
                // [*image*]:LINK
55
                $texy->registerLinePattern(
1✔
56
                        $this->patternImage(...),
1✔
57
                        '#\[\*\ *+([^\n' . Patterns::MARK . ']{1,1000})' . Patterns::MODIFIER . '?\ *+(\*|(?<!<)>|<)\]' // [* urls .(title)[class]{style} >]
58
                        . '(?::(' . Patterns::LINK_URL . '|:))??()#Uu',
1✔
59
                        'image',
1✔
60
                );
61
        }
1✔
62

63

64
        /**
65
         * Text pre-processing.
66
         */
67
        private function beforeParse(Texy\Texy $texy, &$text): void
1✔
68
        {
69
                if (!empty($texy->allowed['image/definition'])) {
1✔
70
                        // [*image*]: urls .(title)[class]{style}
71
                        $text = Texy\Regexp::replace(
1✔
72
                                $text,
1✔
73
                                '#^\[\*([^\n]{1,100})\*\]:[\ \t]+(.{1,1000})[\ \t]*' . Patterns::MODIFIER . '?\s*()$#mUu',
1✔
74
                                $this->patternReferenceDef(...),
1✔
75
                        );
76
                }
77
        }
1✔
78

79

80
        /**
81
         * Callback for: [*image*]: urls .(title)[class]{style}.
82
         */
83
        private function patternReferenceDef(array $matches): string
1✔
84
        {
85
                [, $mRef, $mURLs, $mMod] = $matches;
1✔
86
                // [1] => [* (reference) *]
87
                // [2] => urls
88
                // [3] => .(title)[class]{style}<>
89

90
                $image = $this->factoryImage($mURLs, $mMod, tryRef: false);
1✔
91
                $this->addReference($mRef, $image);
1✔
92
                return '';
1✔
93
        }
94

95

96
        /**
97
         * Callback for [* small.jpg 80x13 || big.jpg .(alternative text)[class]{style}>]:LINK.
98
         */
99
        public function patternImage(Texy\LineParser $parser, array $matches): Texy\HtmlElement|string|null
1✔
100
        {
101
                [, $mURLs, $mMod, $mAlign, $mLink] = $matches;
1✔
102
                // [1] => URLs
103
                // [2] => .(title)[class]{style}<>
104
                // [3] => * < >
105
                // [4] => url | [ref] | [*image*]
106

107
                $image = $this->factoryImage($mURLs, $mMod . $mAlign);
1✔
108

109
                if ($mLink) {
1✔
110
                        if ($mLink === ':') {
1✔
111
                                $link = new Texy\Link($image->linkedURL ?? $image->URL);
1✔
112
                                $link->raw = ':';
1✔
113
                                $link->type = $link::IMAGE;
1✔
114
                        } else {
115
                                $link = $this->texy->linkModule->factoryLink($mLink, null, null);
1✔
116
                        }
117
                } else {
118
                        $link = null;
1✔
119
                }
120

121
                return $this->texy->invokeAroundHandlers('image', $parser, [$image, $link]);
1✔
122
        }
123

124

125
        /**
126
         * Adds new named reference to image.
127
         */
128
        public function addReference(string $name, Image $image): void
1✔
129
        {
130
                $image->name = Helpers::toLower($name);
1✔
131
                $this->references[$image->name] = $image;
1✔
132
        }
1✔
133

134

135
        /**
136
         * Returns named reference.
137
         */
138
        public function getReference(string $name): ?Image
1✔
139
        {
140
                $name = Helpers::toLower($name);
1✔
141
                if (isset($this->references[$name])) {
1✔
142
                        return clone $this->references[$name];
1✔
143
                }
144

145
                return null;
1✔
146
        }
147

148

149
        /**
150
         * Parses image's syntax. Input: small.jpg 80x13 || linked.jpg
151
         */
152
        public function factoryImage(string $content, string $mod, bool $tryRef = true): Image
1✔
153
        {
154
                $image = $tryRef ? $this->getReference(trim($content)) : null;
1✔
155

156
                if (!$image) {
1✔
157
                        $texy = $this->texy;
1✔
158
                        $content = explode('|', $content);
1✔
159
                        $image = new Image;
1✔
160

161
                        // dimensions
162
                        $matches = null;
1✔
163
                        if ($matches = Texy\Regexp::match($content[0], '#^(.*) (\d+|\?) *(X|x) *(\d+|\?) *()$#U')) {
1✔
164
                                $image->URL = trim($matches[1]);
1✔
165
                                $image->asMax = $matches[3] === 'X';
1✔
166
                                $image->width = $matches[2] === '?' ? null : (int) $matches[2];
1✔
167
                                $image->height = $matches[4] === '?' ? null : (int) $matches[4];
1✔
168
                        } else {
169
                                $image->URL = trim($content[0]);
1✔
170
                        }
171

172
                        if (!$texy->checkURL($image->URL, $texy::FILTER_IMAGE)) {
1✔
UNCOV
173
                                $image->URL = null;
×
174
                        }
175

176
                        // linked image
177
                        if (isset($content[2])) {
1✔
UNCOV
178
                                $tmp = trim($content[2]);
×
UNCOV
179
                                if ($tmp !== '' && $texy->checkURL($tmp, $texy::FILTER_ANCHOR)) {
×
UNCOV
180
                                        $image->linkedURL = $tmp;
×
181
                                }
182
                        }
183
                }
184

185
                $image->modifier->setProperties($mod);
1✔
186
                return $image;
1✔
187
        }
188

189

190
        /**
191
         * Finish invocation.
192
         */
193
        public function solve(
1✔
194
                ?Texy\HandlerInvocation $invocation,
195
                Image $image,
196
                ?Texy\Link $link = null,
197
        ): ?Texy\HtmlElement
198
        {
199
                if ($image->URL == null) {
1✔
UNCOV
200
                        return null;
×
201
                }
202

203
                $texy = $this->texy;
1✔
204

205
                $mod = $image->modifier;
1✔
206
                $alt = $mod->title;
1✔
207
                $mod->title = null;
1✔
208
                $hAlign = $mod->hAlign;
1✔
209
                $mod->hAlign = null;
1✔
210

211
                $el = new Texy\HtmlElement('img');
1✔
212
                $el->attrs['src'] = null; // trick - move to front
1✔
213
                $mod->decorate($texy, $el);
1✔
214
                $el->attrs['src'] = Helpers::prependRoot($image->URL, $this->root);
1✔
215
                if (!isset($el->attrs['alt'])) {
1✔
216
                        $el->attrs['alt'] = $alt === null
1✔
217
                                ? $this->defaultAlt
1✔
218
                                : $texy->typographyModule->postLine($alt);
1✔
219
                }
220

221
                if ($hAlign) {
1✔
222
                        $var = $hAlign . 'Class'; // leftClass, rightClass
1✔
223
                        if (!empty($this->$var)) {
1✔
224
                                $el->attrs['class'][] = $this->$var;
1✔
225

226
                        } elseif (empty($texy->alignClasses[$hAlign])) {
1✔
227
                                $el->attrs['style']['float'] = $hAlign;
1✔
228

229
                        } else {
UNCOV
230
                                $el->attrs['class'][] = $texy->alignClasses[$hAlign];
×
231
                        }
232
                }
233

234
                if (!is_int($image->width) || !is_int($image->height) || $image->asMax) {
1✔
235
                        $this->detectDimensions($image);
1✔
236
                }
237

238
                $el->attrs['width'] = $image->width;
1✔
239
                $el->attrs['height'] = $image->height;
1✔
240

241
                $texy->summary['images'][] = $el->attrs['src'];
1✔
242

243
                if ($link) {
1✔
244
                        return $texy->linkModule->solve(null, $link, $el);
1✔
245
                }
246

247
                return $el;
1✔
248
        }
249

250

251
        private function detectDimensions(Image $image): void
1✔
252
        {
253
                // absolute URL & security check for double dot
254
                if (!Helpers::isRelative($image->URL) || str_contains($image->URL, '..')) {
1✔
255
                        return;
1✔
256
                }
257

258
                $file = rtrim((string) $this->fileRoot, '/\\') . '/' . $image->URL;
1✔
259
                if (!@is_file($file) || !($size = @getimagesize($file))) { // intentionally @
1✔
260
                        return;
1✔
261
                }
262

UNCOV
263
                if ($image->asMax) {
×
UNCOV
264
                        $ratio = 1;
×
UNCOV
265
                        if (is_int($image->width)) {
×
UNCOV
266
                                $ratio = min($ratio, $image->width / $size[0]);
×
267
                        }
268

269
                        if (is_int($image->height)) {
×
270
                                $ratio = min($ratio, $image->height / $size[1]);
×
271
                        }
272

UNCOV
273
                        $image->width = (int) round($ratio * $size[0]);
×
274
                        $image->height = (int) round($ratio * $size[1]);
×
275

UNCOV
276
                } elseif (is_int($image->width)) {
×
UNCOV
277
                        $image->height = (int) round($size[1] / $size[0] * $image->width);
×
278

279
                } elseif (is_int($image->height)) {
×
UNCOV
280
                        $image->width = (int) round($size[0] / $size[1] * $image->height);
×
281

282
                } else {
UNCOV
283
                        $image->width = $size[0];
×
UNCOV
284
                        $image->height = $size[1];
×
285
                }
286
        }
287
}
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