Importeasy_svg_pro_import_font_max_bytes

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:

Parameters

body
$bytesint
Required

The largest font file, in bytes, that the set import reads. 16777216 by default.

Return value

returnint

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.

  1. The route checks the set, the sanitizer, the uploaded file and the hardener.
  2. If the file name ends in .zip, the ZIP reader runs and this filter is not used.
  3. Otherwise the file is treated as an SVG font. Its size on disk is compared with your limit before it is read.
  4. Too large: the answer is 413 with the code easy_svg_pro_file_too_large and the message "That font is too large to import."
  5. 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 .svg is read as a font, and a font named .zip goes 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_filesize stops larger files before this check.