forked from ctmaloney/imgsizer
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathext.imgsizer.php
87 lines (73 loc) · 3.08 KB
/
ext.imgsizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
class Imgsizer_ext {
var $name = 'Imgsizer';
var $version = '4.1.0';
var $settings_exists = 'n';
function channel_entries_tagdata_end($tagdata, $row) {
if (strpos($tagdata, 'imgsizer-figure') !== false) {
// parse tag so we have full image paths to work with
$tagdata = ee()->TMPL->parse_variables($tagdata, []);
// get the tags out of the tagdata
preg_match_all("/<figure(.*)imgsizer-figure(.*)figure>/", $tagdata, $tags);
foreach ($tags[0] as $tag) {
$figure_tag = $tag;
$out_figure = str_replace('imgsizer-figure', '', $figure_tag);
preg_match("/<img(.*)>/U", $figure_tag, $result);
$img_tag = $result[0];
// get any alt text from image tag
$alt_text = preg_match("/alt=\"(.*)\"/U", $img_tag, $result) ? $result[1] : '';
// get the src and width, remove that data from the html output
preg_match("/data-src=\"(.*)\"/U", $figure_tag, $result);
$src = $result[1];
$out_figure = str_replace($result[0], '', $out_figure);
preg_match("/data-width=\"(.*)\"/U", $figure_tag, $result);
$width = isset($result[1]) ? $result[1] : '';
$out_figure = str_replace(isset($result[0]) ? $result[0] : '', '', $out_figure);
if ($width == '') {
// no resizing, show regular image
$out_img = '<img src="' . $src . '" alt="' . $alt_text . '" />';
} else {
// replace the new tags into the tagdata
$out_img = '{exp:imgsizer:size src="' . $src . '" width="' . $width . '" quality="75"}
<img src="{sized}" width="{width}" height="{height}" alt="' . $alt_text . '" />
{/exp:imgsizer:size}';
}
$out_figure = str_replace($img_tag, $out_img, $out_figure);
$tagdata = str_replace($figure_tag, $out_figure, $tagdata);
}
}
return $tagdata;
}
function rte_before_display($field, $data)
{
if (strpos($data, 'imgsizer-figure') !== false) {
// get the tags out of the data
preg_match_all("/<figure(.*)imgsizer-figure(.*)figure>/", $data, $tags);
foreach ($tags[0] as $tag) {
$figure_tag = $tag;
$out_figure = str_replace('imgsizer-figure', '', $figure_tag);
preg_match("/<img(.*)>/U", $figure_tag, $result);
$img_tag = $result[0];
// get any alt text from image tag
$alt_text = preg_match("/alt=\"(.*)\"/U", $img_tag, $result) ? $result[1] : '';
// get the src and width, remove that data from the html output
preg_match("/data-src=\"(.*)\"/U", $figure_tag, $result);
$src = $result[1];
$out_figure = str_replace($result[0], '', $out_figure);
preg_match("/data-width=\"(.*)\"/U", $figure_tag, $result);
$width = isset($result[1]) ? $result[1] : '';
$out_figure = str_replace(isset($result[0]) ? $result[0] : '', '', $out_figure);
if ($width == '') {
// no resizing, show regular image
$out_img = '<img src="' . $src . '" alt="' . $alt_text . '" />';
} else {
// replace the new tags into the data
$out_img = '<img src="' . $src . '" width="' . $width . '" alt="' . $alt_text . '" />';
}
$out_figure = str_replace($img_tag, $out_img, $out_figure);
$data = str_replace($figure_tag, $out_figure, $data);
}
}
return $data;
}
}