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
LVL 10
ray-solomonAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Ray PaseurConnect With a Mentor Commented:
This seems to produce a useful result.  

http://www.laprbass.com/RAY_imagepng_drop_shadow2.php

Best regards, ~Ray
<?php // RAY_imagepng_drop_shadow2.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 TEXT (DEFAULT RED)
$rgb_tr = isset($_GET["tr"]) ? $_GET["tr"] : 255;
$rgb_tg = isset($_GET["tg"]) ? $_GET["tg"] :   1;
$rgb_tb = isset($_GET["tb"]) ? $_GET["tb"] :   1;

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

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

// SETTING FOR THE NUMBER OF DROP SHADOW LAYERS
$lcount = isset($_GET["n"])  ? $_GET["n"]  :  12;

// 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;

// GET AN IMAGE FROM THE LIBRARY
$doggie = ImageCreateFromPNG('RAY_temp_600x374.png');

// CREATE THE OVERLAY IMAGE RESOURCES
$x  = $lcount+1;
$im = array();
while ($x)
{
    $x--;
	$im[$x] = ImageCreateTrueColor($wide, $high);
	ImageAlphaBlending($im[$x], TRUE);

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

	ImageColorTransparent($im[$x], $bgcolor);

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

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

    // BLUR BY APPROXIMATIONS: MAN PAGE http://us.php.net/manual/en/function.imagefilter.php
    $blur_count = $x;
    if ($x)
    {
        while ($blur_count)
        {
            ImageFilter($im[$x], IMG_FILTER_GAUSSIAN_BLUR);
            $blur_count--;
        }
    }
}


// ADD THE TEXT BACK INTO ELEMENT ZERO
ImageFilledRectangle($im[0], 0, 0, $wide, $high, $bgcolor);
ImageTTFtext($im[0], $size, $angle, $x_off, $y_off,   $text,   $font, $string);

// PRODUCE A PNG IMAGE
$x = $lcount+1;
while ($x)
{
    $x--;
    if ($x > 1)
    {
        $d = (int)round($dense / (pow($x, 1.5)));
    }
    else
    {
        $d = (int)round($dense / 2);
    }
    ImageCopyMerge($doggie, $im[$x], 50, 50, 0, 0, $wide, $high, $d);
}
// FINAL TEXT
ImageCopyMerge($doggie, $im[0], 50, 50, 0, 0, $wide, $high, 100);
Header('Content-type: image/png');
ImagePNG($doggie);

Open in new window

RAY-temp-600x374.png
0
 
Ray PaseurCommented:
Maybe it is not working because large segments of the code are commented out?
0
 
Ray PaseurCommented:
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

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ray-solomonAuthor Commented:
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.
0
 
Ray PaseurCommented:
Thanks for clarifying that.  Let me look at it again.  Back in a while...
0
 
Ray PaseurCommented:
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.
0
 
Slick812Commented:
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

0
 
Slick812Commented:
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');
0
 
ray-solomonAuthor Commented:
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.
0
 
ray-solomonAuthor Commented:
@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.
0
 
ray-solomonAuthor Commented:
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.
0
 
Ray PaseurCommented:
Thanks for the points - it's an interesting question!  Best regards, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.