-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathampimage.php
176 lines (137 loc) · 5.27 KB
/
ampimage.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace Grav\Plugin;
use Grav\Common\Page\Media;
use Grav\Common\Page\Medium\ImageMedium;
use Grav\Common\Page\Page;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;
/**
* Class AmpimagePlugin
* @package Grav\Plugin
*/
class AmpimagePlugin extends Plugin
{
/**
* Register events with Grav
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPageContentProcessed' => ['onPageContentProcessed', 0]
];
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized()
{
// Don't proceed if we are in the admin plugin
if ($this->isAdmin()) {
return;
}
}
/**
* Do some work for this event, full details of events can be found
* on the learn site: http://learn.getgrav.org/plugins/event-hooks
*
* @param Event $event
*/
public function onPageContentProcessed(Event $event)
{
$pluginsobject = (array) $this->config->get('plugins');
$test = 1;
if (!empty($pluginsobject['ampimage']['enabled'])) {
/**
* $var array $ampimageSettings
*/
$ampimageSettings = $pluginsobject['ampimage'];
$page = $event['page'];
$buffer = $page->content();
/**
* @var Media $media
*/
$media = $page->media();
$mediaArr = $media->images();
/* Unwrap <img> from <p> */
$unwrap = "/<p>\s*?(<a .*<img.*<\/a>|<img.*)?\s*<\/p>/";
$buffer = preg_replace($unwrap, "$1", $buffer);
$images = [];
$pattern = '/<img[^>]+>/i';
preg_match_all($pattern, $buffer, $images);
if (!empty($images[0])) {
foreach ($images[0] as $image) {
$imgAttr = [];
$doc = new \DOMDocument();
$doc->loadHTML(mb_convert_encoding($image, 'HTML-ENTITIES', 'UTF-8'));
$anchors = $doc->getElementsByTagName('img');
/**
* @var \DOMElement[] $anchors
*/
foreach($anchors as $node) {
if ($node->hasAttributes()) {
foreach($node->attributes as $a) {
$imgAttr[$a->name] = $a->value;
}
}
}
if (empty($imgAttr['src'])) {
continue; // no src, no image
}
/**
* For most layout modes amp requires width an height of image
*/
$imgfilename = basename($imgAttr['src']);
if (array_key_exists($imgfilename, $mediaArr)) {
/**
* @var ImageMedium
*/
$thisImgObj = $mediaArr[$imgfilename];
$thisImgArray = $thisImgObj->toArray();
} else {
// Just something to fall back on
$thisImgArray['width'] = 50;
$thisImgArray['height'] = 50;
}
// Alt text
$alttext = (!empty($imgAttr['alt']) ? $imgAttr['alt']:'');
// Class
$class = (!empty($imgAttr['class']) ? $imgAttr['class']:'');
$output = '<amp-img ';
$output .= 'src="'.$imgAttr['src'].'" ';
$output .= 'layout="'.$ampimageSettings['defaultLayout'].'" ';
$output .= 'alt="'.$alttext.'" ';
$output .= ' width="'.$thisImgArray['width'].'"' ;
$output .= ' height="'.$thisImgArray['height'].'"' ;
if (!empty($imgAttr['srcset'])) {
$output .= 'srcset="'.$imgAttr['srcset'].'" ';
}
if (!empty($imgAttr['sizes'])) {
$output .= 'sizes="'.$imgAttr['sizes'].'" ';
}
if (!empty($class) &&
(empty($ampimageSettings['moveClassToFigure']) || empty($ampimageSettings['addFigure']))
) {
$output .= 'class="'.$class.'" ';
}
$output .= '></amp-img>';
if (!empty($ampimageSettings['addFigure'])) {
$figure = '<figure ';
if (!empty($class) && !empty($ampimageSettings['moveClassToFigure'])) {
$figure .= 'class="'.$class.'"';
}
$figure .= '>'.$output;
if (!empty($alttext)) {
$figure .= '<figcaption>'.$alttext.'</figcaption>';
}
$figure .= '</figure>';
$output = $figure;
}
// Replace image
$buffer = str_replace($image, $output, $buffer);
}
$page->setRawContent($buffer);
}
}
}
}