https://github.com/smtws/gfontdownloader
php google fonts downloader with local css creation
https://github.com/smtws/gfontdownloader
downloader font-dow fonts google googlefonts mit-license object-oriented php psr-logger-compatible webfonts
Last synced: 6 months ago
JSON representation
php google fonts downloader with local css creation
- Host: GitHub
- URL: https://github.com/smtws/gfontdownloader
- Owner: smtws
- License: mit
- Created: 2020-01-17T08:09:04.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2025-03-17T15:52:10.000Z (7 months ago)
- Last Synced: 2025-03-25T13:17:21.484Z (7 months ago)
- Topics: downloader, font-dow, fonts, google, googlefonts, mit-license, object-oriented, php, psr-logger-compatible, webfonts
- Language: PHP
- Homepage:
- Size: 41 KB
- Stars: 3
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# gFontDownloader
[](https://packagist.org/packages/smtws/google-font-downloader)
[](https://packagist.org/packages/smtws/google-font-downloader)gFontDownloader is a PHP tool that allows you to download Google Fonts and create local CSS files for them.
## Features
- Download Google Fonts to a local directory
- Create local CSS files for the downloaded fonts
- PSR-3 compatible logging## Configuration
You can configure gFontDownloader using one of the following methods:
1. Set configuration in `config.json` and call `->setConfig()`
2. Set configuration via `->setConfig([array of configuration key-value pairs])`
3. Set configuration via `->setConfig($key, $value)`Combinations of these methods are possible.
### Configuration Options
- `output`: Directory where fonts are downloaded to (each font family will have its own subdirectory). Defaults to `./`
- `formats`: Optional array of font formats to be downloaded. Defaults to all valid values. Valid values are: `eot`, `woff`, `woff2`, `svg`, `ttf`
- `onRecoverableError`: How to handle recoverable errors. Valid values are: `stop` (default), `recover`## Adding Fonts to Download List
You can add fonts to the download list using one of the following methods:
- `->addFont(string $fontFamily, string $fontStyle, array $fontWeights)`
or
- `->addFontByUrl(string $urlOfFont)`
Examples:
- `"https://fonts.google.com/?selection.family=Gelasio:500i,700|Open+Sans|Roboto"`
- `"https://fonts.googleapis.com/css?family=Gelasio:500i,700|Open+Sans|Roboto&display=swap"`## Running the Downloader
To start the download process, call `->download()`. This method:
- Returns an array of all downloaded fonts
- Accepts a callback function that is passed information on each font individually## Error Handling
In case of unrecoverable errors, you can run `->createFamilyCssFiles()` to create Font Family CSS files for fonts that were successfully downloaded before the error occurred (see `example.php`).
## Logging
You can use PSR-3 compatible loggers with gFontDownloader:
```php
->setLogger(new \PSRCompatibleLogger());
```## Example Usage
Here is an example of how to use gFontDownloader:
```php
setConfig([
'output' => 'fonts/',
'formats' => ['woff2', 'ttf'],
'onRecoverableError' => 'recover'
]);// Add fonts to the download list
$downloader->addFont('Gelasio', 'italic', [500, 700]);
$downloader->addFontByUrl('https://fonts.googleapis.com/css?family=Open+Sans|Roboto&display=swap');// Set a logger (optional)
$downloader->setLogger(new \MyLogger());// Run the downloader
$downloadedFonts = $downloader->download();// Handle downloaded fonts
foreach ($downloadedFonts as $font) {
echo "Downloaded font: " . $font['family'] . "\n";
}```
For more details, refer to the [example.php](example.php) file.
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.