Link to home
Start Free TrialLog in
Avatar of ray-solomon
ray-solomonFlag for United States of America

asked on

Using GD to add text over image with drop shadow blur

I have a 24-bit png file that I need to overlay a text with a drop shadow.
The drop shadow is a copy of the text with an offset and Gaussian blur.

I need help getting this to work.

I attached an example image and existing code.
<?php

/* 24-bit PNG source image */
$src_image = 'image.png';

/* create new image from source image */
$im_base = @imagecreatefrompng($src_image);
$width = imagesx($im_base);
$height = imagesy($im_base);

/*
$drop_shadow = imagecreatetruecolor($width, $height);

$black = imagecolorallocate($drop_shadow, 0, 0, 0);
imagecolortransparent($drop_shadow, $black);
*/

/* 
$transparent = imagecolorallocatealpha($img, 0,0,0, 127); 
*/

/* alpha blending on 
imagealphablending($drop_shadow, true);*/

/* save alphablending setting 
imagesavealpha($drop_shadow, false); */



/* drop shadow text 
$font = 'Banty.ttf';
$fontSize = 70; // points
$fontRotation = 0; //  0 degrees
$fontX = -5; // pixels
$fontY = 75; // pixels
$fontColor = imagecolorallocate($drop_shadow, 0xD6, 0xD6, 0xD6); // hex or RGB
$text = "Test";
*/

/* add text to image 
imagettftext($drop_shadow, $fontSize, $fontRotation, $fontX, $fontY, $fontColor, $font, $text);
*/

/* add blur shadow 
imagefilter($drop_shadow, IMG_FILTER_GAUSSIAN_BLUR);
imagefilter($drop_shadow, IMG_FILTER_GAUSSIAN_BLUR);
imagefilter($drop_shadow, IMG_FILTER_GAUSSIAN_BLUR);
*/

/*
imagecopymerge($im, $drop_shadow, 50, 50, 50, 50, $width, $height, 100);
imagecopyresampled($im, $drop_shadow, 0, 0, 0, 0, $width, $height, $width, $height);
*/

/* customize the text 
$font = 'Banty.ttf';
$fontSize = 70; // points
$fontRotation = 0; //  0 degrees
$fontX = 1; // pixels
$fontY = 70; // pixels
$color = imagecolorallocate($im, 0xFF, 0xFF, 0xFF); // hex or RGB
$text = "Test";
*/

/* add text to image 
@imagettftext($im_base, $fontSize, $fontRotation, $fontX, $fontY, $color, $font, $text);
*/


/* output image */
header('Content-type: image/png');
@imagepng($im_base);
/* empty the buffer */
@imagedestroy($im_base);

?>

Open in new window

image.png
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Maybe it is not working because large segments of the code are commented out?
Have a look at this script and see if it helps.
http://www.laprbass.com/RAY_imagepng_drop_shadow.php?sr=34&sb=34&sg=139
<?php // RAY_imagepng_drop_shadow.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO WRITE TEXT WITH A DROP SHADOW IN A PNG IMAGE


// GET SOME TEXT
$string = isset($_GET["t"]) ? $_GET["t"] : 'Hello World!';

// SETTINGS FOR THE BACKGROUND COLOR (DEFAULT WHITE)
$rgb_br = isset($_GET["br"]) ? $_GET["br"] : 255;
$rgb_bg = isset($_GET["bg"]) ? $_GET["bg"] : 255;
$rgb_bb = isset($_GET["bb"]) ? $_GET["bb"] : 255;

// SETTINGS FOR THE TEXT (DEFAULT GREENISH)
$rgb_tr = isset($_GET["tr"]) ? $_GET["tr"] :  34;
$rgb_tg = isset($_GET["tg"]) ? $_GET["tg"] : 139;
$rgb_tb = isset($_GET["tb"]) ? $_GET["tb"] :  34;

// SETTINGS FOR THE DROP SHADOW (DEFAULT BLACK)
$rgb_sr = isset($_GET["sr"]) ? $_GET["sr"] :   0;
$rgb_sg = isset($_GET["sg"]) ? $_GET["sg"] :   0;
$rgb_sb = isset($_GET["sb"]) ? $_GET["sb"] :   0;

// SETTING FOR SHADOW TRANSPARENCY / INTENSITY
$dense  = isset($_GET["d"])  ? $_GET["d"]  :  90;

// A FONT: KRISTEN
$font = 'itckrist.ttf';

// IMAGE RESOURCE DIMENSIONS
$wide   = 200;
$high   = 60;

// LOCATION OF THE TEXT
$x_off  = 25;
$y_off  = 25;

// SIZE AND ANGLE OF THE TEXT
$size   = 20;
$angle  = 0;



// CREATE THE IMAGE RESOURCE
$im     = ImageCreateTrueColor($wide, $high);

// SET COLORS: MAN PAGE http://us.php.net/manual/en/function.imagecolorallocatealpha.php
$bgcolor = ImageColorAllocateAlpha($im, $rgb_br, $rgb_bg, $rgb_bb, 0);
$shadow  = ImageColorAllocateAlpha($im, $rgb_sr, $rgb_sg, $rgb_sb, $dense);
$text    = ImageColorAllocateAlpha($im, $rgb_tr, $rgb_tg, $rgb_tb, 0);

// FILL THE BACKGROUND: MAN PAGE http://us.php.net/manual/en/function.imagefilledrectangle.php
ImageFilledRectangle($im, 0, 0, $wide, $high, $bgcolor);

// MAN PAGE http://us.php.net/manual/en/function.imagealphablending.php
ImageAlphaBlending($im, TRUE);

// MAN PAGE http://us.php.net/manual/en/function.imagesavealpha.php
ImageSaveAlpha($im, TRUE);

// SHADOW FIRST TO CREATE THE BLUR: MAN PAGE http://us.php.net/manual/en/function.imagettftext.php
ImageTTFtext($im, $size, $angle, $x_off, $y_off+5, $shadow, $font, $string);

// BLUR BY APPROXIMATIONS: MAN PAGE http://us.php.net/manual/en/function.imagefilter.php
ImageFilter($im, IMG_FILTER_GAUSSIAN_BLUR);
ImageFilter($im, IMG_FILTER_GAUSSIAN_BLUR);
ImageFilter($im, IMG_FILTER_GAUSSIAN_BLUR);

// MORE SHADOW AND SOME BLUR
ImageTTFtext($im, $size, $angle, $x_off, $y_off+3, $shadow, $font, $string);
ImageFilter($im, IMG_FILTER_GAUSSIAN_BLUR);
ImageFilter($im, IMG_FILTER_GAUSSIAN_BLUR);

// MORE SHADOW AND LESS BLUR
ImageTTFtext($im, $size, $angle, $x_off, $y_off+1, $shadow, $font, $string);
ImageFilter($im, IMG_FILTER_GAUSSIAN_BLUR);

// THEN TEXT
imagettftext($im, $size, $angle, $x_off, $y_off,   $text,   $font, $string);

// PRODUCE A PNG IMAGE
header('Content-type: image/png');
ImagePNG($im);
ImageDestroy($im);

Open in new window

Avatar of ray-solomon

ASKER

Hi Ray, thank you for helping.

Question.

I need the text and drop shadow to go over a background image without blurring the background image. That is the issue that hung me up. Is this possible?

Currently you are using ImageFilledRectangle(), but it needs to be an existing png image instead. This is my problem.

After many code revisions, I just gave you code bits which did not work because I was tired and frustrated. At some point before I requested help, I had it almost working, but gave up because I could not get rid of the black background behind the shadow text. I just needed to get it to be transparent. I spend all day reading the php manual on every GD image function that could help and of course google as well.
Thanks for clarifying that.  Let me look at it again.  Back in a while...
Just so you know I haven't forgotten this Q,...

I am running into difficulties preserving the transparency after using the Gaussian blur.  I may have to try a different strategy.
greetings ray-solomon, , as already noted, the Gaussian blur is a very additive alpha-blend factor and I did not like trying to work wid it, you seem to want blurred edges shadows, you might try my code below, to see if it get any effect as you need, , I did not blur the shadows, bot use the "transpaency" and drew two more shadows, offset from the main one, with more transparency, to give a blurred edge effect. . .
you seem to have some experience wid GD functions, so I did not comment heavily. . . I you have questions, be sure to ask, and tell us if this gives anything like you need for your effect.
<?php

$src_image = 'images3/spaco.jpg';

$im_base = imagecreatefromjpeg($src_image);
$w = imagesx($im_base);
$h = imagesy($im_base);


$drop_shadow = imagecreatetruecolor($w, $h);

$imgColor = imagecolorallocatealpha($drop_shadow, 255,255,255, 127);// transparent

// to get the Background to be transparent you MUST cut OFF the alphablending
imagealphablending($drop_shadow, false);
imagefilledrectangle($drop_shadow, 0, 0, $w, $h, $imgColor);
// to get the text to be semi-transparent you MUST cut ON the alphablending
imagealphablending($drop_shadow, true);

$imgColor = imagecolorallocatealpha($drop_shadow, 97, 97, 97, 70);
//this first shadow text is the main shadow comoponent
imagettftext($drop_shadow, 70, 0, 12, 85, $imgColor, 'academ.ttf', 'Shadow Test');

//these two next shadow text are offset and "more transparent", giving the impression of "blurred" shadow edges
$imgColor = imagecolorallocatealpha($drop_shadow, 97, 97, 97, 100);
imagettftext($drop_shadow, 70, 0, 10, 83, $imgColor, 'academ.ttf', 'Shadow Test');
// remember these draws are additive to the first showdow draw
imagettftext($drop_shadow, 70, 0, 14, 87, $imgColor, 'academ.ttf', 'Shadow Test');


//imagefilter($drop_shadow, IMG_FILTER_GAUSSIAN_BLUR);
//imagefilter($drop_shadow, IMG_FILTER_GAUSSIAN_BLUR);
//imagefilter($drop_shadow, IMG_FILTER_GAUSSIAN_BLUR);
$imgColor = imagecolorallocatealpha($drop_shadow, 255, 255, 255, 0);// NO transparency
imagettftext($drop_shadow, 70, 0, 2, 78, $imgColor, 'academ.ttf', 'Shadow Test');


imagealphablending($im_base, true);

imagecopy($im_base, $drop_shadow, 0, 0, 0, 0, $w, $h);
imagesavealpha($im_base, true);
//header('Content-type: image/png');
@imagepng($im_base, 'images3/dshadow1.png');
@imagedestroy($im_base);
@imagedestroy($drop_shadow);

?>

// in my test page I show result with <img src="images3/dshadow1.png">

Open in new window

sorry, I just noticed, for smaller file size you may want to turn off the alpha save for the image-  $im_base


imagesavealpha($im_base, false);// does NOT need to be alpha transparent
@imagepng($im_base, 'images3/dshadow1.png');
Maybe it will be easier if using a GIF instead of a PNG?
I can go either way as long as the drop shadow looks really good like the picture above.
@Slick812 - good quick solution. However, I could have done that myself.
Is there no way to do Gaussian blur?

@Ray_Paseur - using a JPG works fine too as long as the text and drop shadow is transparent.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Brilliant. I understand.

Stacking the drop shadow text multiple times while offsetting and applying a Gaussian blur to each layer.
Then it increments the opacity for each drop shadow layer and merges each one together, then the text itself is merged on top.

Thank you.
good solution.
Thanks for the points - it's an interesting question!  Best regards, ~Ray