Imagemagick 和网络字体

问题描述 投票:0回答:5

有人知道 ImageMagick 是否可以由网络字体提供支持吗? (或者也许通过 STD IN 提供信息?)我尝试了下面的行,但没有成功:

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

我的目标是让用户选择谷歌网络字体。

text fonts imagemagick
5个回答
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

$


4
投票

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


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();

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. 像这样访问字体的网址:

    <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
    

0
投票

您可以使用以下一种方式获取所需的 Google Web 字体,然后将其与 imagemagick 一起使用:

wget -qO- 'https://fonts.googleapis.com/css?family=Noto+Sans:400' | grep -Eo 'http.*://[^ >]+' | sed 's/.ttf)/.ttf/' | wget -i - -O 'Noto Sans.ttf'

然后我使用以下脚本生成基于文本的缩略图,将所有我最喜欢的 ttf 字体文件复制到之前的同一文件夹中:

ls -1 *.ttf | while read line
do
magick -gravity center -background '#086cdf' -fill '#f1fffe' -size 490x400 -font "$line" caption:"Sample Text" -background red -extent 500x500 "$(echo "$line"|sed 's/.ttf/_icon.png/')"
done

以上脚本生成图标大小的缩略图,大小为 500x500 像素。

对于像 youtube 这样的社交媒体平台,缩略图的大小必须为 1280x720,为此我使用以下脚本来生成它:

ls -1 *.ttf | while read line
do
magick -gravity center -background '#086cdf' -fill '#f1fffe' -size 1270x620 -font "$line" caption:"Sample Text" -background red -extent 1280x720 "$(echo "$line"|sed 's/.ttf/_Social_Media_Platforms.png/')"
done

希望所有这些脚本能帮助人们在谷歌上搜索解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.