Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CLI tool to download Google Fonts into a local directory, in WOFF2 format, while rewriting Google Fonts CSS to point to local files.
<?php
/************************************************************************************************
* Google Font Packager
* Version 1.0 (2018-02-11)
* by Matheus Pratta (https://github.com/matheusmk3)
*/
if (!isset($argv[1]))
die ('Usage: google-font-packager.php [google-font-import-url [output-file.css]]');
// Set our Google Fonts URL
$google_font_url = $argv[1];
// Test for 'output-file.css' argument
if (isset($argv[2]))
$raw_output_file = $argv[2];
else
$raw_output_file = 'output-file.css';
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function dd ($d) {
var_dump($d);
exit;
}
function _ ($data) { echo $data . "\r\n"; }
function wget ($url) {
_ ('Fetching: ' . $url);
$user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36';
$options = array(
CURLOPT_CUSTOMREQUEST =>"GET", //set request type post or get
CURLOPT_POST =>false, //set to GET
CURLOPT_USERAGENT => $user_agent, //set user agent
CURLOPT_COOKIEFILE =>"cookie.txt", //set cookie file
CURLOPT_COOKIEJAR =>"cookie.txt", //set cookie jar
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0,
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
_ ('Returned: ' . $err . ' (' . $errmsg . ')');
return $content;
}
// \/\*([\w\s\-]+)\*\/\n\@font-face {([\w\s\-\:\'\;\(\)\,\/\.\+]+)}
$REGEX_FONT = '/\/\*([\w\s\-]+)\*\/\n\@font-face {([\w\s\-\:\'\;\(\)\,\/\.\+]+)}/mis';
// font-family:[\s+]'([\s\w]+)';
$REGEX_FONT_FAMILY = "/font-family:[\s+]'([\s\w]+)'/mis";
// font-weight:[\s]([\d]+)
$REGEX_FONT_WEIGHT = '/font-weight:[\s]([\d]+)/mis';
// font-style:[\s]([\w]+);
$REGEX_FONT_STYLE = '/font-style:[\s]([\w]+);/mis';
// url\(([\S]+)\)
$REGEX_FONT_SOURCE = '/url\(([\S]+)\)/mis';
// \.([\w]+)
$REGEX_FONT_SOURCE_FORMAT = '/\.([\w]+)/mis';
_ ('Obtaining Google Font...');
// Fetch our Google Font
$raw_font = wget ($google_font_url);
_ ('Processing...');
preg_match_all($REGEX_FONT, $raw_font, $font_matches);
$font_locales = $font_matches[1];
$font_data = $font_matches[2];
_ ('Found ' . count($font_data) . ' fonts.');
// This is our output fonts CSS
$final_fonts_css = $raw_font;
$font_directory = 'fonts_local';
@mkdir ($font_directory);
foreach ($font_data as $i => $font) {
// Get font locale
$font_locale = trim($font_locales[$i]);
// Get font family name
preg_match ($REGEX_FONT_FAMILY, $font, $font_family);
$font_family = trim($font_family[1]);
// Get font weight
preg_match ($REGEX_FONT_WEIGHT, $font, $font_weight);
$font_weight = trim($font_weight[1]);
// Get font style
preg_match ($REGEX_FONT_STYLE, $font, $font_style);
$font_style = trim($font_style[1]);
// Get font source url
preg_match ($REGEX_FONT_SOURCE, $font, $font_source);
$font_source = $font_source[1];
// Get font source url format
preg_match_all ($REGEX_FONT_SOURCE_FORMAT, $font_source, $font_source_format);
$font_source_format = $font_source_format[1][count($font_source_format[1]) - 1];
// Set font final name
$font_output = $font_family . '-' . $font_locale . '-' . $font_weight . $font_style . '.' . $font_source_format;
$font_output_path = $font_directory . '/' . $font_output;
_ (' ');
_ ('Font ' . ($i + 1) . ' of ' . count($font_data) . '...');
_ ('Font Family: ' . $font_family);
_ ('Font Weight: ' . $font_weight);
_ ('Font Style: ' . $font_style);
_ ('Font Locale: ' . $font_locale);
_ ('Font Source: ' . $font_source);
_ ('Font Source Format: ' . $font_source_format);
_ ('Font Output: ' . $font_output);
_ ('Preparing source...');
$final_fonts_css = str_replace($font_source, $font_output_path, $final_fonts_css);
_ ('Downloading font...');
$font_source = wget ($font_source);
_ ('Writing font...');
file_put_contents($font_output_path, $font_source);
}
_ ('Writing output file...');
file_put_contents($raw_output_file, $final_fonts_css);
_ ('Finished!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.