How to host google web fonts on my own server?

前端 未结 19 2071
闹比i
闹比i 2020-11-27 09:10

I need to use some google fonts on an intranet application. The clients may or may not have internet connection. Reading the license terms, it appears that its legally allo

19条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 09:41

    You may follow the script which is developed using PHP. Where you can download any google fonts by using the script. It will download the fonts and create a CSS file and archive to zip.
    You can download the source code from the GitHub https://github.com/sourav101/google-fonts-downloader

    $obj = new GoogleFontsDownloader;
    
    if(isset($_GET['url']) && !empty($_GET['url']))
    {
        $obj->generate($_GET['url']);
    }
    
    if(isset($_GET['download']) && !empty($_GET['download']) && $_GET['download']=='true')
    {
        $obj->download();
    }
    

    /**
    * GoogleFontsDownloader
    * Easy way to download any google fonts.
    * @author     Shohrab Hossain
    * @version    1.0.0 
    */
    class GoogleFontsDownloader
    {
        private $url      = '';
        private $dir      = 'dist/';
        private $fontsDir = 'fonts/';
        private $cssDir   = 'css/';
        private $fileName = 'fonts.css';
        private $content  = '';
        private $errors   = '';
        private $success  = '';
        public  $is_downloadable  = false;
    
        public function __construct()
        {
            ini_set('allow_url_fopen', 'on');
            ini_set('allow_url_include', 'on');
        }
    
        public function generate($url = null)
        {
            if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) 
            {
                $this->errors .= "
  • Invalid url! $url
  • "; } else { $this->url = $url; // delete previous files $this->_destroy(); // write font.css $this->_css(); // write fonts $this->_fonts(); // archive files $this->_archive(); } // show all messages $this->_message(); } public function download() { // Download the created zip file $zipFileName = trim($this->dir, '/').'.zip'; if (file_exists($zipFileName)) { header("Content-type: application/zip"); header("Content-Disposition: attachment; filename = $zipFileName"); header("Pragma: no-cache"); header("Expires: 0"); readfile("$zipFileName"); // delete file unlink($zipFileName); array_map('unlink', glob("$this->dir/*.*")); rmdir($this->dir); } } private function _archive() { if (is_dir($this->dir)) { $zipFileName = trim($this->dir, '/').'.zip'; $zip = new \ZipArchive(); if ($zip->open($zipFileName, ZipArchive::CREATE) === TRUE) { $zip->addGlob($this->dir. "*.*"); $zip->addGlob($this->dir. "*/*.*"); if ($zip->status == ZIPARCHIVE::ER_OK) { $this->success .= '
  • Zip create successful!
  • '; $this->is_downloadable = true; } else { $this->errors .= '
  • Failed to create to zip
  • '; } } else { $this->errors .= '
  • ZipArchive not found!
  • '; } $zip->close(); } else { $this->errors .= "
  • File not exists!
  • "; } } private function _css() { $filePath = $this->dir.$this->cssDir.$this->fileName; $content = $this->_request($this->url); if (!empty($content)) { if (file_put_contents($filePath, $content)) { $this->success .= "
  • $this->fileName generated successful!
  • "; $this->content = $content; } else { $this->errors .= '
  • Permission errro in $this->fileName! Unable to write $filePath.
  • '; } } else { $this->errors .= '
  • Unable to create fonts.css file!
  • '; } } private function _fonts() { if (!empty($this->content)) { preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $this->content, $match); $gFontPaths = $match[0]; if (!empty($gFontPaths) && is_array($gFontPaths) && sizeof($gFontPaths)>0) { $count = 0; foreach ($gFontPaths as $url) { $name = basename($url); $filePath = $this->dir.$this->fontsDir.$name; $this->content = str_replace($url, '../'.$this->fontsDir.$name, $this->content); $fontContent = $this->_request($url); if (!empty($fontContent)) { file_put_contents($filePath, $fontContent); $count++; $this->success .= "
  • The font $name downloaded!
  • "; } else { $this->errors .= "
  • Unable to download the font $name!
  • "; } } file_put_contents($this->dir.$this->cssDir.$this->fileName, $this->content); $this->success .= "
  • Total $count font(s) downloaded!
  • "; } } } private function _request($url) { $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_SSL_VERIFYPEER => FALSE, CURLOPT_HEADER => FALSE, CURLOPT_FOLLOWLOCATION => TRUE, CURLOPT_URL => $url, CURLOPT_REFERER => $url, CURLOPT_RETURNTRANSFER => TRUE, )); $result = curl_exec($ch); curl_close($ch); if (!empty($result)) { return $result; } return false; } private function _destroy() { $cssPath = $this->dir.$this->cssDir.$this->fileName; if (file_exists($cssPath) && is_file($cssPath)) { unlink($cssPath); } else { mkdir($this->dir.$this->cssDir, 0777, true); } $fontsPath = $this->dir.$this->fontsDir; if (!is_dir($fontsPath)) { mkdir($fontsPath, 0777, true); } else { array_map(function($font) use($fontsPath) { if (file_exists($fontsPath.$font) && is_file($fontsPath.$font)) { unlink($fontsPath.$font); } }, glob($fontsPath.'*.*')); } } private function _message() { if (strlen($this->errors)>0) { echo "
      $this->errors
    "; } if (strlen($this->success)>0) { echo "
      $this->success
    "; } } }

提交回复
热议问题