├── .gitignore ├── README.md ├── downloader.php └── index.php /.gitignore: -------------------------------------------------------------------------------- 1 | google-fonts.css 2 | fonts -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Google Fonts Downloader 2 | == 3 | **A PHP script to download [Google Fonts](https://www.google.com/fonts) so you can use them in offline** 4 | 5 | Usage 6 | -- 7 | * Open the script in your browser and copy/paste the google `` code in the input box and press "Download". A sample `` for Open Sans is there by default. 8 | * After downloading, you can find the fonts in `fonts` directory and the css file as `google-fonts.css` file in the root directory. 9 | 10 | Note 11 | -- 12 | I use assets directory structure like this: 13 | ``` 14 | assets/ 15 | ├── css/ 16 | │ └── google-fonts.css 17 | └── fonts/ 18 | ├── cJZKeOuBrn4kERxqtaUH3aCWcynf_cDxXwCLxiixG1c.ttf 19 | ├── DXI1ORHCpsQm3Vp6mXoaTYnF5uFdDttMLvmWuJdhhgs.ttf 20 | └── k3k702ZOKiLJc3WVjuplzInF5uFdDttMLvmWuJdhhgs.ttf 21 | ``` 22 | So the path in the font in the css file is `url(../fonts/font-file.ttf)`. So if you use different directory structure to manage your assets, then change the **line no 57** in downloader.php. 23 | -------------------------------------------------------------------------------- /downloader.php: -------------------------------------------------------------------------------- 1 | $font ) { 44 | $font = rtrim( $font, ")" ); 45 | 46 | $font_file = explode( "/", $font ); 47 | $font_file = array_pop( $font_file ); 48 | 49 | // download font 50 | $ch = curl_init(); 51 | $fp = fopen ( "fonts/{$font_file}", 'w+' ); 52 | $ch = curl_init( $font ); 53 | curl_setopt( $ch, CURLOPT_TIMEOUT, 50 ); 54 | curl_setopt( $ch, CURLOPT_FILE, $fp ); 55 | curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 ); 56 | curl_exec( $ch ); 57 | curl_close( $ch ); 58 | fclose( $fp ); 59 | 60 | // replace string 61 | $css_file_contents = str_replace( $font, "../fonts/{$font_file}", $css_file_contents ); 62 | } 63 | 64 | $fh = fopen ( $css_file, 'w+' ); 65 | fwrite( $fh, $css_file_contents ); 66 | fclose( $fh ); 67 | 68 | $msg = "Done!"; 69 | } 70 | } 71 | } 72 | 73 | // recursive delete files/directories in a directory 74 | // http://php.net/manual/en/function.rmdir.php 75 | function delTree( $dir ) { 76 | $files = array_diff( scandir( $dir ), array( '.', '..' ) ); 77 | foreach ( $files as $file ) { 78 | ( is_dir( "$dir/$file" ) && !is_link( $dir ) ) ? delTree( "$dir/$file" ) : unlink( "$dir/$file" ); 79 | } 80 | 81 | return rmdir($dir); 82 | } 83 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 8 | * @license http://opensource.org/licenses/gpl-2.0.php GPL v2 or later 9 | * @link https://github.com/ediamin/google-fonts-downloader 10 | */ 11 | 12 | require_once 'downloader.php'; 13 | ?> 14 | 15 | 16 |
17 | 18 |Done!
87 | 88 | 92 |