Skip to content

Instantly share code, notes, and snippets.

@deepaksp
Last active July 7, 2023 15:00
Show Gist options
  • Save deepaksp/6253dc7a5bae877c7991b414817e465f to your computer and use it in GitHub Desktop.
Save deepaksp/6253dc7a5bae877c7991b414817e465f to your computer and use it in GitHub Desktop.
Google Fonts to Local Fonts - make google font as local font without any hassle with php
<?php
$css_name = 'ko_gothic.css';
$font_link = '/resources/themes/interad/assets/css';
$base_dir = __DIR__ . '/resources/themes/interad/assets/css';
$file = 'https://fonts.googleapis.com/css2?family=Nanum+Gothic&display=swap';
$options = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'));
$context = stream_context_create($options);
$response = file_get_contents($file, false, $context);
$matches = [];
preg_match_all('/url\((https:\/\/fonts.gstatic.com\/[^)]+)\)/', $response, $matches);
$css = preg_replace('/https:\/\/fonts.gstatic.com\/s/', $font_link, $response);
file_put_contents($base_dir . '/' . $css_name, $css);
for ($i = 0; $i < count($matches[0]); $i++) {
$uris = explode('/', $matches[1][$i]);
$uris = array_slice($uris, -3, 3);
$font_name = $uris[0];
$file_ver = $uris[1];
$file_uri = $uris[2];
$path = $base_dir .'/'. $font_name . '/' . $file_ver;
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
file_put_contents($path . '/' . $file_uri, file_get_contents($matches[1][$i]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment