Archive for the ‘Tutorial’ Category

Getting to know GD

4. January 2010

1 Comment »

Create dynamic userbars with GD

Introduction:
Have you ever been on aim and received a message from one of your friends with a big long URL followed by something like “John%20Is%20Stupid.” You know what ever it is, wont be worth your time, but you click on it anyway to find someone goofy looking holding a sign and on it printed in big bold letters “John Is Stupid.” Ever wonder how they work? Chances are, the script takes advantage of the GD functions. In this tutorial you will learn many GD functions that you can use to generate images. The goal for this tutorial is for you to generate a dynamic user bar.

Here is a list of the functions used in this tutorial:

  1. header()
  2. imagecolorallocate()
  3. imagettfbbox()
  4. imagesx()
  5. abs()
  6. imagettftext()
  7. imagepng()
  8. imagedestroy()

The first thing you want to do is create the header type using the header function. Next you use the GD tools to create an image from a png file. In my case, I have userbar.png in the same directory as this script is in. The function imagecolorallocate creates a color using RGB (red-green-blue) format. The next three lines just set some basic information which isn’t that hard to understand. The last three lines to must of the work creating the image. The most important function here to pay attention to is imagettftext. The imagettftext requires 8 arguments which are the resource image, font size, the angle, x location, y location, font color, the actual font, and the string to be printed.

<?php
    header('Content-type: image/png');
    $im = imagecreatefrompng ("userbar.png");
    $color = imagecolorallocate($im, 0, 0, 0);
    $text = "test";
    $font = 'font.ttf';
    $size = 8;
    imagettftext($im, $size, 0, 15, 12, $color, $font, $text);
    imagepng($im);
    imagedestroy($im);
?>

Now that we have GD generating an image, lets make it a little more complex. We’ve added the

$text = $_GET['text'];

line to allow the user to enter a random string, and we have also added the

$size = imagettfbbox($fontsize, 0, $font, $text);
$dx = (imagesx($im)) - (abs($size[2]-$size[0])) - 20;

Which calculate where to start printing the string on the image. The function imagettfbbox generates the length of the font string in pixels. If you were to use something like strlen($text) that would just return the number of characters in the string, which isn’t very usefull in this case. Next, we use the imagesx() function to determine the width of the image, we take that value and subtract the string length in pixels and subtract an extra 20 pixels to leave some room toward the end of the image.

<?php
    header('Content-type: image/png');
    $text = $_GET['text'];
    $im = imagecreatefrompng ("userbar.png");
    $color = imagecolorallocate($im, 0, 0, 0);
    $font = 'font.ttf';
    $fontsize = 6;
    $size = imagettfbbox($fontsize, 0, $font, $text); //calculate the pixel of the string
    $dx = (imagesx($im)) - (abs($size[2]-$size[0])) - 20; //calculate the location to start the text
    imagettftext($im, $fontsize, 0, $dx, 13, $color, $font, $text);
    imagepng($im);
    imagedestroy($im);
?>

At this point you could call yourself done. However you could add some extra features such as make the font color vary with the user input. Adding a simple if statement will accomplish this.

<?php
    header('Content-type: image/png');
    $text = $_GET['text'];

    $im = imagecreatefrompng ("userbar.png");

    if($text == "ADMINISTRATOR"){ //if administrator
        $color = imagecolorallocate($im, 255, 0, 0); //red
    }
    else if($text == "MODERATOR"){ //if moderator
        $color = imagecolorallocate($im, 0, 0, 255); //blue
    }
    else if($text == "JUNKIE"){ //if junkie
        $color = imagecolorallocate($im, 0, 0, 0); //black
    }
    else { //something else...
        $color = imagecolorallocate($im, 0, 128, 0); //green
    }

    $font = 'font.ttf'; //font file
    $fontsize = 6; //font size
    $stringsize = imagettfbbox($fontsize, 0, $font, $text); //calculate the pixel of the string
    $dx = (imagesx($im)) - (abs($stringsize[2]-$stringsize[0])) - 20; //calculate the location to start the text
    imagettftext($im, $fontsize, 0, $dx, 13, $color, $font, $text);
    imagepng($im);
    imagedestroy($im);
?>

Finally you can save this script as generator.php and display it in forums by using the html tags.

<img src="http://www.yourdomain.com/generator.php?text=JUNKIE" />

NAND and NOR

13. August 2009

No Comments »

In addition to the logical operations defined here – computer scientists have defined two other operations (which are merely abstractions of operations defined in my previous article) since they are used so often. They are NAND and NOR, symbolically ↑ (|) and ↓ (⊥). I say they are abstractions because they are simply the negation of AND and OR operations

NAND

A B A AND B NOT (A AND B) A NAND B
1 1 1 0 0
1 0 0 1 1
0 1 0 1 1
0 0 0 1 1

NOR

A B A OR B NOT (A OR B) A NOR B
1 1 1 0 0
1 0 1 0 0
0 1 1 0 0
0 0 0 1 1

Basic Logic

12. August 2009

1 Comment »

The modern computer is perhaps one of the most complex and perplexing yet ever so simple creations known. The entire personal computer only knows of two numbers – zero and one. Everything you see or do is simply an abstraction. These ones and zeros are called binary and are used to perform logical operations. The six logic operations, conjunction, disjunction, implication, equality, and exclusive disjunction, are outlined below. Their symbols are ¬ (~), ∧ (&, .), ∨ (||, +), →, ≡, and ⊕ respectively.

Negation

A NOT A
1 0
0 1

Conjunction

A B A AND B
1 1 1
1 0 0
0 1 0
0 0 0

Disjunction

A B A OR B
1 1 1
1 0 1
0 1 1
0 0 0

Implication

A B A IMPLIES B
1 1 1
1 0 0
0 1 1
0 0 1

Equality

A B A EQUALS B
1 1 1
1 0 0
0 1 0
0 0 1

Exclusive Disjunction

A B A XOR B
1 1 0
1 0 1
0 1 1
0 0 0

Using these six operations, you can do everything a computer can do. Don’t believe me? Be sure to read my follow up articles.

Image mouseover effects – The right way (using CSS Sprites)

10. May 2009

No Comments »

Websites are growing more complex every day. Image quality is increasing, and users are required to download thousands of lines of JavaScript and style sheets. Sure this increases the beauty of a website, but it comes at the expense of users who do not have broadband access. This beauty increases the page size to potentially a few megabytes, and dial-up or ISDN users are simply unable to browse these websites. One technique to reduce the page load time is to minimize the amount of requests a page makes to the server. Thus we will employ the use of sprites.

What is a sprite?

[A sprite] is a two-dimensional/three-dimensional image or animation that is integrated into a larger scene.

Sprites were originally invented as a method of quickly compositing several images together in two-dimensional video games using special hardware. As computer performance improved, this optimization became unnecessary and the term evolved to refer specifically to the two dimensional images themselves that were integrated into a scene. That is, figures generated by either custom hardware or by software alone were all referred to as sprites. As three-dimensional graphics became more prevalent, the term was used to describe a technique whereby flat images are seamlessly integrated into complicated three-dimensional scenes.

How does this apply to websites?
It is quite simple. Rather than requesting several images and displaying them, create a single composite image composed of each individual image and request that one image. I will show you an example of how I employ this technique on my website.

Mouse over effects are quite common common. A few years ago this effect was achieved using onMouseOver/onMouseOut JavaScript. Today we use CSS. However many people still use individual images for each link. For example a navigation with four links might require eight images. Four for the links and another four for the hover effect. That is eight requests to the server! And you can do it in ONE!

First open your navigation bar in Photoshop
1

Then add the text for your links
2

Next duplicate this group (once for each effect). I intend on having two effects, a hover effect and an active effect, so I am going to duplicate the group twice.
3
Now increase your canvass size so all three groups can fit.
3

Modify each group to reflect your desired effect. I am only going to change the text color.
5

Save your image. Now we get to the coding. Currently we have:


<div class="navigation"></div>

with the associated style:

.navigation {
background: url('images/navigation_bg.gif');
height: 39px;
}

We are going to insert four links into our HTML.


<div class="navigation"><a class="btn_home" href="/"></a>
<a class="btn_down" href="/downloads"></a>
<a class="btn_tuts" href="/tutorials"></a>
<a class="btn_snip" href="/snippets"></a></div>

Now we are going to style the links.

a.btn_home, a.btn_down, a.btn_tuts, a.btn_snip {
width: 130px;
height: 39px;
display: table-cell;
background: url('images/navigation.gif') no-repeat;
}

This is our result:
6

Four links labeled “Home” was not our intention. However, we can adjust the background position to show the desired link.

a.btn_down {
background-position: -130px 0px;
}

a.btn_tuts {
background-position: -260px 0px;
}

a.btn_snip {
background-position: -390px 0px;
}

Now all four buttons have the correct label.
7
Now all we need to do is adjust the background position for the hover property.

a.btn_home:hover {
background-position: 0px -39px;
}

a.btn_down:hover {
background-position: -130px -39px;
}

a.btn_tuts:hover {
background-position: -260px -39px;
}

a.btn_snip:hover {
background-position: -390px -39px;
}

Now you are done! You have a fully functional navigation with hover effects. Moverover, you used a single image and thus a single request to the server.

Note that the two background-position parameters are the x location and the y location of where you want the background to start. Keep in mind, when designing your hover effects, it will make life a lot easier if each button is a uniform size.

I have further optimized the navigation by reducing the size of navigation.gif.
9
Who uses sprites?
To name a few: Google, Apple, Facebook, and Digg.

Note: The code provided will only work in Firefox due to Internet Explorers failure to support the table-cell display property.