-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvrannemstein.php
More file actions
266 lines (241 loc) · 11.7 KB
/
Copy pathvrannemstein.php
File metadata and controls
266 lines (241 loc) · 11.7 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
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
<?php
/**
* @package vrannemstein
* @author Leonardo Laureti
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: Vrannemstein
* Plugin URI: https://github.com/ctlcltd/vrannemstein
* Description: Image thumbnails using wasm-vips via the client side.
* Update URI: false
* Requires at least: 6.8
* Requires PHP: 8.3
* Version: 0.1.8
* Author: Leonardo Laureti
* Author URI: https://github.com/ctlcltd
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
*/
defined( 'ABSPATH' ) || die();
class Vrannemstein {
const string VERSION = '0.1.8';
const string WASM_VIPS_VERSION = '0.0.18'; // reflects package.json version
public int $queue_priority = 9999; // higher scripts enqueue priority
public int $subsizes_filter_priority = 9999; // higher intermediate image sizes filter priority
public function __construct() {
add_action( 'rest_api_init', array( $this, 'rest_api' ) );
// conditionally loads vrannemstein, wasm-vips scripts
add_action( 'load-post.php', array( $this, 'backend' ) );
add_action( 'load-post-new.php', array( $this, 'backend' ) );
add_action( 'load-upload.php', array( $this, 'backend' ) );
add_action( 'load-media-new.php', array( $this, 'backend' ) );
/**
* Filter to allow bulk resizer thumbnails
*
* @param bool $allow default true
*/
if ( apply_filters( 'vrannemstein_bulk_resizer', '__return_true' ) ) {
add_action( 'load-upload.php', array( $this, 'bulk_resizer' ) );
}
/**
* 'intermediate_image_sizes_advanced'
*
* This filter turns off image subsize generation.
*
* @see wp_create_image_subsizes()
*
* @param array $new_sizes
* @param array $image_meta
* @param int $attachment_id
* @return array
*/
add_filter( 'intermediate_image_sizes_advanced', '__return_empty_array', $this->subsizes_filter_priority );
add_filter( 'plupload_default_settings', array( $this, 'plupload_image_editor_supports_compat' ) );
}
static function init() {
static $init = new Vrannemstein();
}
/**
* Thumbnailer configuration
*
* @todo test gifload|webpload n=-1 (1 page, -1 all)
* @todo test background type ArrayConstant SingleOrArray<number>
*
* @return array Script options
*/
public function thumbnailer_config() {
/**
* Filters thumbnailer configuration
*
* Useful to extend thumbnailer options from theme or plugin.
*
* @see wp_get_registered_image_subsizes()
*
* @param array $config Javascript options
*/
return apply_filters( 'vrannemstein_config', array(
'verbosity' => 7, // flags (0 none, 1 log, 2 info, 4 error, 7 all)
'debug' => false, // debug wasm-vips
'image_sizes' => wp_get_registered_image_subsizes(),
'noImageDecoding' => true, // disallow browser image decoding on wasm-vips load, default true
'dynamicLibraries' => array('vips-heif.wasm'), // dynamic wasm-vips libraries load [vips-jxl.wasm, vips-heif.wasm, vips-resvg.wasm]
'checkMimeType' => true, // check for input file MIME type, default true
'density' => 72, // metadata resolution in dpi, default 72 (false = use source resolution from exif data)
'readXmp' => false, // allow vrannemstein_hooks.readXmp javascript hook, default false
'readExif' => false, // allow vrannemstein_hooks.readExif javascript hook, default false
'readIptc' => false, // allow vrannemstein_hooks.readIptc javascript hook, default false
'reduce' => array(
'centre' => true, // sampling offset, default true, (deprecated parameter)
'kernel' => 5 // resample kernel, default 5, VipsKernel(0 nearest, 1 linear, 2 cubic, 3 mitchell, 4 lanczos2, 5 lanczos3, 6 mks2013, 7 mks2021)
),
'smartcrop' => array(
'interesting' => 1 // crop area, default 3, VipsInteresting(0 none, 1 centre, 2 entropy, 3 attention, 4 low, 5 high, 6 all)
),
'jpegsave' => array(
'Q' => 86, // quality factor, defaults wp 82, php 75, gd 75, vips 75
'interlace' => false, // progressive jpeg, default false
'optimize_coding' => true, // optimize huffman tables, defaults gd false, vips false, sharp-js true
'quant_table' => 3, // quantization table, default 0, defaults gd 0, mozjpeg 3, vips 0, enum (0 JPEG Annex K, 1 flat, 2 MSSIM tuned, 3 mozjpeg default, 4 PSNR-HVS-M tuned, 5, 6, 7, 8)
'trellis_quant' => true, // trellis code quantization, default false
// 'overshoot_deringing' => false, // oveshooting samples to reduce ringing artifacts, default false
// 'optimize_scans' => false, // progressive jpeg, optimize DCT coefficients, default false
// 'subsample_mode' => 0, // jpeg chroma subsampling, default 0, VipsForeignSubsample(0 auto, 1 YUV420, 2 YUV444)
// 'background' => '#ffffff',
// 'page_height' => 0, // page height on multipage, min 0, max 100000000
'keep' => 0 // keep metadata flags, VipsForeignKeep(0 none, 1 exif, 2 xmp, 4 iptc, 8 icc, 16 other, 31 all)
),
'pngsave' => array(
'Q' => 100, // quantization value, default 100, min 0, max 100
'compression' => 9, // compression ratio, default 6, min 1, max 10
'dither' => 0, // dithering value, default 100, min 0, max 100
'interlace' => false, // progressive png, default false
'palette' => true, // PNG-8 256 colors palette, default false
// 'bitdepth' => 8, // png image bit-depth, default 8, min 1, max 16, enum (1 mono, 2 mono+alpha, 4 PNG-8, 8 PNG-24, 16 PNG-48)
// 'effort' => 7, // cpu effort on quantization, default 7, min 1, max 10
// 'filter' => 0, // libpng filter flags, default 8, VipsForeignPngFilter(8 none, 16 sub, 32 up, 64 avg, 128 paeth, 248 all)
// 'background' => '#ffffff',
// 'page_height' => 0, // page height on multipage, min 0, max 100000000
'keep' => 0 // keep metadata flags, VipsForeignKeep(0 none, 1 exif, 2 xmp, 4 iptc, 8 icc, 16 other, 31 all)
),
'gifsave' => array( // gifsave is cgifsave in vips
'dither' => 1, // dithering value, default 1, min 0, max 1
'interlace' => false, // progressive gif, default false
// 'reuse' => false, // reuse palette from input, default false
// 'interpalette_maxerror' => 3, // max inter-palette for reuse palette, default 3, max 256
// 'bitdepth' => 8, // gif palette bit-depth, default 8, min 1, max 8
// 'effort' => 7, // cpu effort on quantization, default 7, min 1, max 10
// 'keep_duplicate_frames' => false, // default false
// 'background' => '#ffffff',
// 'page_height' => 0, // page height on multipage, min 0, max 100000000
'keep' => 0 // keep metadata flags, VipsForeignKeep(0 none, 1 exif, 2 xmp, 4 iptc, 8 icc, 16 other, 31 all)
),
'webpsave' => array(
'Q' => 88, // quality factor, defaults wp 86, php 80, gd 75, vips 75
// 'lossless' => false, // lossless compression, default false (false = lossy compression)
'smart_deblock' => true, // webp smart deblocking filter, default false
'smart_subsample' => true, // webp smart chroma subsampling, default false
// 'effort' => 4, // cpu effort on file size, default 4, min 0, max 6
// 'passes' => 1, // compression passes number, default 1, min 1, max 10
// 'alpha_q' => 100, // alpha quality in lossy, default 100, min 0, max 100
'exact' => true, // preserve color alpha, default false
// 'kmin' => 2147483646, // min frames between key frames, default 2147483646, min 0, max 2147483647
// 'kmax' => 2147483647, // max frames between key frames, default 2147483647, min 0, max 2147483647
// 'min_size' => false, // optimize on min file size, default false
// 'target_size' => 0, // desired target size in bytes, default 0, min 0, max 2147483647
// 'mixed' => false, // allow mixed encoding, default false
// 'preset' => 0, // lossy presets, default 0, VipsForeignWebpPreset(0 default, 1 picture, 2 photo, 3 drawing, 4 icon, 5 text)
// 'near_lossless' => false, // pre-processing lossless using Q value, default false
// 'background' => '#ffffff',
// 'page_height' => 0, // page height on multipage, min 0, max 100000000
'keep' => 0 // keep metadata flags, VipsForeignKeep(0 none, 1 exif, 2 xmp, 4 iptc, 8 icc, 16 other, 31 all)
),
'avifsave' => array( // avifsave is heifsave in vips
'Q' => 55, // quality factor, defaults wp 52, php 52, vips 50
'bitdepth' => 10, // av1 image bit-depth, default 12, min 8, max 12
// 'lossless' => false, // lossless compression, default false (false = lossy compression)
// 'effort' => 4, // av1 cpu effort value, default 4, min 0, max 9
// 'subsample_mode' => 0, // av1 chroma subsampling, default 0, VipsForeignSubsample(0 auto, 1 YUV420, 2 YUV444)
// 'tune' => '', // av1 tuning parameters string
// 'background' => '#ffffff',
// 'page_height' => 0, // page height on multipage, min 0, max 100000000
'keep' => 0 // keep metadata flags, VipsForeignKeep(0 none, 1 exif, 2 xmp, 4 iptc, 8 icc, 16 other, 31 all)
)
) );
}
public function backend() {
// wasm-vips specific, SharedArrayBuffer require-corp same-origin cross-origin policies
$this->crossorigin_policies();
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), $this->queue_priority );
add_action( 'admin_print_footer_scripts', array( $this, 'thumbnailer_script' ), $this->queue_priority );
add_action( 'admin_print_footer_scripts', array( $this, 'postprocess_script' ), $this->queue_priority );
}
public function bulk_resizer() {
if ( current_user_can( 'upload_files' ) && current_user_can( 'edit_posts' ) ) {
add_filter( 'bulk_actions-upload', array( $this, 'bulk_resizer_generate_thumbnails' ) );
add_action( 'admin_print_footer_scripts', array( $this, 'bulk_resizer_script' ), $this->queue_priority );
}
}
/**
* Filters bulk actions in upload list table (media view)
*
* @see WP_List_Table
*
* @param array $actions Bulk actions for media view
* @return array $actions
*/
public function bulk_resizer_generate_thumbnails( $actions ) {
$actions = array( 'bulk-resizer' => __( 'Update File' ), ...$actions );
return $actions;
}
/**
* WP-Plupload image editor supports mime-type compatibility
*
* @see wp_plupload_default_settings()
*
* @param array $defaults WP-Plupload default settings
* @return array $defaults
*/
public function plupload_image_editor_supports_compat( $defaults ) {
if ( isset( $defaults['webp_upload_error'] ) )
unset( $defaults['webp_upload_error'] );
if ( isset( $defaults['avif_upload_error'] ) )
unset( $defaults['avif_upload_error'] );
return $defaults;
}
/**
* The thumbnailer script
*/
public function thumbnailer_script() {
include_once __DIR__ . '/inc/dist/thumbnailer-script' . ( SCRIPT_DEBUG ? '': '-min' ) . '.php';
}
/**
* Post-processing script: WP apiFetch middleware, editors and uploader
*/
public function postprocess_script() {
include_once __DIR__ . '/inc/dist/postprocess-script' . ( SCRIPT_DEBUG ? '' : '-min' ) . '.php';
}
/**
* Bulk resizer script: WP Media Library
*/
public function bulk_resizer_script() {
include_once __DIR__ . '/inc/dist/bulk-resizer-script' . ( SCRIPT_DEBUG ? '' : '-min' ) . '.php';
}
protected function crossorigin_policies() {
header( 'Cross-Origin-Embedder-Policy: require-corp' );
header( 'Cross-Origin-Opener-Policy: same-origin' );
}
public function rest_api() {
require_once __DIR__ . '/inc/class-vrannemstein-rest-controller.php';
$controller = new Vrannemstein_REST_Controller();
$controller->register_routes();
$controller->subsizes_filter_priority =& $this->subsizes_filter_priority;
}
public function enqueue_scripts() {
wp_register_script( 'vrannemstein', false, false, false, true );
wp_add_inline_script( 'vrannemstein', 'var vrannemstein_config = ' . wp_json_encode( $this->thumbnailer_config() ) . ';', 'before' );
wp_enqueue_script( 'vrannemstein' );
wp_enqueue_script( 'wasm-vips', plugins_url( 'node_modules/wasm-vips/lib/vips.js', __FILE__ ), false, $this::WASM_VIPS_VERSION, true );
}
}
add_action( 'init', '\Vrannemstein::init' );