Skip to content

Instantly share code, notes, and snippets.

@fetchTe
Forked from nikoskip/gfonts.php
Created October 19, 2016 03:18
Show Gist options
  • Save fetchTe/b49ed585f68f3b59a9e8a6369a32bfe8 to your computer and use it in GitHub Desktop.
Save fetchTe/b49ed585f68f3b59a9e8a6369a32bfe8 to your computer and use it in GitHub Desktop.

Revisions

  1. @nikoskip nikoskip revised this gist Jun 1, 2016. 1 changed file with 9 additions and 25 deletions.
    34 changes: 9 additions & 25 deletions gfonts.php
    Original file line number Diff line number Diff line change
    @@ -3,29 +3,13 @@
    $fontTypes = array('woff2', 'woff', 'ttf', 'svg', 'eot');
    $gFontURL = 'http://fonts.googleapis.com/css?family=';

    function getUA($fontType) {
    switch ($fontType) {
    case 'woff2':
    $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36';
    break;
    case 'woff':
    $ua = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/4.0; GTB7.4; InfoPath.3; SV1; .NET CLR 3.1.76908; WOW64; en-US)';
    break;
    case 'ttf':
    $ua = 'Mozilla/5.0 (Linux; U; Android 2.2.1; en-ca; LG-P505R Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1';
    break;
    case 'svg':
    $ua = 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10';
    break;
    case 'eot':
    $ua = 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)';
    break;
    default:
    $ua = null;
    break;
    }
    return $ua;
    }
    $uaFonts = array(
    'woff2' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
    'woff' => 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/4.0; GTB7.4; InfoPath.3; SV1; .NET CLR 3.1.76908; WOW64; en-US)',
    'ttf' => 'Mozilla/5.0 (Linux; U; Android 2.2.1; en-ca; LG-P505R Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1',
    'svg' => 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10',
    'eot' => 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)'
    );

    function curlGoogleFont($url, $ua) {
    $curl = curl_init();
    @@ -87,11 +71,11 @@ function parseCss($css) {
    $fontsDownloadLinks = array();

    foreach ($fontTypes as $fontType) {
    $content = curlGoogleFont($gFontURL, getUA($fontType));
    $content = curlGoogleFont($gFontURL, $uaFonts[$fontType]);
    $fontsDownloadLinks[$fontType] = parseCss($content);
    }
    }

    }
    ?>

    <!doctype html>
  2. @nikoskip nikoskip revised this gist Jun 1, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gfonts.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    <?php

    $fontTypes = array('woff2', 'woff', 'ttf', 'svg', 'eot');
    $gFontURL = 'http://fonts.googleapis.com/css?family=';

    @@ -122,7 +123,7 @@ function parseCss($css) {
    ?>

    <hr>
    <a href="https://twitter.com/nikoskip">Contact</a>
    <a href="https://twitter.com/nikoskip">Contact</a> | <a href="https://gist.github.com/nikoskip/bcf58106f728a3e7b7f9dcb6edaa1e82">Download code</a>
    </head>
    <body>

  3. @nikoskip nikoskip renamed this gist Jun 1, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @nikoskip nikoskip revised this gist Jun 1, 2016. No changes.
  5. @nikoskip nikoskip created this gist Jun 1, 2016.
    130 changes: 130 additions & 0 deletions Download fonts from Google Fonts CSS
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,130 @@
    <?php
    $fontTypes = array('woff2', 'woff', 'ttf', 'svg', 'eot');
    $gFontURL = 'http://fonts.googleapis.com/css?family=';

    function getUA($fontType) {
    switch ($fontType) {
    case 'woff2':
    $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36';
    break;
    case 'woff':
    $ua = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/4.0; GTB7.4; InfoPath.3; SV1; .NET CLR 3.1.76908; WOW64; en-US)';
    break;
    case 'ttf':
    $ua = 'Mozilla/5.0 (Linux; U; Android 2.2.1; en-ca; LG-P505R Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1';
    break;
    case 'svg':
    $ua = 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10';
    break;
    case 'eot':
    $ua = 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)';
    break;
    default:
    $ua = null;
    break;
    }
    return $ua;
    }

    function curlGoogleFont($url, $ua) {
    $curl = curl_init();

    curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_HEADER => 1,
    CURLOPT_VERBOSE => 1,
    CURLOPT_URL => $url,
    CURLOPT_USERAGENT => $ua
    ));
    ;
    if (($response = curl_exec($curl))) {
    $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
    $header = substr($response, 0, $header_size);

    if (strrpos($header, 'Content-Type: text/css')) {
    return $response;
    }
    }
    }

    function parseCss($css) {
    $fonts = array();
    $fontName = null;
    $url = null;

    foreach (preg_split('/\r\n|\n|\r/', $css) as $cssLine) {
    // We save the font-family name for EOT
    if (strpos($cssLine, 'font-family')) {
    preg_match("/'(.*?)'/i", $cssLine, $data);
    $fontName = $data[1];
    }
    if (strpos($cssLine, 'url')) {
    preg_match('/local\((.*?)\)/i', $cssLine, $data);
    if (count($data)) {
    $fontName = str_replace('\'', '', $data[1]);
    }
    }
    if (strpos($cssLine, 'src')) {
    preg_match('/url\((.*?)\)/i', $cssLine, $data);
    $url = $data[1];
    }

    if ($fontName !== null && $url !== null) {
    $fonts[$fontName] = $url;
    $fontName = null;
    $url = null;
    }
    }

    return $fonts;
    }

    if (isset($_POST['url'])) {
    $urlParts = parse_url($_POST['url']);
    parse_str($urlParts['query'], $queryParts);
    $gFontURL .= urlencode($queryParts['family']);
    $fontsDownloadLinks = array();

    foreach ($fontTypes as $fontType) {
    $content = curlGoogleFont($gFontURL, getUA($fontType));
    $fontsDownloadLinks[$fontType] = parseCss($content);
    }
    }

    ?>

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title></title>
    <p>Thanks for visiting. Please provide your Google Fonts URL to import in your CSS (example: http://fonts.googleapis.com/css?family=Cabin:500,700,500italic,700italic) and you will get links for download <?php echo implode(', ', $fontTypes); ?> fonts.</p>
    <form action="" method="post">
    Google Fonts URL: <input type="text" name="url" value="<?php if(isset($_POST['url'])) echo $_POST['url']; ?>">
    <input type="submit" value="Get fonts!">
    </form>

    <?php if (isset($fontsDownloadLinks)) { ?>
    <hr>
    <?php foreach ($fontsDownloadLinks as $font => $links) { ?>
    <strong><?php echo $font; ?></strong>

    <?php echo count($links) === 0 ? '<p>Not found</p>' : ''; ?>
    <table border="1" cellspacing="0" cellpadding="2">
    <tr>
    <?php foreach ($links as $fontName => $link) { ?>
    <td><a href="<?php echo $link; ?>"><?php echo $fontName; ?></a></td>
    <?php } ?>
    </table>
    <?php
    }
    }
    ?>

    <hr>
    <a href="https://twitter.com/nikoskip">Contact</a>
    </head>
    <body>

    </body>
    </html>