-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponsiveimages.module
More file actions
88 lines (76 loc) · 2.71 KB
/
responsiveimages.module
File metadata and controls
88 lines (76 loc) · 2.71 KB
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
<?php
/**
* Implements hook_filter_info().
*
*/
function responsiveimages_filter_info() {
$filters['responsiveimages'] = array(
'title' => t('Responsive Images Filter'),
'description' => t('Turns normal images into "responsive" images'),
'process callback' => '_responsiveimages_filter_process',
'default settings' => array(
'image_path' => 'files/styles',
'sizes' => array('480', '768', '992', '1382'),
'noscript' => 'true',
),
'settings callback' => '_responsiveimages_filter_settings',
);
return $filters;
}
/**
* Settings callback
*
*/
function _responsiveimages_filter_settings($form, $form_state, $filter, $format, $defaults) {
global $base_url;
$settings['image_path'] = array(
'#type' => 'textfield',
'#title' => t('Path to your images directory'),
'#default_value' => isset($filter->settings['image_path']) ? $filter->settings['image_path'] : $defaults['image_path'],
'#description' => t('The path to your image folder. Relative to ' . $base_url . '/')
);
$settings['sizes'] = array(
'#type' => 'textfield',
'#title' => t('Image sizes to consider'),
'#default_value' => isset($filter->settings['sizes']) ? $filter->settings['sizes'] : $defaults['sizes'],
'#description' => t('Separate size values using a space, ie. "480 768"')
);
$settings['noscript'] = array(
'#type' => 'radios',
'#title' => t('Provide a <noscript> alternative'),
'#default_value' => isset($filter->settings['noscript']) ? $filter->settings['noscript'] : $defaults['noscript'],
'#options' => array(
'true' => 'Yes',
'false' => 'No'
),
'#description' => t('Provide a <noscript> tag with the default <img> tag in case there is no Javascript support.')
);
return $settings;
}
/**
* Implements hook_filter_process().
*
*/
function _responsiveimages_filter_process($html, $filter, $format) {
global $base_url;
$tokens = explode(' ', $filter->settings['sizes']);
$baseURL = $base_url . '/' . $filter->settings['image_path'];
preg_match_all('#<img(.+)(src="(' . $baseURL . '([a-z0-9\-]+)/[^"]+)")([^>]*)>#i', $html, $matches);
$len = count($matches[0]);
for ($i = 0; $i < $len; $i++) {
$src = array($i);
foreach ($tokens as $key) {
$url = str_replace($matches[4][$i], $key, $matches[3][$i]);
$src[] = 'data-src-' . $key . '="' . $url .'"';
}
reset($tokens);
if ($filter->settings['noscript'] == 'true') {
$replacement = '<noscript>' . $matches[0][$i] . '</noscript><img' .$matches[1][$i] . implode(' ', $src) . $matches[5][$i] . '>';
} else {
$replacement = '<img' .$matches[1][$i] . implode(' ', $src) . $matches[5][$i] . '>';
}
$html = str_replace($matches[0][$i], $replacement, $html);
}
return $html;
}
?>