Archive for the ‘PHP’ Category

Getting to know GD

4. January 2010

2 Comments »

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

HTML with Swing Components

1. August 2009

1 Comment »

One useful feature of Swing GUI’s many people overlook is the ability to use simple HTML tags within swing components. This tutorial assumes you know how to create a GUI in swing and add components. If you do not know how to do this, it is a good idea to read one of my previous tutorials.

Lets first create a basic GUI with a JButton and JLabel:

package tutorials;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class HTMLJButton {

	public HTMLJButton(){
		JFrame mainFrame = new JFrame("HTML");
		mainFrame.setLayout(new java.awt.FlowLayout());

		JButton button1 = new JButton("JButton Text");
		JLabel label1 = new JLabel("JLabel Text");

		mainFrame.add(button1);
		mainFrame.add(label1);

		mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
		mainFrame.setVisible(true);
		mainFrame.pack();
	}

	public static void main(String[] args){
		new HTMLJButton();
	}
}

But what if you want the text on the button to be a different color? You can use the HTML font tags inside the JButton string parameter.

Such as:

JLabel label1 = new JLabel("<html><font color='#FF0000'>Red Text</font>" +
		"<br /><font color='#00FF00'>Blue Text</font></html>");

Not only can you modify fonts, you can also insert images:

JLabel label1 = new JLabel("<html><img src='http://www.google.com/intl/en_ALL/images/logo.gif'></img></html>");

There is much more you can do with HTML, just note, when you do insert HTML into a componnents string paramater, you need to surround the HTML with the opening and closing HTML tags and you need to use single quotes since the entire string is bound by double quotes.

package tutorials;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class HTMLJButton {

	public HTMLJButton(){
		JFrame mainFrame = new JFrame("HTML");
		mainFrame.setLayout(new java.awt.FlowLayout());

		JButton button1 = new JButton("<html><font color='#FF0000'>JButton Text</font></html>");
		JLabel label1 = new JLabel("<html><font color='#FF0000'>Red Text</font>" +
				"<br /><font color='#00FF00'>Blue Text</font>" +
				"<br /><font color='#0000FF'>Green Text</font>" +
				"<br /><font color='#000000'>Black Text</font>" +
				"<br /><font color='#FFFFFF'>Blue Text</font></html>");

		mainFrame.add(button1);
		mainFrame.add(label1);

		mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
		mainFrame.setVisible(true);
		mainFrame.pack();
	}

	public static void main(String[] args){
		new HTMLJButton();
	}
}