Draw text with custom font using ImageMagick and PHP

前端 未结 2 1514
时光取名叫无心
时光取名叫无心 2020-12-18 13:53

I want to dynamically render text to an image with a custom font, preferably with the option to output directly or save to a file. And to automatically

相关标签:
2条回答
  • 2020-12-18 14:15

    Decided to skip the API and use the command-line interface instead.

    convert -background lightblue -fill blue -font Arial -pointsize 72 label:Anthony png:-
    

    This returns the raw PNG data, which we can then output to the browser. Replace png:- with the filename to save to a file instead.

    Don't forget to use escapeshellarg if you are using user input as parameters here.

    0 讨论(0)
  • 2020-12-18 14:21

    You should be able to use the annotateImage function of the Imagick class to duplicate that functionality.

    Here's a straight up copy-paste from that documentation:

    <?php
    /* Create some objects */
    $image = new Imagick();
    $draw = new ImagickDraw();
    $pixel = new ImagickPixel( 'gray' );
    
    /* New image */
    $image->newImage(800, 75, $pixel);
    
    /* Black text */
    $draw->setFillColor('black');
    
    /* Font properties */
    $draw->setFont('Bookman-DemiItalic');
    $draw->setFontSize( 30 );
    
    /* Create text */
    $image->annotateImage($draw, 10, 45, 0, 
        'The quick brown fox jumps over the lazy dog');
    
    /* Give image a format */
    $image->setImageFormat('png');
    
    /* Output the image with headers */
    header('Content-type: image/png');
    echo $image;
    
    0 讨论(0)
提交回复
热议问题