easy_svg_pro_import_font_max_bytes
Set the largest SVG icon font the Easy SVG Pro set import reads. Bigger fonts are refused with HTTP 413 before they are loaded into memory.
Filter · Easy SVG Pro · Status: Stable
The set editor can import an SVG icon font and turn each glyph into its own icon. A font file is read into memory in one piece, so Easy SVG Pro checks its size first and refuses anything above 16 MB. This filter sets that size.
Usage
apply_filters( 'easy_svg_pro_import_font_max_bytes', int $bytes )
Applied in includes/core/rest.php, inside rest_import_set(). The result is cast to an integer.
When to use it
- An older icon font (some ship as one big SVG file) is refused as too large.
- You want to forbid large font imports on a small host.
Do not use it when:
- You import a ZIP. ZIP files use
easy_svg_pro_import_max_entriesandeasy_svg_pro_import_max_total_bytes. - You upload a single SVG to the media library or the icon library. That is the free plugin's
easy_svg_max_bytes.
Parameters
The largest font file, in bytes, that the set import reads. 16777216 by default.
Return value
The size limit in bytes. A file larger than this is refused. A file of exactly this size passes.
Default
16777216 (16 MB).
Examples
Allow one bigger legacy font
function acme_import_font_max_bytes( $bytes ) {
return max( $bytes, 24 * MB_IN_BYTES );
}
add_filter( 'easy_svg_pro_import_font_max_bytes', 'acme_import_font_max_bytes' );
Small fonts only
/**
* Our fonts are under 2 MB. Anything bigger is a mistake.
*/
function acme_import_font_small( $bytes ) {
return min( (int) $bytes, 2 * MB_IN_BYTES );
}
add_filter( 'easy_svg_pro_import_font_max_bytes', 'acme_import_font_small' );
Handle the refusal in your own client
const res = await fetch( `/wp-json/easy-svg-pro/v1/sets/${ setId }/import`, {
method: 'POST',
headers: { 'X-WP-Nonce': wpApiSettings.nonce },
body: formData,
} );
if ( res.status === 413 ) {
const err = await res.json();
// err.code === 'easy_svg_pro_file_too_large'
}
How it works
The check lives in POST /easy-svg-pro/v1/sets/{id}/import, the route behind the import button of the set editor. It needs manage_options, an active licence and the Icon set editor feature.
- The route checks the set, the sanitizer, the uploaded file and the hardener.
- If the file name ends in
.zip, the ZIP reader runs and this filter is not used. - Otherwise the file is treated as an SVG font. Its size on disk is compared with your limit before it is read.
- Too large: the answer is
413with the codeeasy_svg_pro_file_too_largeand the message "That font is too large to import." - Small enough: the font is read, each glyph becomes an icon, and every icon is sanitized and hardened.
The route only parses. It answers the icons and a count, and the set editor merges them into its state. Nothing is saved until the person presses save.
Pitfalls
- Only the file name decides. A ZIP renamed to
.svgis read as a font, and a font named.zipgoes to the ZIP reader and imports nothing. - Parsing still costs memory. A font is parsed as XML after it is read. A limit far above 16 MB can still run out of memory on a small host.
- Not the upload limit. PHP's
upload_max_filesizestops larger files before this check.