h1>PIM Team Case Study: Creating Text Effects With PHP and GD See how you can create graphic effects on text with PHP and GD - drop shadows, arcs, fonts and colors.
Problem A-tec Signs and Sraphics Inc. launched a web site with the idea to sell decals online. To achieve better customers ineterest the website had to integrate online decal builder. The company is offering also decals for vehicles which brought some specific requirements to the builder like having the decal text turning arround 4 types of arcs.
Goals - Provide users with preview area - Allow visitors
to choose font and color - Allow adding drop shadow and
selecting drop shadow color - Allow turning the text into arcs -
Real Time calculating Solution Because of the need for
increasing customers interest we had to think about not for
perfect math formulas when showing the graphs in the preview
area, but for the people who will look at them. As we will
reaveal below, there were few problems going arround human
appreceptions for something 'perfectly smooth' and the
matchematical perfect figures. Methodology We were going
to extensively use PHP GD library for the text effects. It
provided easy changing of fonts and colors, adding drop shawdows
and rotating the texts. We had also to create color palletes
which to appear when user click and disappear when color is
selected (You can personally try the decals creating here). Using hidden layers and javascript was
supposed to do the work. The main problem in this site was to
create 4 types of arcs so when the user selects one of them the
text is created arround imaginary arc (like in the vector
graphical softwares). We were going to study Bezie's formulas
and create these arcs with its help. Implementation PIM Team Bulgaria had
the task to build the full functional online decals builder with
the following features: - Decal background Some users
were supposed to have their decals placed on colored background.
We had to allow the preview area to be painted in a selected
background. First we created the image in temp folder: //
the name of destination image $dest='decals/'.time().'.jpg';
//the background imagefilledrectangle ( $im, 0, 0, 590, 60,
$colors[$_POST['bcolors']]); $colors array contains the
available color which are stored by the administrator in the
database. Thus, when the visitor selects a background it is
passed as parametter to imagefilledrectangle function. - Font
selection Users should be able to select fonts for their
future decals. Knowing that we can't consider all the fonts will
be available on all visitor's computers we had to upload them on
the web server directory. We allowed the admin to manage the
fonts, adding their names and uploading files in admin area. The
fonts in the select box came from the database. Selected font
was passed in the call to imagettftext funtion which is drawing
on the previously created image. - Color Selections The
color selections had to be a palettes which appear when user
clicks and disappear when color is selected. The palette had to
look as a table with colors and these colors are also defined in
the admin area so they had to come dynamicly. We had to seed a
static javascript function with dynamic content. We created a
PHP cycle which was taking the colors from the database and then
creating a string for HTML table. This table is then passed to a
javascript function which creates the palletes with the help of
hidden layers: function showTable(table) { mouseX =
window.event.x + document.body.scrollLeft+25;
if(table=='background') { var content="=$table?>"; var y=460; }
if(table=='fonts') { var content="=$ftable?>"; var y=690; }
if(table=='shadows') { var content="=$stable?>"; var y=810; }
document.getElementById('tabler').style.pixelLeft=mouseX;
document.getElementById('tabler').style.pixelTop=y;
document.getElementById('tabler').style.visibility='visible';
document.getElementById('tabler').innerHTML=content; }
Of course, once the user select the desired color we had to hide
the pallette: function setColor(elid,color,fromid,shc) {
document.getElementById(elid).value=color; document.getElementById('tabler').style.visibility='hidden'; }
Thus we created nice palettes which appear and disappear on a single click and don't take much space on the screen.
- Drop Shawdows The decals offered has the ability to
have a drop shadow added so we had to add this option to the
online builder. PHP however didn't offered a nice function for
that. We created a procedure which draws the texts twice - once
the original 100% saturated text and once the shadow with a
percentage of the color and appropriate displacement. Of course
the shadow was drawn on the image before the main text.
@imagettftext($img, 20, $gr[$i], $x+$dx, $ys[$i]+$dy,
$scolors[$shadowcolor], "fonts/".$_POST['fonts'],$word[$i]);
- Arcs The main problem came when we had to 'rotate' the
texts thru arcs. First we created perfect Bezie funtion which to
draw the curves and adjust the letter above them. But what a
surprise - the curves looked perfect alone, but when we adjusted
the letters above them they seemed rough. After studying this
problem we realised that the rough screen resolution and the
disability to antialise the images wouldn't allow us to create
nice arcs. We were standing against insoluble problem. We
decided to create few arcs with a graphical software (CorelDraw)
and to see what could be wrong. We noticed that Corel's curves
were looking great after they are manually adjusted. However you
can't just leave the program to create perfect curves
automaticly. A human eye was needed to judge when a curve looks
right and when not. We got a totally different direction. There
wasn't an universal function to help us. The solution we found
was to 'manually' adjust each letter. We created a procedure
with cases which were adjusting each letter on the appropriate
place and with appropriate rotation depending on how long was
the text. It worked! We created 2 arrays for each arc type - one
array with the positions and one array with the rotations. The
rest was simple: if($arctype) { $start=(35-$l)/2;
if($start%2) $start+=1; $gr=array_slice($gr,$start,$l);
$ys=array_slice($ys,$start,$l); }
if(!$arctype) { $ys=array(); $gr=array(); //making the arrays for($i=0;$i<$l;$i++) { array_push($ys,50); array_push($gr,0); } }
You can go on the atec's site and try the arcs we achieved (http://atecsigns.com/decal/step_1.php). Results Now A-tec Sings's web builder creates perfect decals with graphs, calculates the price and allows you to add the decals to your shopping cart and chgeckout (the shopping cart software is also created by PIM Team Bulgaria). The builder allows the visitor to create the desired decals with any color, dropped shadow, background and shape, to preview it and to calculate the cost for different sizes and quantities. The website and builder were promoted with massive radio advertising company. At that time it was the only decal builder which allowed creating texts arround arcs. Conclusions - Use GD to create text effects - Do not forget that you can create you own functions for what GD does not offer - Do not always search for math perfect formulas. The graphical effects are intended to the human eye - Load fonts in the server - Use javascript and hidden layers to achieve great flexibility
About the author: Bobby Handzhiev is senior developer in PIM Team Bulgaria