-
Notifications
You must be signed in to change notification settings - Fork 1
/
canvas-image-resize.php
executable file
·331 lines (297 loc) · 8.37 KB
/
canvas-image-resize.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/*
Plugin Name: Canvas Image Resize
Plugin URI: https://de.wordpress.org/plugins/canvas-image-resize/
Description: Re-sizes images right inside the browser BEFORE uploading them.
Version: 1.0.1
Author: Simon Sippert
Author URI: http://www.sippsolutions.de/
Text Domain: canvas-image-resize
Domain Path: /lang
License: GPL3
License URI: https://www.gnu.org/licenses/gpl-3.0.html
*/
/*
Canvas Image Resize, a plugin for WordPress
Copyright (C) 2017 Simon Sippert, sippsolutions (http://www.sippsolutions.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Canvas Image Resize
*
* @copyright 2017 Simon Sippert <[email protected]>
*/
class CanvasImageResize
{
/**
* Define the plugin name
*
* @var string
*/
const PLUGIN_NAME = 'Canvas Image Resize';
/**
* Defines the text domain
*
* @var string
*/
const TEXT_DOMAIN = 'canvas-image-resize';
/**
* Defines the plugin's options page name
*
* @var string
*/
const OPTIONS_PAGE_NAME = 'cir_options';
/**
* Field name for max width
*
* @var string
*/
const FIELD_IMAGE_MAX_WIDTH = 'image_max_width';
/**
* Field name for max height
*
* @var string
*/
const FIELD_IMAGE_MAX_HEIGHT = 'image_max_height';
/**
* Field name for max quality
*
* @var string
*/
const FIELD_IMAGE_MAX_QUALITY = 'image_max_quality';
/**
* Store default options
*
* @var array
*/
protected $defaultOptions = array(
self::FIELD_IMAGE_MAX_WIDTH => 1600,
self::FIELD_IMAGE_MAX_HEIGHT => 1600,
self::FIELD_IMAGE_MAX_QUALITY => 100,
);
/**
* Initialize the plugin
*/
public function __construct()
{
$this->initPlugin();
$this->initPluginPage();
}
/**
* Initialize the filter settings
*
* @return void
*/
protected function initPlugin()
{
// define function
$setImageSettingsFunction = 'setImageSettings';
// add filters
add_filter('plupload_default_settings', array($this, $setImageSettingsFunction), 100);
add_filter('plupload_default_params', array($this, $setImageSettingsFunction), 100);
add_filter('plupload_init', array($this, $setImageSettingsFunction), 100);
}
/**
* Initialize the plugin page
*
* @return void
*/
protected function initPluginPage()
{
add_action('plugins_loaded', array($this, 'addTextDomain'));
add_action('admin_init', array($this, 'initOptionsPage'));
add_action('admin_menu', array($this, 'addOptionsPage'));
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'addPluginPage'));
}
/**
* Add text domain
*
* @return void
*/
public function addTextDomain()
{
load_plugin_textdomain(static::TEXT_DOMAIN, false, dirname(plugin_basename(__FILE__)) . '/lang');
}
/**
* Get plugin name
*
* @return string
*/
protected function getPluginName()
{
return __(static::PLUGIN_NAME, static::TEXT_DOMAIN);
}
/**
* Get options name
*
* @return string
*/
protected function getOptionsName()
{
return static::TEXT_DOMAIN . '_settings';
}
/**
* Get options
*
* @return array
*/
protected function getOptions()
{
return wp_parse_args(get_option($this->getOptionsName()), $this->defaultOptions);
}
/**
* Add the plugin page
*
* @param array $links
* @return array
*/
public function addPluginPage(array $links)
{
array_unshift($links, '<a href="options-general.php?page=' . static::TEXT_DOMAIN . '">' . __('Settings', static::TEXT_DOMAIN) . '</a>');
return $links;
}
/**
* Add the options page
*
* @return void
*/
public function addOptionsPage()
{
add_options_page($this->getPluginName(), $this->getPluginName(), 'manage_options', static::TEXT_DOMAIN, array($this, 'renderOptionsPage'));
}
/**
* Render the options page
*
* @return void
*/
public function initOptionsPage()
{
// add the possibility to add settings
register_setting(static::OPTIONS_PAGE_NAME, $this->getOptionsName());
// set section name
$sectionName = implode('_', array(static::TEXT_DOMAIN, static::OPTIONS_PAGE_NAME, 'general'));
// add section
add_settings_section(
$sectionName,
__('General settings', static::TEXT_DOMAIN),
null,
static::OPTIONS_PAGE_NAME
);
// add fields
add_settings_field(
static::FIELD_IMAGE_MAX_WIDTH,
__('Maximum width of images', static::TEXT_DOMAIN),
array($this, 'renderFieldGeneralImageMaxWidth'),
static::OPTIONS_PAGE_NAME,
$sectionName
);
add_settings_field(
static::FIELD_IMAGE_MAX_HEIGHT,
__('Maximum height of images', static::TEXT_DOMAIN),
array($this, 'renderFieldGeneralImageMaxHeight'),
static::OPTIONS_PAGE_NAME,
$sectionName
);
add_settings_field(
static::FIELD_IMAGE_MAX_QUALITY,
__('Quality of images (0-100)', static::TEXT_DOMAIN),
array($this, 'renderFieldGeneralImageMaxQuality'),
static::OPTIONS_PAGE_NAME,
$sectionName
);
}
/**
* Render the options page
*
* @return void
*/
public function renderOptionsPage()
{
?>
<form action='options.php' method='post'>
<h1><?php echo $this->getPluginName(); ?></h1>
<p><?php echo __('Below you can configure which maximum dimensions images uploaded to your site should have.', static::TEXT_DOMAIN); ?></p>
<?php
settings_fields(static::OPTIONS_PAGE_NAME);
do_settings_sections(static::OPTIONS_PAGE_NAME);
submit_button();
?>
</form>
<?php
}
/**
* Render a field
*
* @param string $name
* @param string [$type]
*
* @return void
*/
protected function renderField($name, $type = 'number')
{
$options = $this->getOptions();
?>
<input type='<?php echo $type; ?>'
title='<?php echo $name; ?>'
name='<?php echo $this->getOptionsName(); ?>[<?php echo $name; ?>]'
value='<?php echo $type == 'number' ? abs((int)$options[$name]) : $options[$name]; ?>'>
<?php
}
/**
* Render a specific field
*
* @return void
*/
public function renderFieldGeneralImageMaxWidth()
{
$this->renderField(static::FIELD_IMAGE_MAX_WIDTH);
}
/**
* Render a specific field
*
* @return void
*/
public function renderFieldGeneralImageMaxHeight()
{
$this->renderField(static::FIELD_IMAGE_MAX_HEIGHT);
}
/**
* Render a specific field
*
* @return void
*/
public function renderFieldGeneralImageMaxQuality()
{
$this->renderField(static::FIELD_IMAGE_MAX_QUALITY);
}
/**
* Set image re-sizing settings
* [Does all the magic :3]
*
* @param array $defaults
* @return array
*/
public function setImageSettings(array $defaults)
{
// get options
$options = $this->getOptions();
// set values
$defaults['resize'] = array(
'width' => abs((int)$options[static::FIELD_IMAGE_MAX_WIDTH]),
'height' => abs((int)$options[static::FIELD_IMAGE_MAX_HEIGHT]),
'quality' => abs((int)$options[static::FIELD_IMAGE_MAX_QUALITY]),
);
return $defaults;
}
}
// init
new CanvasImageResize();