Draw text using custom font using ImageMagick and PHP

I want to dynamically visualize the image text using a special font, preferably with the ability to output directly or save to a file. And to automatically set the image size according to the font / size combination.

I can already do this with GD, but it does not handle fonts where characters overlap.

So now I'm looking for ImageMagick. I found an example in the docs that seems to do what I want. Is this possible with php_magick? Especially when the image size is not set :) If this is not the case, can I get the magy command line to output the raw image, so I can pass it directly to the client with PHP?

Thanks!


The real question is probably this: how do I convert the IM command below to PHP code using php_magick?

convert -background lightblue -fill blue -font Arial -pointsize 72 label:Anthony
+2
source share
2 answers

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 raw PNG data that can then be output to the browser. Replace png:-with the file name to save it to a file.

Remember to use escapeshellargif you are using user settings here.

+3
source

You must use the annotateImageclass function Imagickto duplicate this functionality.

Here's the direct copy-paste from the 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;
+8
source

Source: https://habr.com/ru/post/1605885/


All Articles