{"id":20114185,"url":"https://github.com/iconify/json-tools.php","last_synced_at":"2026-03-14T06:02:05.771Z","repository":{"id":56988729,"uuid":"155606144","full_name":"iconify/json-tools.php","owner":"iconify","description":"Functions for manipulating Iconify JSON SVG collections (PHP version)","archived":false,"fork":false,"pushed_at":"2020-12-16T13:04:36.000Z","size":42,"stargazers_count":15,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-30T12:53:12.065Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iconify.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-31T18:34:36.000Z","updated_at":"2024-09-02T05:15:48.000Z","dependencies_parsed_at":"2022-08-21T12:50:32.746Z","dependency_job_id":null,"html_url":"https://github.com/iconify/json-tools.php","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/iconify/json-tools.php","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iconify%2Fjson-tools.php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iconify%2Fjson-tools.php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iconify%2Fjson-tools.php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iconify%2Fjson-tools.php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iconify","download_url":"https://codeload.github.com/iconify/json-tools.php/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iconify%2Fjson-tools.php/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268625644,"owners_count":24280369,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-03T02:00:12.545Z","response_time":2577,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T18:28:35.160Z","updated_at":"2026-03-14T06:02:00.718Z","avatar_url":"https://github.com/iconify.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Iconify JSON tools\n\nThis library is used to manipulate JSON icon collections.\n\nThe library is available for PHP and Node.js, code in both versions is almost identical.\n\n## Installation\n\nTo install the library run this command:\n\n```\ncomposer require iconify/json-tools\n```\n\nThere are two classes in this package: `Collection` and `SVG`\n\n## Collection class\n\nCollection class represents an icon set.\n\nTo include it use this code:\n\n```php\nuse \\Iconify\\JSONTools\\Collection;\n```\n\nWhat can Collection class do?\n\n- Read and write JSON collections.\n- Add, remove, list icons in an icon set.\n- Retrieve icon data.\n- Create icon bundles for Iconify icon sets.\n\n### Initializing class instance\n\nThere are two ways to create an instance: with icon set prefix and without icon set prefix.\n\nYou can skip icon set prefix in the constructor if you are going to load data from a JSON file because JSON files contain icon set prefix.\n\n```php\n$collection = new Collection();\n```\n\n```php\n$collectionWithPrefix = new Collection('custom-icons');\n```\n\n### Loading JSON data\n\nThere are several functions to load an icon set from JSON file:\n\n- `loadFromFile()` - loads collection.\n- `loadJSON()` - loads JSON data from string or array.\n- `loadIconifyCollection()` - loads Iconify collection from `iconify/json` repository.\n\n#### loadFromFile()\n\nThis function loads an icon set from a JSON file.\n\nFunction parameters:\n\n- `$file` - file to load data from.\n- `$defaultPrefix` - optional default prefix in case if JSON file does not have it.\n- `$cacheFile` - cache file for a parsed icon set. This option does not exist in Node.js version of the library. Use this to speed up loading.\n\nReturns:\n\n- boolean - true on success, false on failure\n\n```php\n$collection = new Collection();\nif (!$collection-\u003eloadFromFile('json/custom-icons.json')) {\n    throw new \\Exception('Failed to load custom-icons.json');\n}\n```\n\n```php\n$collection = new Collection();\nif (!$collection-\u003eloadFromFile('json/custom-icons.json', null, 'cache/custom-icons.php')) {\n    throw new \\Exception('Failed to load custom-icons.json');\n}\n```\n\n#### loadJSON()\n\nThis function loads an icon set from a string or an array.\n\nFunction parameters:\n\n- `$data` - JSON string or array.\n- `$prefix` - optional prefix if JSON file doesn't include one.\n\nReturns:\n\n- boolean - true on success, false on failure\n\n```php\n$collection = new Collection();\n// Use this if collection has prefix\nif (!$collection-\u003eloadJSON($data)) {\n    throw new \\Exception('Failed to load JSON data');\n}\n```\n\n```php\n$collection = new Collection();\n// Use this if collection is missing prefix\nif (!$collection-\u003eloadJSON($data, 'custom-icons')) {\n    throw new \\Exception('Failed to load JSON data');\n}\n```\n\n#### loadIconifyCollection()\n\nThis function loads Iconify collection from [iconify/json](https://github.com/iconify-design/collections-json) repository.\n\nFunction parameters:\n\n- `$name` - the name of the icon set.\n- `$dir` - optional root directory of Iconify icon set. Use this option if you want to load Iconify icon set from a custom directory instead of the `iconify/json` repository.\n\nReturns:\n\n- boolean - true on success, false on failure\n\n```php\n$collection = new Collection();\nif (!$collection-\u003eloadIconifyCollection('mdi')) {\n    throw new \\Exception('Failed to load Material Design Icons');\n}\n```\n\n### Caching icon sets\n\nPHP loads icon sets on every page load, so it makes sense not to parse the same data many times. This is why PHP version of the library has caching functions.\n\n#### loadFromCache()\n\nThis function loads icon set from the cache.\n\nFunction parameters:\n\n- `$filename` - cached filename.\n- `$time` - time stamp (retrieved with `filemtime()` function) of the original JSON file. This parameter is used to invalidate cache if JSON file has been updated since the last time cache was saved.\n\nReturns:\n\n- boolean - true on success, false on failure\n\n```php\n$collection = new Collection();\n$file = Collection::findIconifyCollection('mdi');\nif (!$collection-\u003eloadFromCache('cache/mdi.php', filemtime($file))) {\n    if (!$collection-\u003eloadFromFile($file)) {\n        throw new \\Exception('Cannot load Material Design Icons');\n    }\n    $collection-\u003esaveCache('cache/mdi.php', filemtime($file));\n}\n```\n\n### saveCache()\n\nStores icon set data in the cache.\n\nThis function does not return anything.\n\nFor a usable example, see `loadFromCache()` example above.\n\n### Getting icon data\n\nSeveral functions can be used to retrieve icon data from an icon set:\n\n- `getIconData()` - returns full data for one icon. It can be used to generate SVG (see SVG class documentation below).\n- `getIcons()` - returns JSON data for icons, which can be used to import to another JSON collection or can be added to Iconify using `Iconify.addCollection()`.\n- `scriptify()` - returns JavaScript bundle file that can be used to load icons in browser with Iconify.\n\n#### getIconData()\n\nThis function returns JSON data for one icon. It returns full data, including optional fields, so the result can be used to generate SVG.\n\nFunction parameters:\n\n- `$name` - icon name.\n\nReturns:\n\n- array - icon data\n\n```php\n$data = $collection-\u003egetIconData('arrow-left');\n$svg = new SVG($data);\necho $svg-\u003egetSVG();\n```\n\n#### getIcons()\n\nThis function returns JSON data for selected icons. If used without parameters, it returns JSON data for an entire icon set.\n\nParameters:\n\n- `$icons` - icon names array.\n\n```php\n$data = $collection-\u003egetIcons(['arrow-left', 'arrow-right', 'home']);\nfile_put_contents('bundle.json', json_encode($data));\n```\n\nThis function can also be used to copy collection:\n\n```php\n$data = $collection-\u003egetIcons();\n$newCollection = new Collection();\n$newCollection-\u003eloadJSON($data);\n```\n\nUsing `$collection-\u003egetIcons()` without parameters is the same as accessing `$collection-\u003eitems` array.\n\n#### scriptify()\n\nThis is similar to `getIcons()`, but it generates JavaScript file instead of JSON data and parameters are passed as one array.\n\nFunction parameters:\n\n- `$options` - options array.\n\nReturns:\n\n- string - JavaScript code you can bundle with your scripts.\n\nOptions array keys:\n\n- `icons` - an array of icons to retrieve. If not set or null, all icons will be retrieved.\n- `optimize` - boolean. If set to true, JSON data will be optimized to make output smaller.\n- `pretty` - boolean. If set to true, JSON data will include white spaces that make output easy to read.\n- `callback` - string. JavaScript callback to wrap JSON data in. The default value is `Iconify.addCollection`.\n\nCode to create a bundle with selected icons from one collection (repeat same code for different collections to make bundle of all icons used on website):\n\n```php\n$collection = new Collection();\nif (!$collection-\u003eloadIconifyCollection('mdi')) {\n    throw new \\Exception('Cannot load Material Design Icons');\n}\n$code = $collection-\u003escriptify([\n    'icons' =\u003e ['account', 'account-alert', 'home', 'book-open'],\n    'pretty' =\u003e false,\n    'optimize' =\u003e true\n]);\nfile_put_contents('bundle-mdi.js', $code);\n```\n\n### Adding/removing icons\n\n#### addIcon()\n\nThis function adds a new icon to the icon set.\n\nFunction parameters:\n\n- `$name` - icon name.\n- `$data` - icon data.\n\nReturns:\n\n- boolean - true on success, false on failure. Failure is possible if an icon is missing 'body' property of if the icon set has no prefix.\n\n```php\n$collection = new Collection('custom-icons');\n$collection-\u003eaddIcon('arrow', [\n    'body' =\u003e '\u003cpath d=\"\" /\u003e',\n    'width' =\u003e 24,\n    'height' =\u003e 24\n]);\n```\n\n### addAlias()\n\nThis function adds an alias for an existing icon.\n\nFunction parameters:\n\n- `$name` - alias name.\n- `$parent` - parent icon name.\n- `$data` - optional data that should override parent icon's data (such as rotation or flip).\n\nReturns:\n\n- boolean - true on success, false on failure. Failure is possible if the parent icon is missing.\n\n```php\n$collection = new Collection('custom-icons');\n$collection-\u003eaddIcon('arrow-left', [\n    'body' =\u003e '\u003cpath d=\"\" /\u003e',\n    'width' =\u003e 24,\n    'height' =\u003e 24\n]);\n$collection-\u003eaddAlias('arrow-right', 'arrow-left', [\n    'hFlip' =\u003e true\n]);\n$collection-\u003eaddAlias('arrow-right-alias', 'arrow-right');\n```\n\n### setDefaultIconValue()\n\nSet default value for all icons.\n\nFunction parameters:\n\n- `$key` - attribute name.\n- `$value` - default value.\n\n```php\n$collection-\u003esetDefaultIconValue('height', 24);\n```\n\n### removeIcon()\n\nRemoves an icon or an alias from the icon set.\n\nFunction parameters:\n\n- `$name` - icon name.\n- `$checkAliases` - if true, the icon set will be checked for aliases that use removed icon as parent icon and those aliases will be removed too. Set to false if you know for sure there are no aliases referencing this icon, otherwise set to true.\n\n```php\n$collection = new Collection();\n$collection-\u003eloadIconifyCollection('fa-solid');\n$collection-\u003eremoveIcon('home');\n```\n\n### iconExists()\n\nChecks if an icon or an alias exists.\n\nFunction parameters:\n\n- `$name` - icon name.\n\nReturns:\n\n- boolean - true or false\n\n```php\nif (!$collection-\u003eiconExists('home')) {\n    throw new \\Exception('Missing \"home\" icon!');\n}\n```\n\n### listIcons()\n\nLists all icons in an icon set.\n\nFunction parameters:\n\n- `$includeAliases` - set to true to include aliases in the result.\n\nReturns:\n\n- array - list of icons\n\n```php\n$collection = new Collection();\n$collection-\u003eloadIconifyCollection('vaadin');\necho 'Available icons in vaadin collection: ', implode(', ', $collection-\u003elistIcons(true)), \"\\n\";\n```\n\n### Other functions\n\n#### items\n\nThis is a property, not a function. You can use it to have access to raw JSON data. Value is the same as using `getIcons()` without parameters, however editing the result of `getIcons()` will not affect collection data because it copies array.\n\nEditing the `$collection-\u003eitems` array will change collection data.\n\n#### prefix()\n\nReturns the icon set prefix, `false` if the icon set has no prefix.\n\nReturns:\n\n- string|boolean - Prefix, `false` on error.\n\n```php\n$prefix = $collection-\u003eprefix();\n```\n\n#### findIconifyCollection()\n\nThis function locates Iconify icon set from [iconify/json](https://github.com/iconify-design/collections-json) repository.\n\nFunction parameters:\n\n- `$name` - Prefix of the icon set.\n- `$dir` - Optional root directory where Iconify icon sets are stored. Use this option if you want to load Iconify icon set from a custom directory instead of the `iconify/json` repository.\n\nReturns:\n\n- string - location of the file.\n\n```php\necho 'fa-solid.json can be found at ', Collection::findIconifyCollection('fa-solid'), \"\\n\";\n```\n\n#### optimize()\n\nOptimize is a static function that optimizes JSON data. It modifies an array passed by reference in the first parameter.\n\nFunction parameters:\n\n- `$data` - JSON data to optimize, passed by reference.\n- `$props` - an optional array of properties to optimize. If not set, default properties list will be used.\n\n```php\n$data = $collection-\u003egetIcons();\nCollection::optimize($data);\n```\n\n#### deOptimize()\n\nOpposite of the previous function. It converts optimized JSON data into full JSON data, making it easy to retrieve data for each icon.\n\nFunction parameters:\n\n- `$data` - JSON array to de-optimize.\n\n```php\n$data = json_decode(file_get_contents('ant-design.json'), true);\nCollection::deOptimize($data);\n```\n\n## SVG class\n\nThe `SVG` class generates code for the icon.\n\nTo include it use this code:\n\n```php\nuse \\Iconify\\JSONTools\\SVG;\n```\n\n### Initializing class instance\n\n```php\n$svg = new SVG($data);\n```\n\n### Getting SVG icon\n\nThe `SVG` class has one function: `getSVG()`. It returns SVG as a string.\n\n```php\nuse \\Iconify\\JSONTools\\Collection;\nuse \\Iconify\\JSONTools\\SVG;\n\n$collection = new Collection();\n$collection-\u003eloadIconifyCollection('mdi');\n$svg = new SVG($collection-\u003egetIconData('home'));\necho $svg-\u003egetSVG();\n```\n\n`getSVG()` has one parameter: custom properties array. Possible array keys:\n\n- `inline` - if true or \"true\" or \"1\" (string or boolean), code will include `vertical-align` style, making it behave like a glyph. See [inline vs block article](https://iconify.design/docs/inline-vs-block/).\n- `width`, `height` - dimensions of icon. If only one attribute is set, another attribute will be set using icon's width/height ratio. Value can be string (such as \"1em\", \"24px\" or number). If value is \"auto\", icon's original dimensions will be used. If both width and height are not set, height defaults to \"1em\".\n- `hFlip`, `vFlip` - if true or \"true\" or \"1\" (string or boolean), icon will be flipped horizontally and/or vertically.\n- `flip` - alternative to \"hFlip\" and \"vFlip\", string. Value can be \"horizontal\", \"vertical\" or \"horizontal,vertical\"\n- `rotate` - rotation. Value can be in degrees \"90deg\" (only 90, 180 and 270 rotations are available), percentages \"25%\" (25%, 50% and 75% are aliases of 90deg, 180deg and 270deg) or number 1-3 (1 - 90deg, 2 - 180deg, 3 - 270deg).\n- `align` - alignment. This is useful if you have custom width and height set. Unlike other images, SVG keep aspect ratio (unless stated otherwise) when scaled. Value is comma or space separated string with possible strings (example: \"left,top,crop\"):\n  - `left`, `right`, `center` - horizontal alignment\n  - `top`, `middle`, `bottom` - vertical alignment\n  - `crop` - crop parts that go outside of boundaries\n  - `meet` - scale icon down to fit entire icon (opposite of crop)\n- `color` - custom color string to replace currentColor. This is useful when using icon as background image because background image cannot use currentColor\n- `box` - if true or \"true\" or \"1\" (string or boolean), icon will include extra rectangle matching its view box. This is useful to export icon to editor making icon easier to scale or place into its position in sketch because often editors ignore viewBox.\n\n```php\n$svg-\u003egetSVG([\n    'height' =\u003e '24px'\n]);\n$svg-\u003egetSVG([\n    'height' =\u003e '24px',\n    'width' =\u003e '24px',\n    'align' =\u003e 'center,middle,meet',\n    'color' =\u003e '#ff8000',\n    'rotate' =\u003e '90deg', // same as \"'rotate' =\u003e  1\" or \"'rotate' =\u003e '25%'\"\n    'flip' =\u003e 'horizontal', // same as \"'hFlip' =\u003e true\"\n    'box' =\u003e true\n]);\n$svg-\u003egetSVG([\n    'height' =\u003e 'auto' // height and width will be set from viewBox attribute, using original icon's dimensions\n]);\n```\n\n## License\n\nThe library is released with MIT license.\n\n© 2018 - 2020 Vjacheslav Trushkin\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficonify%2Fjson-tools.php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ficonify%2Fjson-tools.php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficonify%2Fjson-tools.php/lists"}