Fixing those pesky PNGs

I’ve been struggling with PNG colour matching for a while. It’s fine on my Linux boxes, with a standard sRGB colour profile, but on Macs and Win/IE with an ICCP in place, the colours never match up with the CSS colours. This is always frustrating, as PNGs are so nice for proper alpha transparency.

I just found this handy snippet out there in the ether, to get rid of the gamma and colour profile info from PNGs.

pngcrush -d outputdir -rem gAMA -rem cHRM -rem iCCP *.png

pngcrush also optimizes the file and reduces the size, which is nice. Handy little tool.

Nginx and WordPress with SSL and Queries

So setting up WordPress on Nginx isn’t difficult. You could follow the example here. This works fine for just a basic install, but what about something that has a load of plugins, for forms and such like? Here we need to tweak the config a bit.

SSL: using SSL with Nginx is not as simple as just turning on the HTTPS section from the default/example config file and copying over the location directives from the port 80 section. You need to ensure that the PHP fastcgi knows that HTTPS is on, so add the directive:

fastcgi_param HTTPS on;

to your location ~ \.php$ section.

Queries: if you are using a forms plugin, or something else that sends GET requests, you may find that they don’t work. Change your default rewrite to:

try_files $uri $uri/ /index.php?$args;

Security: if you are using php5-fpm (you should be!) then it is quite possible to execute remote php files. We don’t want that! Add this line to your php section:

try_files $uri = 404;

Suhosin: you may want to use the Suhosin patch (I do), but this can cause problems with file uploads (and phpMyAdmin). Add the following to your php.ini: = 256 = 65535 = 1024
suhosin.request.max_array_index_length = 256
suhosin.request.max_totalname_length = 65535
suhosin.request.max_vars = 1024

I added this in /etc/php5/conf.d/suhosin.ini, but you may wish to add it elsewhere.

Nginx + php5-fpm + APC + memcache is a super speedy combination. I use it everywhere now. But it does take a bit of getting used to if you are accustomed to Apache.

Just as an afterthought, the docs for setting up WordPress on Nginx in the codex suggest that unless you use APC and a WordPress caching plugin, you will get no performance benefit over Apache. This is utter drivel. Yes I highly recommend APC – it’s great – but you can use it on Apache too. Nginx is fundamentally faster than Apache, even if you compare apples with apples and set up Apache with fastcgi instead of the more common mod_php. This is because Nginx uses an asynchronous, event driven architecture, thus using significantly less resources. It is all explained here.

Get rid of caption inline styles in WordPress

For some stupid reason, WordPress injects an inline style to its image caption divs, setting the width to 10px larger than the enclosed image. This is wrong in so many ways:

  1. You should never use inline styles.
  2. WordPress normally leaves styling to the themes (as it should).
  3. Even if you wanted this behaviour, padding would have been more appropriate.

So how do we fix it? Well I just spent a while searching and found a few ill thought out suggestions, like overriding the style with !important (doesn’t work on all browsers and could cause problems if you want to override it again in a specific class) or replacing the shortcode function, either directly in wp-includes/media.php (just plain crazy) or in your functions.php (better but doesn’t work too well and not very maintainable). This last idea did lead me to find a good solution though. The img_caption_shortcode function has a filter, which effectively overrides its own output. So here’s what I used (tested on WP 3.1):

add_filter( 'img_caption_shortcode', 'my_img_caption', 10, 3 );

function my_img_caption( $nowt, $attr, $content )
	extract( shortcode_atts( array(
		'id' => '',
		'align' => 'alignnone',
		'width' => '',
		'caption' => '',
	), $attr ) );

	if ( empty( $caption ) )
		return $content;

	if ( $id )
		$id = 'id="' . esc_attr( $id ) . '" ';

	return '<div ' . $id . 'class="wp-caption ' . esc_attr( $align ) .
		'">' . do_shortcode( $content ) . '<p class="wp-caption-text">' .
		$caption . '</p></div>';

Shove that in your theme’s functions.php and it will replace the core caption function, with the inline style removed. If you’re wondering what the $nowt variable is about, it is an unused blank string passed by the filter.

Time for a new phone

It’s time for me to get a new phone. I’ve had my beloved G1 for 18 months now. It doesn’t seem that long. Normally by this time I’m desperate to get rid of the phone and get a new one, rueing the day I signed up for the contract. But not this one. It has been the best phone I’ve ever had, by a mile. In fact I would go so far as to say it has been life-changing! The only reason I’m getting a new phone is because I can get a cheaper contract now and the newer Android phones are much faster. I’m glad I’ve been an early adopter of the platform though (I got the phone the day it came out in this country). It has been a fun journey and a real pleasure to see loads of others follow suit and enjoy the freedom of a real smartphone.

This will mean a change in theme for this much neglected blog of course. Since the theme is based on the G1, it won’t really be relevant anymore. I think I’ll go for a different vibe this time. Gotta keep moving.

Can’t wait ’til my HTC Desire comes on Saturday. Super fast and slinky!