Article

Image Manipulation with PHP - The GD Libraries

Page: 1 2 3

Further Manipulations

In the prior script, there was an area commented out as /* image manipulation scripting */, which was empty. If we wanted to try a few weird and wonderful effects on our thumbnails, we could add code here, integrating all the variables we defined or deduced earlier.

An example idea might be to add a shadowed bevel to the thumbnail, perhaps with light colour shading on the top and left, and dark colour shading on the right and base. To give you an idea of the techniques involved, I'll take the shadowed bevel idea and step through it slowly.

Shadowed Bevel

A useful function in GD is ImageCopyMerge, as it allows us to merge a part of one image onto our thumbnail. It is especially useful because we can also define an opacity for the merged portion (which to you and me means shading). If we use a short for loop to count how far we are from each edge of the thumbnail, we can also use that incremental number to work out the opacity with which we'll draw our dark and light lines.

// create a dark image and a light image  
 
$dark_shadey = ImageCreate($maxdim,$maxdim);  
$nadir = ImageColorAllocate($dark_shadey,0,0,0);  
$light_shadey = ImageCreate($maxdim,$maxdim);  
$nadir = ImageColorAllocate($light_shadey,255,255,255);  
 
// decide how wide we want our edge shading  
 
$edge_width = 10;  
for($edge_pixel = 0; $edge_pixel < $edge_width; $edge_pixel++)  
{  
 
// work out the opacity relative to how far from the edge we are  
 
$opacity =  100 - (($edge_pixel+1) * (100 / $edge_width));  
 
// merge a bit of the light image along the top and left side  
// merge a bit of the dark image along the base and right side  
 
ImageCopyMerge($thumb,$light_shadey,$to_x+($edge_pixel-1),  
$to_y+($edge_pixel-1),0,0,1,$to_h-(2*$edge_pixel),$opacity);  
ImageCopyMerge($thumb,$light_shadey,$to_x+($edge_pixel-1),  
$to_y+($edge_pixel-1),0,0,$to_w-(2*$edge_pixel),1,$opacity);  
ImageCopyMerge($thumb,$dark_shadey,$to_x+($to_w-($edge_pixel+1)),  
$to_y+$edge_pixel,0,0,1,$to_h-(2*$edge_pixel),$opacity-20);  
ImageCopyMerge($thumb,$dark_shadey,$to_x+$edge_pixel,$to_y+  
($to_h-($edge_pixel+1)),0,0,$to_w-(2*$edge_pixel),1,$opacity-20);  
}  
 
// destroy the two new images that we used  
 
ImageDestroy($dark_shadey);  
ImageDestroy($light_shadey);

You might notice I've used a few weird syntaxes here, such as reducing the dark image opacity by 20 in the ImageCopyMerge calls. This is simply because the resultant image looks better if the dark isn't quite as dark as the light is light. If you decide to code your own manipulations, you will (more than likely) have to add a few workaround syntaxes like this to your own scripts.

Just to keep you going, here are a couple more manipulations that can be cut and pasted into the /* image manipulation scripting */ area of the main tutorial script.

Spider's Web

This small plugin scriptlets produces a spider's web effect, which is drawn over the thumbnail in the colour defined as $zenith. This script simply draws a few lines from the lower left corner at various angles, and then draws elliptical arcs centred at the same corner.

$zenith = ImageColorAllocate($thumb,255,255,255);  
for($draw = 0;$draw<$to_h;$draw+=12)  
{  
ImageLine($thumb,$to_x,($to_h+$to_y),($to_w+$to_x),  
(($to_h-$draw)+$to_y),$zenith);  
}  
for($draw = 0;$draw<$to_w;$draw+=12)  
{  
ImageLine($thumb,$to_x,($to_h+$to_y),($draw+$to_x),  
$to_y,$zenith);  
}  
for($draw = 1;$draw<14;$draw++)  
{  
ImageArc($thumb,$to_x,($to_h+$to_y),$draw*($to_w/4),$draw*  
($to_h/4),270,0,$zenith);  
}

Doughnut

This larger plugin scriptlet produces an elliptical shaded doughnut thumbnail. The background and shading colour is defined by $zenith. This manipualtion works by iterating through the thumbnail one pixel at a time, and deciding exactly how much opacity to apply when merging a small dot at that place.

$dot = ImageCreate(1,1);  
$zenith = ImageColorAllocate($dot,255,255,255);  
 
for($ypos=0;$ypos<$to_h;$ypos++)  
{  
for($xpos=0;$xpos<$to_w;$xpos++)  
{  
$xdist = abs(($to_w/2)-$xpos);  
if($xdist==0) {$xdist = 0.01;}  
$ydist = abs(($to_h/2)-$ypos);  
$dist = sqrt(pow($xdist,2)+pow($ydist,2));  
$angl = atan($ydist/$xdist);  
$el_dist =    
sqrt(pow(abs(cos($angl)*$to_w/2),2)+pow(abs(sin($angl)*$to_h/2),2));  
if($dist>$el_dist || $dist<$el_dist/6)  
{  
ImageCopyMerge($thumb,$dot,$xpos+$to_x,$ypos+$to_y,0,0,1,1,100);  
}  
else {  
$dnut_dist = ($el_dist/12)*5;  
$offset_dist = abs((($el_dist/12)*7)-$dist);  
$uppy = sin(acos($offset_dist/$dnut_dist))*$dnut_dist;  
$opac = 100-((100/$dnut_dist)*$uppy);  
ImageCopyMerge($thumb,$dot,$xpos+$to_x,$ypos+$to_y,0,0,1,1,$opac);  
}  
}  
}  
ImageDestroy($dot);

I hope that tutorial and the extra little scriptlets have taught you how versatile the GD libraries can be. Perhaps you have even caught the image-manipualtion bug and are eager to start coding your own transformations! Good luck.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: