Tech Notes

Text to Image in PHP with GD

This is simple script to demonstrate the capabilities of PHP with the GD library, which provides a lot of image functions that can be useful in many applications. GD provides a rich set of functions. For a complete list of these functions, check the PHP manual.

header('Content-Type: image/png');
// Text to be converted to image
$text = 'Hello World';
// Font to use, give accessible path from script
$font = './arial.ttf';

// Convert HTML entities into ISO-8859-1
$text = html_entity_decode($text,ENT_NOQUOTES, "ISO-8859-1");

// Create the image
$im = imagecreatetruecolor(160, 160);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

// Create some colors
imagefilledrectangle($im, 0, 0, 160, 80, $white);

// Add the text
imagettftext($im, 12, 0, 20, 20, $black, $font, $out);


GD can also be used to create and manipulate image files in a variety of different image formats, including GIF, PNG, JPEG, WBMP, and XPM.


  • Though GD supports Unicode text inputs, it doesn’t support complex text rendering
  • Complex Text Rendering is required for properly displaying many language texts, such as the Arabic alphabet and scripts of the Brahmic family, which includes Tamil and many other Indic scripts.

There is a alternative for this using Pango and Cairo in PHP. I’ll post a detailed update on that in my next post.

Tech Notes

Activate Oracle on XAMPP for Windows

With the default installation of XAMPP for Windows, we don’t get PHP Oracle connectivity enabled. This can be enabled easily when you need to connect to a Oracle Database from your PHP application/script. PHP has got the OCI8 extension, which provides Oracle connectivity to PHP application, and OCI8 uses Oracle Instant Client Package to get Oracle specific functions.

I had the need to connect to a Oracle Database from a PHP script in one of my recent projects, the following is what I did to enable Oracle connectivity in XAMPP for Windows.

  1. In your XAMPP Start Page, go to phpinfo, look for string oci8. If string found it indicate that connection to oracle is available, otherwise to activate connection do the following steps:
  2. Open the currently used php.ini file by looking at the phpinfo, from the XAMPP folder.
  3. Find string ;extension=php_oci8.dll. Remove the semicolon (;) ahead of the string to activate the oracle extension.
  4. Save the php.ini file.
  5. Download the “Instant Client Package – Basic” for Windows from the OTN Instant Client page. Unzip it to c:\instantclient_11_1
  6. Edit the PATH environment setting and add c:\instantclient_11_1 before any other Oracle directories. For example, on Windows XP, follow Start -> Control Panel -> System -> Advanced -> Environment Variables and edit PATH in the System variables list.
  7. Set desired Oracle globalization language environment variables such as NLS_LANG. If nothing is set, a default local environment will be assumed. See An Overview on Globalizing Oracle PHP Applications for more details.
  8. Unset Oracle variables such as ORACLE_HOME and ORACLE_SID, which are unnecessary with Instant Client (if they are set previously).
  9. Restart XAMPP (or Start if its not already started).
  10. To make sure that connection to oracle database has successfully activated, go to phpinfo. Find string: oci8. If found, then XAMPP can now communicate with Oracle Database.

The steps to do the same in a Linux box are almost similar, except there you will use the Linux versions of the packages and setting PATH variables would be different.

You can ping me back with a comment if you run into any issues, I might be able to help you or I can learn from you.


Tech Notes

Free calls to US using Google Voice & Gizmo (with IPKall or a Friend in US)

[Update: Gizmo5 has been acquired by Google and so new user sign-up has been suspended. Use to get a SIP number instead of Gizmo5 and Express Talk instead of Gizmo5 client. Gmail can also be used as a client for Google Voice. An updated guide can be found at Digital Inspiration, titled Get Google Voice from Outside the U.S.]

So how did I get my Google Voice number from outside US… Yeah I live in Sri Lanka, and I have registered my Google Voice number and use it without any restrictions…!

Google Voice is a wonderful service that functions like a virtual phone switchboard, and you can manage calls and route them to your various phones, voice mail, etc. It gives you a free phone number that can receive regular phone calls and route them to any other actual phones you have connected to your account.

Unfortunately, currently its forwarding abilities are limited only to US phones. Also you are not allowed to access service registration when you are outside US. But I was able to do some workaround with the help of couple of online guides to create my own Google Voice number.

How to signup to Google Voice?
Currently its only by ‘invite mode’ but you can get an invitation within a week or so. During the signup process you will be required to verify your account via a US telephone number.

How to access the Google Voice website for signup from outside US? (After you are signed-up you are allowed to access even from outside US)

  1. Get a friend of your’s in US to do this.
  2. Use a SOCKS Tunnel to access. (I won’t suggest this as its risky, also web based proxies won’t help)

How to get a US number and verify it when you are outside US?

  1. Get a friend of your’s in US to add his home number and verify it for you.
  2. Buy a US number as a CallIn number on Gizmo, or any other similar providers, (priced around 10 – 20 $). (Callcentric, Voxalot)
  3. Get a free US number from IPKall (forwarded to Gizmo or any other VoIP service) that can be added to Google Voice signup.

How to connect this with Gizmo?

  • Register for a free Gizmo account and get your Gizmo number (which is a SIP number, that can be used on any SIP client, on PC, on mobiles, on ATA devices)
  • Add your Gizmo number in your Google Voice settings page.
  • Add your Google Voice number in your My Gizmo page.

What I have got now?

  • I have a Gizmo account I can add to any SIP enabled device.
  • I have a IPKall number which is also a US phone number forwarded to my Gizmo account, but I’m not going to use that at all.
  • I have a Google Voice number (thats what we wanted to have…!).
  • Any one can call my US number 540-IMN-IMAL (540-466-4625), and it will ring in my Gizmo phones, SIP applications or any other device. Its free for me.
  • I can call any US (& Canada?) numbers for free, (at least for 3 minutes).

Thats how I got my nice Google Voice number. I’m ready to help you if you want to get your own Google Voice number, just leave a comment.

Further Reading:

  1. Gizmo Voice – A mashup using Gizmo5 and Google Voice
  2. Adding a Gizmo number to Google Voice
  3. Taking calls from Gizmo
  4. Google Voice from Wikipedia
Tech Notes

Using the ‘exclude’ option with ‘tar’

I’m always confused with the use of ‘exclude’ options with ‘tar’. Here I’m sharing a few tips I found online, just in case it might help you. The meanings of –exclude and –exclude-from are always confusing:

  • Use –exclude when files to be excluded are given as a pattern on the command line.
  • Use –exclude-from to introduce the name of a file which contains a list of patterns, one per line; each of these patterns can exclude zero, one, or many files.

When using –exclude=pattern, be sure to quote the pattern parameter, so GNU tar sees wildcard characters like ‘*’.

For example, write:

$ tar -c -f archive.tar --exclude '*.o' directory

tar does not act on a path name explicitly listed on the command line if one of its file name components is excluded. If files that end with ‘*.o’ are excluded when creating an archive, but explicitly name the file ‘dir.o/foo’ after all the options have been listed, ‘dir.o/foo’ will be excluded from the archive.
Only shell syntax, or globbing will work with exclude options in tar. The command might fail if regexp syntax is used to describe files to be excluded in the command.


Tech Notes

Being Nice on a Linux Box – Process priority with ‘nice’ & ‘renice’

Problem (I had): Wanted to run a VirtualBox on a RHEL 4.7 Linux server which is shared by someothers. But as some other processes took more CPU, the VirtualBox performance was bad. So I wanted to increase the priority given to VirtualBox.

Solution (I found): VirtualBox can be given higher priority using nice command. Also renice command can be used to change the priority of a running process, which will also be useful.

nice -10 VBoxHeadless

Gives the VBoxHeadless process 1.5 times priority than the normal process, calculated as (20 – -10)/20 = 1.5

nice -20 make

Executes make at maximum priority.

renice +20 2222

Changes the priority of process 2222 to +20 (minimum priority).

renice -10 -p 13013

Changes the priority of a running process by specifying its process ID, where priority can be,

  • 1 to 20 : Runs the specified processes slower than the base priority.
  • 0 : Sets priority of the specified processes to the base scheduling priority.
  • -20 to -1 : Runs the specified processes quicker than the base priority.

References and further reading: