Snippets › Adding Imagick Text Shadow in PHP

  • Published on 4th Sep 2017

This example code shows how you can add text shadow to your texts using Imagick draw. The idea is to create two sets of texts using annotateImage(), first one is the shadow and another one is the text. We just move shadow text x and y position by 1 pixel to the east-south to achieve that shadow effect. This is a very basic example if you have some advance example please do share :

<?php
$text_to_right = "Imagick makes image manipulation in PHP\n extremely easy through an OO interface";

//create a new Image
$canvas = new Imagick();
$canvas->newImage(400, 70, "white");

//Set Imagick Draw for texts
$draw = new ImagickDraw();
$draw->setGravity (Imagick::GRAVITY_CENTER); //center text
$draw->setFontSize(18); //set font size  
//$draw->setFont($font_path); //set font if use different font

//text shadow
$draw->setFillColor("rgb(132,132,132)"); //set shadow color
$canvas->annotateImage($draw, 0 + 1, 0 + 1, 0, $text_to_right); 

//now the original text
$draw->setFillColor("rgb(0,166,206)"); //set text color
$canvas->annotateImage($draw, 0, 0, 0, $text_to_right);

$canvas->setImageFormat('png');
header("Content-Type: image/png");
echo $canvas;
?>