Imagemagick and web fonts

Does anyone know if ImageMagick can work on a web font? (Or maybe through STD IN?) I tried the line below, no luck:

convert -fill black -font http://fonts.googleapis.com/css?family=Diplomata+SC -pointsize 72 label:'Acme Dynamite Company, LLC' logo.png

My goal is to let users select the Google web font.

+3
source share
4 answers

Final update: user barethon wrote python woff for a ttf converter that works fine. ( https://github.com/hanikesn/woff2otf/blob/master/woff2otf.py ) Now I can break the font I want from Google, convert it to ttf and use it with imagemagick. A little more complicated than I would like, but never mind.

+3
source

If you wget the GWF API, it returns the TTF URL:

$wget -qO- "http://fonts.googleapis.com/css?family=Diplomata+SC" | urlext2

http://themes.googleusercontent.com/static/fonts/diplomatasc/v1/JdVwAwfE1a_pahXjk5qpNonF5uFdDttMLvmWuJdhhgs.ttf

$

+4

, , , - PHP, , . , , , . , woff woff2 url, - tff.

    $fontUrl = 'http://fonts.googleapis.com/css?family=Anton';
    $fontDescription = file_get_contents($fontUrl);

    $startStr = 'url(';
    $startStrLen = strlen($startStr);
    $start = strpos($fontDescription, $startStr) + $startStrLen;
    $end = strpos($fontDescription, ')', $start);
    $tffUrl = substr($fontDescription, $start, $end - $start);

    $tffFile = '/tmp/anton.ttf';
    file_put_contents($tffFile, file_get_contents($tffUrl));

    $im = new Imagick();
    $im->setFont($tffFile);
    $im->newPseudoImage(100, 100, "caption:Hello");
    $im->setImageFormat('png');
    $im->setImageBackgroundColor(new ImagickPixel('transparent'));
    header('Content-Type: image/png');
    echo $im->__toString();
+1

Floss . .

, , , font.ttf, , , .

  • Google

    $url = 'https://www.googleapis.com/webfonts/v1/webfonts?key=YOUR KEY HERE';
    
    $responseString = file_get_contents($url);
    $fontJSON = json_decode($responseString);
    
  • URL- :

    <select name="font">
      <?php foreach ($fontJSON->items as $font): ?>
        <option value="<?= $font->files->regular ?>"><?= $font->family ?></option>
      <?php endforeach; ?>
    </select>
    
    • Please note that you will need to do some other tricks to select options (e.g. bold or italics), but no further than this post.
  • After submitting the form data to the server, use it as follows.

    //create a random number for a unique filename
    $random_number = intval( "0" . rand(1,9) . rand(0,9) . rand(0,9) . rand(0,9) . rand(0,9) );
    
    //download the TTF from Google server
    $font_contents = file_get_contents($font);
    
    //Make a local file of a unique name
    $font_file = fopen("/tmp/$random_number.ttf", "w"); 
    
    //Write data from the Google Font file into your local file
    fwrite($font_file, $font_contents); 
    
    //Close the file
    fclose($font_file);
    
    //Set iMagick to use this temporary file
    $draw->setFont("/tmp/$random_number.ttf");
    
    //DO YOUR iMagick STUFF HERE
    
  • Delete temporary font file

    unlink("tmp/$random_number.ttf"); //remove tmp font
    
+1
source

All Articles