2012-05-05 21 views
3

ImageMagickがWebフォントで動くことができるかどうかは誰にも知られていますか? (または、おそらくSTD INを介してフィード?)私は運と下の行を試してみました:ImagemagickとWebフォント

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

私の目標は、ユーザーがGoogleウェブフォントを選択できるようになります。

+0

にちょうど更新の一時フォントファイルを削除します - 私はフォントのリストを取得するには、GoogleのAPIを使用することが可能であると判断して、プルダウンましたがWFTを介したWOFFファイルですが、ImageMagickはWOFF形式のフォントをサポートしていません。誰かがTTFコンバータへのLinuxベースのWOFFを知っていれば、私は家にいないと思う。 – Anthony

+0

'-font。/ myfont.ttf'のようなファイルを指すために' -font'を使うことができる場所は? – Federico

答えて

3

最終更新:ユーザbarethonは、完全に動作するttf converterにpython woffを書きました。 (https://github.com/hanikesn/woff2otf/blob/master/woff2otf.py)私はGoogleから必要なフォントを裂いて、ttfに変換し、それをimagemagickで使用できるようになりました。私が気に入ったよりもやや複雑ですが、大したことはありません。

1

私は、これは、この時点でかなり古いですけど、いくつかの倍の貯蓄他人の利益のために、ここにあなたが欲しいものを行うにはいくつかの基本的な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(); 
+0

おもしろい回答!! – JayKandari

1

この問題へのあなたのアプローチはありがとうございます。同様の方法で解決しました。

font.ttfのように静的なファイル名ではなく乱数を使用した理由は、他のユーザーが同時にこの関数を呼び出していた場合に問題が発生する可能性があるからです。

  1. クエリGoogleのフォントリスト

    $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 
    
関連する問題