Imagemagick和Web字体

4
有人知道ImageMagick是否可以使用Web字体(或通过STD IN输入)进行操作吗?我尝试了以下命令但没有成功:
convert -fill black -font http://fonts.googleapis.com/css?family=Diplomata+SC -pointsize 72 label:'Acme Dynamite Company, LLC' logo.png

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


只是一个更新 - 我已经确定可以使用Google API获取字体列表,然后通过WGET下载WOFF文件,但是ImageMagick似乎不支持WOFF格式的字体。如果有人知道基于Linux的WOFF到TTF转换器,我想我就可以轻松解决了。 - Anthony
你能否使用 -font 来指向一个文件,例如 -font ./myfont.ttf - Federico
5个回答

5

4
最终更新:用户barethon编写了一个Python WOFF转TTF转换器,它运行得非常完美。(https://github.com/hanikesn/woff2otf/blob/master/woff2otf.py)我现在可以从Google下载我想要的字体,将其转换为TTF格式,并与ImageMagick一起使用。虽然比我想象中要稍微复杂一些,但没什么大不了的。

2
感谢Floss对这个问题的解决方法。我以类似的方式解决了它。
我之所以使用随机数而不是像font.ttf这样的静态文件名,是因为如果其他用户同时调用该函数,可能会出现问题。
  1. Query the Google Fonts list

    $url = 'https://www.googleapis.com/webfonts/v1/webfonts?key=YOUR KEY HERE';
    
    $responseString = file_get_contents($url);
    $fontJSON = json_decode($responseString);
    
  2. Access the url of the fonts like so:

    <select name="font">
      <?php foreach ($fontJSON->items as $font): ?>
        <option value="<?= $font->files->regular ?>"><?= $font->family ?></option>
      <?php endforeach; ?>
    </select>
    
    • Note that you'd have to do some other trickery to select the variants (like bold or italic) but that's beyond this post.
  3. After passing the form data to the server, use it in the following way.

    //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. Delete the temporary font file

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

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

有趣的回答!! - JayKandari

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

希望这些脚本能帮助到需要解决问题的人。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接