Skip to content

Commit 51cfa15

Browse files
committed
check if helper imaginatorGenUrls exists
1 parent 590b77d commit 51cfa15

File tree

1 file changed

+48
-37
lines changed

1 file changed

+48
-37
lines changed

src/helpers/ImaginatorGenUrls.php

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,62 @@
22

33
use Omatech\Imaginator\Repositories\Imaginator;
44

5-
function imaginatorGenUrls($id, $alt = '', $formats = [], $options = [], $sets = [])
6-
{
7-
$imaginator = new Imaginator();
8-
9-
$html = '';
10-
$urls = '';
11-
12-
foreach ($formats as $format) {
13-
foreach ($sets as $index => $set) {
14-
if (!empty($set['srcset'])) {
15-
if (gettype($set['srcset']) == 'array') {
16-
$srcset = $set['srcset'];
17-
} elseif (gettype($set['srcset']) === 'integer') {
18-
$srcset = [$set['srcset'] => $set['srcset']];
5+
if (! function_exists('imaginatorGenUrls')) {
6+
7+
/**
8+
* @param $id
9+
* @param string $alt
10+
* @param array $formats
11+
* @param array $options
12+
* @param array $sets
13+
* @return string
14+
*/
15+
function imaginatorGenUrls($id, $alt = '', $formats = [], $options = [], $sets = [])
16+
{
17+
$imaginator = new Imaginator();
18+
19+
$html = '';
20+
$urls = '';
21+
22+
foreach ($formats as $format) {
23+
foreach ($sets as $index => $set) {
24+
if (!empty($set['srcset'])) {
25+
if (gettype($set['srcset']) == 'array') {
26+
$srcset = $set['srcset'];
27+
} elseif (gettype($set['srcset']) === 'integer') {
28+
$srcset = [$set['srcset'] => $set['srcset']];
29+
} else {
30+
$srcset = [];
31+
}
1932
} else {
2033
$srcset = [];
2134
}
22-
} else {
23-
$srcset = [];
24-
}
2535

26-
$html .= "<source";
36+
$html .= "<source";
2737

28-
$urls = $imaginator->generateUrls([
29-
'hash' => $id,
30-
'srcset' => $srcset ?? [],
31-
'media' => $set['media'] ?? '',
32-
'sizes' => $set['sizes'] ?? '',
33-
'options' => $options ?? [],
34-
'format' => $format
35-
]);
38+
$urls = $imaginator->generateUrls([
39+
'hash' => $id,
40+
'srcset' => $srcset ?? [],
41+
'media' => $set['media'] ?? '',
42+
'sizes' => $set['sizes'] ?? '',
43+
'options' => $options ?? [],
44+
'format' => $format
45+
]);
3646

37-
if (!empty($set['media'])) {
38-
$html .= " media='{$set['media']}'";
39-
}
47+
if (!empty($set['media'])) {
48+
$html .= " media='{$set['media']}'";
49+
}
4050

41-
if (!empty($set['sizes'])) {
42-
$html .= " size='{$set['sizes']}'";
43-
}
51+
if (!empty($set['sizes'])) {
52+
$html .= " size='{$set['sizes']}'";
53+
}
4454

45-
$html .= " srcset='{$urls['srcset']}' type='image/{$urls['format']}'>\n";
55+
$html .= " srcset='{$urls['srcset']}' type='image/{$urls['format']}'>\n";
56+
}
4657
}
47-
}
4858

49-
$html .= "<img src='{$imaginator->generateUrls(['hash' => $id, 'options'=> $options])['base']}' alt='{$alt}'>";
50-
51-
return $html;
59+
$html .= "<img src='{$imaginator->generateUrls(['hash' => $id, 'options'=> $options])['base']}' alt='{$alt}'>";
60+
61+
return $html;
62+
}
5263
}

0 commit comments

Comments
 (0)