Drop Shadows with GD Library

This is a very powerful technique that will let you make all sorts of different kinds of drop shadows quickly.

What you will need

  • Photoshop (or some kind of image editing software)
  • A server with PHP and GD Library Installed.
  • An Image where the drop shadow will go
  • A will to learn :)

The Process

  1. Create the drop shadow images in Photoshop
  2. Slice up the drop shadow graphic.
  3. Use GD Library to place the drop shadow beneath an Image of your choosing.
note: for those who don't want to do the Photoshop portion of this tutorial you may download the images here.

Part One: Creating the Drop Shadow Images

Open up Photoshop then go to File > New and set the following settings.

In the layers palette, click Create a new layer to create a new layer above "Background" name this new layer "base".

Make sure that "base" is selected and then type Ctrl + A (Cmd + A for Mac). This will select everything; notice the dashed line moving around the document. Now go to Select > Modify > Contract, give it a value of 15 pixels and hit “ok.” This will contract your selection by 15 pixels on all sides.

Your canvas should look like the one at the left.

Hit Shift + F5 to open up the fill dialog box. Select white from the dropdown list and hit OK. Your selection is now filled with white.

Double Click the layer named "base" to open up the layer style window. And set these settings below.

Tadaaa! You now have a drop shadow!

Part 2: Slicing up the Drop Shadow Graphic

The next step is to cut that image up so that we can use it in GD library.

Go to window > Info (or hit F8) on your keyboard.
Make sure that the Slice Tool is selected by pressing K on your keyboard.

Hold down Shift and in your image, draw a square going from the top left-hand corner of the canvas, keep drawing until the width and height in the info palette reads .88 inches.

Right click this slice and click Edit Slice Options, name it shadow_TL.

Do this for the remaining three corners and give them appropriate names. Such as shadow_BR and shadow_BL. Once you’re done it should look something like this.

Near the center on the left hand side of the canvas draw a slice from the left side of the canvas going to the left side of the square. Make sure that that slice is 1 pixel in height.

Right click this slice and click Edit Slice Options, name it shadow_L.

Do this for the remaining 3 sides. Again name them something like shadow_R, shadow_T, shadow_B. You should have something like this.

Go to File > Save for Web and Devices

From this window hit K to select the Slice Select Tool and then select all 8 slices that you have created. You may need to zoom in, in order to select the correct slice.
Click Save in the top right hand corner.
Adjust the properties as shown at the left.
Click the dropdown list next to custom and select Other at the bottom of the list.
Under the Saving Files section, make sure that Put Images in Folder is unchecked.
Press OK
Select the shadow folder and press Save. It now just saved a copy of all your slices into the folder. 8 Images at once! Pretty cool huh?!

Part 3: Integrating the Graphic with GD Library

We’re going to leave Photoshop now and journey on over to our friendly neighborhood script editor. I prefer NotePad++ but any pure text editor will suffice.
For this part we are dealing with three different sets of graphics.
  1. The graphic that you want to have the shadows go under.
  2. The shadow images that you just created.
  3. And a canvas to put the two together.
The canvas is the base for everything, the shadow graphics go on top of the canvas creating a kind of picture frame. Then your image goes inside the shadow picture frame.
Ok, the idea seems simple enough. Time to get started.
Open up your text editor and make a new file. Save it to the folder we called dropshadow and name it img.php.
Copy the following code into your file. For an explanation, please read the comments included.
 
// ----------------------------------------
// Image Storage 
// ----------------------------------------
 
//STORING YOUR IMAGE
 
$graphic = "image.gif"; 						 //the path to your image  
list($width, $height) = getImageSize($graphic);  //the width and height of your image 
 
 
//storing the image into memory so that we can use it with GD Library 
$image = imagecreatefromgif($graphic);
 
/*
Definition: imagecreatefromgif("folder/image.jpg"); 
This function creates a copy of an image and stores the image in a variable in php. 
*/
 
//STORING THE DROP SHADOW IMAGES 
//Below I'm storing all 8 shadow images into memory.
 
$tl = imagecreatefromgif("images/shadow_TL.gif");
$t  = imagecreatefromgif("images/shadow_T.gif"); 
$tr = imagecreatefromgif("images/shadow_TR.gif"); 
$r  = imagecreatefromgif("images/shadow_R.gif"); 
$br = imagecreatefromgif("images/shadow_BR.gif"); 
$b  = imagecreatefromgif("images/shadow_B.gif"); 
$bl = imagecreatefromgif("images/shadow_BL.gif");
$l  = imagecreatefromgif("images/shadow_L.gif");
 
 
//We now have stored all images that we are going to use. 
//Next we have to create one more (the canvas) to put them together. 
 
//CREATING THE CANVAS 
/*
Remember, that the canvas has to be larger than the image, otherwise the image will hide the shadow images.
In this case the width of the canvas should be the width of the image plus twice the width of one of the side shadow image. 
 
In other words: Width of Canvas = Width of Image + 2(Width of Side Image)
 
Read that again, it's easier than it sounds. 
*/
 
//First we find the dimensions of the side images
$w = imagesx($l); 	//Width of the left shadow image
$h = imagesy($l);	//Height of the left shadow image
 
/*
Next we get our new heights and widths for the canvas. This creates an image that is slightly larger than 
your image.
*/
$canvasHeight = $height + (2*$w); 
$canvasWidth  = $width + (2*$w);
 
//Third we create a blank canvas with these new dimensions
$canvas = imagecreatetruecolor($canvasWidth, $canvasHeight); 
 
 
// ----------------------------------------
// Putting your images together
// ----------------------------------------
 
/*
We have our graphic image, shadow images and canvas ready. Now it's time to paint! 
In the first section we're adding the top, left, bottom and then right sections. 
*/
 
imagecopyresized($canvas, $t,0,0,0,0,$canvasWidth,$w,$h,$w);			
imagecopyresized($canvas, $l,0,0,0,0,$w,$canvasHeight,$w,$h);
imagecopyresized($canvas, $b,0,$canvasHeight-$w,0,0,$canvasWidth,$w,$h, $w); 
imagecopyresized($canvas, $r,$canvasWidth-$w,0,0,0,$w,$canvasHeight,$w,$h);
 
 
//here I'm doing the same thing again, only this time I'm setting $w and $h to be the width and heights of the corners. 
$w = imagesx($tl); 
$h = imagesy($tl); 
imagecopyresized($canvas, $tl,0,0,0,0,$w,$h,$w,$h);  
imagecopyresized($canvas, $bl,0,$canvasHeight-$h,0,0,$w,$h,$w,$h); 
imagecopyresized($canvas, $br,$canvasWidth-$w,$canvasHeight-$h,0,0,$w,$h,$w,$h);
imagecopyresized($canvas, $tr,$canvasWidth-$w,0,0,0,$w,$h,$w, $h);  
 
/*
At this point the canvas has all the drop shadow images attached, all we need to do now is attach the image and we're done! 
I changed $w back to the size of the perimeter in order to properly place the image.
*/	
$w = imagesx($l); 
imagecopyresampled($canvas, $image, $w,$w,0,0,  imagesx($image), imagesy($image), imagesx($image),imagesy($image));
 
//Notice that I used imagecopyresampled, the reason for this is I get a much higher quality image than with imagecopy
//resized. However, I sacrifice speed for this quality. 
 
 
// ----------------------------------------
// OUTPUTTING THE IMAGE
// ----------------------------------------
header("content-type: image/gif"); 	//This sets the header type (what kind of file is this?) quite literally this php file turns into a gif. 
ImageGif($canvas); 
ImageDestroy($canvas);
 
A lot of the functionality of the code is explained in the comments but I'll spare a few minutes and summarize what it does.
There are three parts to this code.
  1. Image Storage and preparation
  2. Image manipulation
  3. Image Output (the easiest part)
Image Storage
We store the images so that we have access to them in the GD Library. What it is actually doing is creating a copy of the image and storing them into memory.
In this instance we also create a blank canvas by using the function createimagetruecolor. It is like making a new canvas in photoshop. From here the possibilities are almost limitless.

Image manipulation
Like photoshop, the power of manipulation is where the real strength lies. In this instance we are using the imagecopyresized function to place the pictures onto each other (as illustrated in the picture above). The behavior is very similar to that of Photoshop. Click here for more information about this powerful function.

Image Output
This is the most straightforward part of the script. We simply set what kind of file this, ouput the file and then destroy the file and free up resources.

Result:

And here is the final result of all our hard work!

Conclusion


I hope you found this tutorial useful. If you have any questions or suggestions please feel free to contact me.

Post a comment

your name:
your email: (optional)
write your email to be added to the mail list.
your comment: