4

有谁知道 ImageMagick 是否可以由网络字体驱动?(或者也许通过 STD IN 输入?)我尝试了下面的行但没有运气:

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

我的目标是让用户选择一种 Google Web 字体。

4

4 回答 4

5

如果你 wget GWF API,它会返回一个 TTF url:

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

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

$

于 2012-05-12T00:54:26.580 回答
4

最后更新:用户barethon写了一个python woff to ttf转换器,效果很好。(https://github.com/hanikesn/woff2otf/blob/master/woff2otf.py)我现在可以从 Google 撕下我想要的字体,将其转换为 ttf 并将其与 imagemagick 一起使用。比我想要的稍微复杂一点,但没什么大不了的。

于 2012-05-05T21:19:13.287 回答
2

我知道这在这一点上已经很老了,但是为了节省别人的时间,这里有一些基本的 PHP 来做你想做的事。它可以优化为使用 curl 等,但这应该足以让人们继续前进。看起来,当从浏览器访问时,它返回一个 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();
于 2015-07-14T22:48:34.803 回答
2

感谢 Floss 解决这个问题的方法。我以类似的方式解决了它。

我使用随机数而不是静态文件名的原因font.ttf是如果其他用户同时调用该函数,它可能会产生问题。

  1. 查询谷歌字体列表

    $url = 'https://www.googleapis.com/webfonts/v1/webfonts?key=YOUR KEY HERE';
    
    $responseString = file_get_contents($url);
    $fontJSON = json_decode($responseString);
    
  2. 像这样访问字体的 url:

    <select name="font">
      <?php foreach ($fontJSON->items as $font): ?>
        <option value="<?= $font->files->regular ?>"><?= $font->family ?></option>
      <?php endforeach; ?>
    </select>
    
    • 请注意,您必须做一些其他的技巧来选择变体(如粗体或斜体),但这超出了这篇文章的范围。
  3. 将表单数据传给服务器后,按如下方式使用。

    //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's 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
    
  4. 删除临时字体文件

    unlink("tmp/$random_number.ttf"); //remove tmp font
    
于 2017-07-21T04:11:23.990 回答