-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.php
143 lines (121 loc) · 4.37 KB
/
index.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
<?php
require_once("ImageResize.php");
Plugin::setInfos(array(
'id' => 'image_resize',
'title' => 'Image Resize',
'description' => 'Allows for dynamic resizing of images.',
'version' => '1.3.1',
'license' => 'GPL 3.0',
'author' => 'Peter Gassner',
'website' => 'http://www.naehrstoff.ch/code/image-resize-for-frog',
'update_url' => 'http://frog.naehrstoff.ch/plugin-versions.xml',
'require_frog_version' => '0.9.4'
));
// Listen for page_not_found messages
Observer::observe('page_not_found', 'image_resize_try_resizing');
// Add the controller without showing a tab (fourth option)
Plugin::addController('image_resize', 'Image Resize', '', FALSE);
/**
* Execute this function on page_not_found.
* If the request is for an image file,
* resize the image.
*/
function image_resize_try_resizing() {
// Require that visitor be logged in and has
// permission to create files
if (!AuthUser::isLoggedIn()) {
AuthUser::load();
}
if (!AuthUser::hasPermission('administrator,developer,editor')) {
return false;
}
// Check that gd library is available
if (!ImageResize::gd_available()) {
return false;
}
if (preg_match('#\.(jpe?g|gif|png|wbmp)$#i', CURRENT_URI)) {
// If requested file appears to be an accepted format, create the new image
if (image_resize_scale(CURRENT_URI) && !DEBUG) {
// If Frog isn't debugging, it writes to a file; redirect to it
header('Location: '. URL_PUBLIC . "/" . CURRENT_URI);
// Exit here to prevent a page not found message
exit();
}
}
}
/**
* Parse the filename of the requested image
* for size information and resize accordingly.
*/
function image_resize_scale($path) {
$params = explode("/", $path);
$namepart = array_pop($params);
$public_path = URL_PUBLIC . "/" . join("/", $params);
$server_path = FROG_ROOT . "/" . join("/", $params);
// Dissect filename to find dimension information
$pattern = <<<FILENAME_PATTERN
/^
# Acceptable input examples (non-exhaustive):
# Width scaling: file.200.jpg file.200x.jpg
# Height scaling: file.x200.jpg
# Arbitrary scaling: file.200x200.jpg
# Cropping: file.200c.jpg file.200x200c.jpg
(.+) # source file name-part
\.(?!x?c?\.) # prevents "image..jpg" and so forth
(\d+)? # width
x?(\d+)? # height
(c)? # optional crop
(\.[a-z]+) # source file extension
\$/ix
FILENAME_PATTERN;
if (preg_match($pattern, $namepart, $match)) {
$filename = $match[1].$match[5];
$width = (int) $match[2];
$height = (int) $match[3];
$crop = 'c' == $match[4];
$source = $server_path."/".$filename;
$destination = $server_path."/".$namepart;
} else {
return false;
}
if ($crop) {
return ImageResize::image_scale_cropped($source, $destination, $width, $height);
} else {
return ImageResize::image_scale($source, $destination, $width, $height);
}
}
/**
* Helper function to create an image tag
*
* @param string image path
* @param array image options: "width", "height" or "crop"
* @param array html attributes that will be added to the link tag
*/
function image_resize_image_tag($image_path, $options = array(), $html_attributes = array()) {
// Maintain backwards compatibility
$args = func_get_args();
if (is_null($args[1])) {
$options = array();
}
if (is_numeric($args[1])) {
$options = array();
$options['width'] = $args[1];
}
if (is_numeric($args[2])) {
$options['height'] = $args[2];
}
if (is_array($args[3])) {
$html_attributes = $args[3];
}
// Assign values
$width = (array_key_exists('width', $options)) ? $options['width'] : NULL;
$height = (array_key_exists('height', $options)) ? $options['height'] : NULL;
$crop = (array_key_exists('crop', $options)) ? 'c' : "";
if (!array_key_exists("alt", $html_attributes)) $html_attributes['alt'] = '';
$attributes_string = " ";
foreach ($html_attributes as $key => $value) {
$attributes_string .= "$key='$value' ";
}
$image_path = preg_replace('#(.+)\.(jpg|jpeg|gif|png)$#i', '${1}.'.$width.'x'.$height.$crop.'.${2}', $image_path);
return "<img src='$image_path'$attributes_string/>";
}