Monthly Archives: October 2008

Currency Calculation in PHP

Thanks to some help from Stack Overflow, the new and excellent Q&A site for programmers, I have put together a PHP class which can perform a currency conversion using up to date exchange rates pulled live from Yahoo. Here is the code for y’all to share:

<?php

// fx.php - PHP Code to convert currencies using Yahoo's currency conversion service.
// by Adam Pierce <adam@doctort.org> 22-Oct-2008
// This code is public domain.

class ForeignExchange
{
	private $fxRate;
	
	public function __construct($currencyBase, $currencyForeign)
	{
		$url = 'http://download.finance.yahoo.com/d/quotes.csv?s='
			.$currencyBase .$currencyForeign .'=X&f=l1';

		$c = curl_init($url);
		curl_setopt($c, CURLOPT_HEADER, 0);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
		$this->fxRate = doubleval(curl_exec($c));
		curl_close($c);
	}

	public function toBase($amount)
	{
		if($this->fxRate == 0)
			return 0;
			
		return  $amount / $this->fxRate;
	}
	
	public function toForeign($amount)
	{
		if($this->fxRate == 0)
			return 0;
			
		return $amount * $this->fxRate;
	}
};

?>

Because it creates an object, that object will remember the exchange rate so it doesn’t need to look up the rate again and again if you want to do multiple currency conversions on the same web page.

Usage Example:

<?php

// Create an object to convert Australian Dollars to Euros.
    require 'fx.php';
    $fx = new ForeignExchange('AUD', 'EUR');

// This function formats a value with 2 decimal places.
    function fmtMoney($amount)
    {
        return sprintf('.%.2f', $amount);
    }

    $auPrice = 25.50;

    echo '<p>Your price is AU$'. fmtMoney($auPrice)
        .' which is approximately &euro;'. fmtMoney($fx->toForeign($auPrice)) .'</p>';
?>

…enjoy.

Elastic Tabstops by Nick Gravgaard

I’ve just been playing with the Elastic Tabstops plugin by Nick Gravgaard. I really like it. I like my code to look neat so when I write a block of code like this:

   $custFirstName = $_REQUEST['cust_first_name'];
   $custLastName = $_REQUEST['cust_last_name'];
   $custEmail = $_REQUEST['cust_email'];
   $custAddr1 = $_REQUEST['cust_addr_1'];
   $custAddr2 = $_REQUEST['cust_addr_2'];

I always try to neaten it up by adding spaces like so:

   $custFirstName = $_REQUEST['cust_first_name'];
   $custLastName  = $_REQUEST['cust_last_name'];
   $custEmail     = $_REQUEST['cust_email'];
   $custAddr1     = $_REQUEST['cust_addr_1'];
   $custAddr2     = $_REQUEST['cust_addr_2'];

Which is a little fiddly but I always do it because like I said, I like my code to look neat.

But now I have a tool which does it automatically. I’m very happy.

It will even handle proportional fonts so I’ve been experimenting with the decadent pleasure of coding in more beautiful type faces and still having everything line up.

Unfortunately it is only for gedit at this time (which is a fine text editor but just a text editor nevertheless). If this were available in Eclipse or MS Dev Studio, I’d be in heaven!

Windows vs Linux when Changing Motherboards

My new motherboardWell, here’s an interesting little tidbit. My home box is dual boot with Windows XP SP2 and Linux Ubuntu 8.04. My motherboard has been playing up recently so I got myself a new one, a Gigabyte GA-G31M-S2L. This is a budget board with a pretty plain-vanilla collection of peripherals.

After installing the board I chose to boot up Ubuntu first. I thought I’d probably need to install some drivers for the onboard audio, video and Gigabit Ethernet but no, it just worked! I was amazed. It would seem the days are gone when getting new hardware to work under Linux was a massive chore. I was so stunned I had to write a blog post about it. Even 3D acceleration worked immediately, no mess, no fuss.

Then I tried booting into Windows. Ha ha haha ha. Did it work straight up. Did it hell! I had to spend an hour mucking about with loading drivers and rebooting a million times.

Nowdays we have many cross-platform applications such as Open Office, GIMP, Firefox and so on. Add to this the increasing stability and simplicity of the Gnome desktop, Ubuntu is very much a serious competitor for Windows, not just some time in the future but right now.

Centering and Scaling an Image on a Web Page

I want a web page which displays a single image. The image is to be scaled so it is as large as possible within the browser but without losing it’s aspect ratio.

I also want it to be centred (or centered if you are American).

After spending literally hours trying to do this with CSS and scads of nested div tags, I have decided it is not possible to do it this way. The alternative was to use Javascript to compute the size and position of the image.

Here is the final code which works on IE and Firefox:

<html>
<head>
<script>
function scaleAndCenter(id)
{
 img = document.getElementById(id);
 img.style.position = "absolute";

 if(img.width / document.body.clientWidth > img.height / document.body.clientHeight)
 {
  img.style.width = "100%";
  img.style.top   = (document.body.clientHeight - img.height) / 2;
 }
 else
 {
  img.style.height = "100%";
  img.style.left   = (document.body.clientWidth - img.width) / 2;
 }
}
</script>
</head>
<body style="margin: 0; background: black;">
<img id="image" src="myimage.jpeg" onload="scaleAndCenter(this.id)" />
</body>
</html>