Restoring Missing or Empty Environment Variables in PHP 5.3 for TextMate Commands

I am a big fan of TextMate and I am still learning to leverage its features, even after using it for over two years. I have started writing more “commands” to speed up my development process. I love that I can write commands in PHP that run locally on my Mac, and I still have access to all of the environment variables that TextMate passes such as the current line’s value or the document type and name.

The Problem

I recently upgraded to Snow Leopard, and as a result, upgraded to PHP 5.3 on my local development machine. I have been really impressed with the new features of 5.3, and I am happy it came packed with my OS X install. However, I noticed that a few of my commands had stopped functioning correctly.

I hadn’t had a chance to investigate the problem, but when I sat down to write a new command the other day, I figured it out. The $_ENV variable is an empty array on a default Snow Leopard install. When running PHP from the command line, this global variable holds all of the data about the environment PHP is running in. It’s also how TextMate injects it’s variables into the PHP script.

Continue reading

Round to the Nearest Half in Php

Sometimes, you want to round a number, but you want the nearest half instead of a full number. For example, if you have an average rating, and you want to display a star graphic representing the number of stars. In the interest of simplicity, you would like to show only full or half stars (3.5 stars instead of 3.43 stars). Here is a very simple example of how to do this in php.

$average = 3.43;
$round_to = 0.5; // you can change this to any increment you like
$rounded = round($average / $round_to) * $round_to;

The variable “$rounded” is now set to 3.5.

Fixing Pixelation from Scaled Images in IE

A project I worked on recently called for a carousel with five images of different sizes — somewhat like a “light” coverflow. I ended up building it with basic <img> tags that get scaled by javascript which modified the width attribute.

Cover Slider Layout Prototype

Cover Slider Layout

One of the problems that I ran in to involved the loss of quality from Internet Explorer 6 and 7 when the browser renders the image at a smaller dimension than the file.

Wired Cover scaled in IE

An image scaled in internet explorer.

This project was for a site that still has a substantial amount of visitors using IE6 (and IE7′s scaling abilities are not much better than 6). So I needed to come up with a way to correct the image scaling. A short google later, I found Ethan Marcotte‘s blog post Fluid Images [via Unstoppable Robot Ninja] and a post on flickr’s developer blog. Continue reading