Iconizr
A PHP command line tool for converting SVG images to a set of CSS icons (SVG & PNG, single icons and / or CSS sprites) with support for image optimization and Sass output. Created by Joschi Kuphal (@jkphl), licensed under the terms of the MIT license
Install / Use
/learn @jkphl/IconizrREADME
iconizr
![]()
is an – at present: proof-of-concept – command line tool that helps you prepare your vector based SVG icons for use with the widest possible range of devices. It takes a folder of SVG files and processes them to a bunch of files including
- cleaned versions of the original SVG icons (optional),
- a single compact SVG icon sprite,
- single PNG icons (optional),
- a combined PNG icon sprite,
- several CSS files with different formats, including
- SVG single image icons (optional),
- SVG data URIs,
- SVG sprite references,
- PNG single image icons (optional),
- PNG data URIs and
- PNG sprite references,
- Sass variants (scss dialect) of these CSS files for easy inclusion into your Sass project,
- a HTML fragment including some JavaScript for asynchronously loading the most appropriate icon variant
- and finally a couple of HTML preview documents (depending on the options you specified) for previewing and testing the different icon variants.
To leverage iconizr to the greatest extent possible, some additional tools have to be installed on the system independently (see below).
iconizr is written in (namespaced) PHP and meant to be used on the (Linux) command line (i.e. with the PHP CLI SAPI). To be honest, there was only one reason making me go for PHP: It is what I know best. Im sure there are quite some other fancy modern languages and techniques one could use for achieving the same, but I guess I'm just not smart enough for those. Having said this, I'd welcome anyone contributing a port of iconizr.
Getting started
As iconizr is written in PHP, there's nothing much of an installation process. Just put the iconizr.phps script (along with it's accompanying files and directories) to a location of your liking and make sure that it may be executed by setting the proper file permissions. If you'd like to simplify the invocation of iconizr you might create a symlink into your local executables directory, e.g.:
ln -s /path/to/iconizr.phps /usr/local/bin/iconizr
Run iconizr by calling the script with the apropriate command line options (see below). Please consider that – for security reasons – the output directory has to be a sub(sub)directory of the current working directory, so change your working directory first if necessary:
cd /path/to/website/docroot
/path/to/iconizr.phps --css --out css source/svg
or using the symlink mentioned above:
iconizr --css --out css source/svg
This will take all the SVG files found in /path/to/website/docroot/source/svg and render the resulting CSS files and sprites to the directory /path/to/website/docroot/css (and below). For the sake of simplicity, Sass files have been omitted in this example.
Documentation
Usage:
iconizr [options] input/dir1 [input/dir2 ...]
<table>
<tr>
<td>Short</td>
<td>Long</td>
<td>Value</td>
<td>Description</td>
</tr>
<tr>
<td><code>-o</code></td>
<td><code>--out</code></td>
<td>Directory path</td>
<td>Provide the path of the output directory here. For security reasons, the output directory has to be a sub(sub)directory of the current working directory. If it doesn't exist, iconizr will try to create it. Along with the input directory this is a mandatory argument.</td>
</tr>
<tr>
<td> </td>
<td><code>--sassout</code></td>
<td>Directory path</td>
<td>If you want the Sass files to be created in a separate directory, then specify it using this argument. The same restrictions apply as for the standard output directory. If you don't specify this argument, the Sass files will be created in the standard output directory.</td>
</tr>
<tr>
<td><code>-c</code></td>
<td><code>--css</code></td>
<td>String (optional)</td>
<td>If this argument is given, several CSS files will be generated. By default, the filenames will all start with "iconizr" (e.g. "iconizr-svg-data.css"). If you provide a string value for this argument (e.g. "myproject"), this value will be used as the prefix for all generated CSS files (e.g. "myproject-svg-data.css").</td>
</tr>
<tr>
<td><code>-s</code></td>
<td><code>--sass</code></td>
<td>String (optional)</td>
<td>Identical to the <code>-c</code> argument, but applying to the generation of Sass files.</td>
</tr>
<tr>
<td><code>-p</code></td>
<td><code>--prefix</code></td>
<td>String</td>
<td>This is the prefix used for all CSS class names generated. It defaults to "icon", so that a SVG file named <code>example.svg</code> results in a CSS class named "icon-example".</td>
</tr>
<tr>
<td><code>-l</code></td>
<td><code>--level</code></td>
<td>Integer</td>
<td>This is the optimization level for PNG files. It has to lie between 0 and 11 (inclusive) and defaults to 4, with 0 meaning "no optimization", 1 meaning "fast & rough" and 11 meaning "slow & high quality". Setting this to a high value may result in a very long processing time.</td>
</tr>
<tr>
<td><code>-q</code></td>
<td><code>--quantize</code></td>
<td> </td>
<td>If given, iconizr will quantize PNG images (i.e. convert them to 8-bit color depth; please see the <a href="#requirements">requirements</a> for this). The quantized images are only used if they are smaller in file size than their the originals (and this is not necessarily the case for all PNG files). Quantization may also impact the visual image quality, so please make sure to compare the result to the original images.</td>
</tr>
<tr>
<td><code>-e</code></td>
<td><code>--embed</code></td>
<td> </td>
<td>If given, iconizr will use this value as a prefix to the CSS directory path used to embed the stylesheets into your HTML documents (inside the <a href="example/css/weather-loader-fragment.html">JavaScript loader fragment</a>). By default, the <code>--out</code> directory will be used as a root-relative path (i.e. giving <code>path/to/css</code> as <code>--out</code> directory will result in e.g. <code>/path/to/css/weather-svg-sprite.css</code> as CSS embed path). You may specify a period <code>.</code> here to make the embed path relative to your HTML document (i.e. <code>./path/to/css/weather-svg-sprite.css</code>), or use any other arbitrary relative path (e.g. <code>../resources</code> for the embed path <code>../resources/path/to/css/weather-svg-sprite.css</code>).</td>
</tr>
<tr>
<td> </td>
<td><code>--svg</code></td>
<td>Integer</td>
<td>This is the maximum length a SVG data URI may have. If only one icon exceeds this threshold, all data URIs of this icon set will be changed to external SVG sprite references. Defaults to 1048576 (1MB), minimum is 1024 (1kB).</td>
</tr>
<tr>
<td> </td>
<td><code>--png</code></td>
<td>Integer</td>
<td>This is the maximum length a PNG data URI may have. If only one icon exceeds this threshold, all data URIs of this icon set will be changed to external PNG sprite references. Defaults to 32768 (32KB = Internet Explorer 8 limit), minimum is 1024 (1kB).</td>
</tr>
<tr>
<td><code>-d</code></td>
<td><code>--dims</code></td>
<td> </td>
<td>If given, a second CSS rule for each icon will be created, setting the dimensions of the icon (via the <code>width</code> and <code>height</code> properties).</td>
</tr>
<tr>
<td> </td>
<td><code>--width</code></td>
<td>Integer</td>
<td>Default width for icons (in pixels) in case the SVG file is missing a value. Must be between 1 and the maximum width (including).</td>
</tr>
<tr>
<td> </td>
<td><code>--height</code></td>
<td>Integer</td>
<td>Default height for icons (in pixels) in case the SVG file is missing a value. Must be between 1 and the maximum height (including).</td>
</tr>
<tr>
<td> </td>
<td><code>--maxwidth</code></td>
<td>Integer</td>
<td>Maximum width for icons (in pixels). Defaults to 1000.</td>
</tr>
<tr>
<td> </td>
<td><code>--maxheight</code></td>
<td>Integer</td>
<td>Maximum height for icons (in pixels). Defaults to 1000.</td>
</tr>
<tr>
<td> </td>
<td><code>--padding</code></td>
<td>Integer</td>
<td>Transparent padding to be added around each icon (in pixels). Defaults to 0.</td>
</tr>
<tr>
<td> </td>
<td><code>--pseudo</code></td>
<td>String</td>
<td>You may bind an icon to a particular CSS pseudo class (like e.g. <code>:hover</code>, <code>:active</code>, <code>:target</code>). To do so, just append the desired pseudo class to the icon name, separated by a tilde <code>~</code>. For instance, an icon named <code>foo-bar~hover.svg</code> will result in a CSS class <code>icon-foo-bar:hover</code>. If you need to use another delimiter than a tilde <code>~</code>, just use this parameter to provide your custom string.</td>
</tr>
<tr>
<td><code>-k</code></td>
<td><code>--keep</code></td>
<td> </td>
<td>During the compilation of the SVG and PNG sprites, iconizr also creates intermediate SVG and PNG versions of each single icon. Usually, these intermediate files are deleted at the end of the compilation process. If you want to keep these files for some reason, just add this argument.<br/>In this case, also an additional set of stylesheets will be created (both CSS and/or Sass), which uses these single image icons (carrying the suffix <code>-single.html</code>). However, it is not recommended to use these stylesheets for production systems, and you should always prefer the CSS sprite or data URI variants for performance reasons. The si