3

I want to use Imagick API for PHP instead of runnung command line in my PHP code.

exec("convert -gravity Center -size 200x200 -fill black -font Arial -pointsize 20 pango:\"Hello World!\" output.png");

for above command I have following Imagick API code:

<?php
$img = new Imagick();
$draw = new ImagickDraw();
$draw->setFont("Arial");
$draw->setFontSize(20);
$draw->setGravity( Imagick::GRAVITY_CENTER );
$img->newImage( 200, 200, "black", "png" );

//Pango code for Hello World!

$img->writeImage("output.png");
?>

But I could not find equal method/option for Pango. Do you know how can use Pango in Imagick API?

Ali
  • 243
  • 1
  • 5
  • 16

3 Answers3

3

Just improving Aite's answer by adding background color.

$img = new Imagick();
$img->setBackgroundColor(new ImagickPixel('black'));
$img->setFont("Arial");
$img->setPointSize(20);
$img->setGravity( Imagick::GRAVITY_CENTER );
$img->setImageFormat('jpg');

//Pango code for Hello World!
$img->newPseudoImage(200, 200, "pango:Hello World");

$img->writeImage("output.png");
  • So I tried your method and I keep getting unable to open image `Hello World'. Any ideas? Thanks! – Jeremy Jan 22 '19 at 21:43
2

The part pango:\"Hello World!\" is 'the name of a file' in ImageMagick. This means you should call readImage to 'read' the pango image:

$img->readImage("pango:\"Hello World!\"");

This will only work if ImageMagick is compiled with support for pango.

dlemstra
  • 7,813
  • 2
  • 27
  • 43
1

First of all, you don't need the ImagickDraw object, you only need to set your options to the Imagick object then to create/call new pseudo image (with pango: prefix) instead of regular new image method:

$img = new Imagick();
$img->setFont("Arial");
$img->setPointSize(20);
$img->setGravity( Imagick::GRAVITY_CENTER );
$img->setImageFormat('jpg');

//Pango code for Hello World!
$img->newPseudoImage(200, 200, "pango:Hello World");

$img->writeImage("output.png");

sorry that I couldn't find out how to set the fill color, but this is another issue you may know how to solve it.

P.S. You need to make sure you have pango installed, and imagemagick library is built with pango support (which is done for you since you are able to use command line).