The case against tags

To every modern web 2.0 site offering tagging seems to be an absolute requirement. While I may agree, that tagging in some cases may offer some improvements in content navigation over many other points, I do think it’s been too abused in way to may cases.

Tags on sites such as Flickr and Del.icio.us are absolutely fantastic. They offer some cross navigation options, which would probably be near impossible, if not for the tags. I often find my self exploring links though del.icio.us, since the content stored there generally seem to be of a muh high quality than a simple Google search on a common term.

The newest WordPress Release (version 2.3 ) supports tags right out of the box, and I’m sure those not using tags already will go crazy with them, and most wordpress installations will feature tag-clouds within a few months. I’m also sure, that most of these tag clouds shouldn’t be tags, but categories.

Now the problem with tags in most cases, however, is that often there’s only one piece of content per tag (or at least very few). This to me suggests that it’s too easy to make tags or that the author/publisher doesn’t know how to use them. Tags are great for making collections or making ”cross-user navigation”, but if you’re the only user publishing in the tag collection or you doesn’t have content to have a number of posts per tag, then you’re probably better of using categories.

That’s my $0.02 for the time being anyway..

Wanted (source)code plugin for WordPress

The past days I’ve been posting stories with a few code samples – Perl, PHP, Javasscript and/or HTML. While WordPress is fantastic to a number of things, handling sourcecode examples in the posts, reallly isn’t one of them.

I’ve been playing with a number of plugins, but none of them really works for me. What I’m looking for is this:

  • It must be able to handle HTML, Javasscript, PHP and Perl sourcecode.
  • The code samples should work with the visual editor (preferably entered in this, but ”code mode” is quite alright) – Visual post editing shouldn’t break the code example.
  • The plugin should be alive (maintained and tested with recent wordpress releases).

I would have thought it would have been easy to find a plugin that covered the above requirements, but so far I’ve had very little luck – any suggestions?

Changing a form submit url with javascript

Sometimes you might need to change the address a form submits to, it’s quite easy with javascript. One example could be if you – like me – from time to time is hit hard by comment-spammers. Changing the submit URL on the form with javascript, makes it at least somewhat harder for “automated brainless robots” to kill your site in a spam attack.

The recipie below is a very simple example, but so far it seems to work quite well:

<script type="text/javascript">
function doSubmit() {
  var target1 = 'hello';
  var target2 = 'world';
  var target3 = '.cgi';

  var supertarget = target0 + target1 + target2 + target3;
  var theForm=document.getElementById("theForm");
  theForm.action = supertarget;
  theForm.submit();
}
</script>

<form id="myForm" method="post" action="some_fake_address.php">
<input type="text" name="dummy field" value="">
<input type="button" onclick="javascript:doSubmit();">
</form>

Whats going on?

The form is a regular form except for to small things:

The action-url is a fake address. On the url it points to, is only an empty file (to prevent 404 errors in my errorlog.
The normal submit button has been changed to a regular button, and the onclick-evnt calls a small piece of javascript, which does the magic trick.

The javascript function can be more or less advanced depending on your needs. In this example it constructs the new target for the form submit by pasting three strings together and replacing the action of the form.

Bulk resizing images with Perl

Suppose you’ve just filled you digital camera with an endless stream of photos. You want to place them online at your website, but placing 5+ megapixel files online, well…probably a bad idea. Let’s resize them to a propper size – and why not use Perl and ImageMagick for the job.  Not a problem, here’s a complete example on how to resize all images in a directory . Make sure you have ImageMagick installed.

Syntax checking PHP on the commandline

I’m sure most people only thing of PHP as a Weblanguage due to be called through a browser. It has however since version 4.3.0 also been possible to use PHP on the commandline – as you do with Perl, Shell scripts and likewise. If you’re using Linux (or an other Unix-like operating system – including Mac OSX) you probably have a few small programs available which can make it a breeze to check if the syntax in all you PHP scripts is correct.

Here’s how. On the commandline type:

find *.php |xargs -I {} php -l {}

… and here’s an explaination of what it does:

  • “find *.php” findes all files with a dot php ending.
  • the pipe-character “|” sends the result to a command called xargs.
  • “xargs -I {} php -l {}” takes the lines one by one password from the find and call “php -l <line input>”.
  • “php -l <line input>” (where line input would be the php-files found in bullit one) runs php with the “lint” parameter which does the syntax checking.

Converting between image formats with Perl

Changing files from one format to another is quite easy with a little bit of ImageMagick . In the example below a JPG image (test.jpg) is converted into a GIF-image (test.gif). To output in a different (ImageMagick supported ) format, just change the “image->Set” line.

[php] no_tags
#!/usr/bin/perl -w
use strict;
use Image::Magick;

my $image = Image::Magick->new();

# To explicitly set image format use this instead:
# my $image = Image::Magick->new(magick=> ‘JPEG’);

my $x = $image->Read(‘test.jpg’);
$x = $image->Set(magick => ‘GIF’);
$x = $image->Write(‘test.gif’);

exit();[/php]

Image sizes in Perl

If you need for figure out the size of an image, fetch Image::Size from CPAN , it’s just what you need. The module recognizes the most common image-formats such as JPG, GIF, PNG, PSD, TIFF and plenty more. The interface is simple and will get you what you need with no trouble at all.

[php] no_tags
#!/usr/bin/perl -w
use strict;
use Image::Size;

# Just fetch the size
my ($size_x, $size_y) = Image::Size::imgsize(‘test.jpg’);
print “Image is: $size_y x $size_x (height x width)\n”;

# HTML-friendly format
my $size = Image::Size::html_imgsize(‘test.jpg’);
print “Here is my image \n”;

exit();
[/php]

Image sizes and PHP

If you have the GD extension available, you can do a few useful things. One of the simple things is getting an image size is a simple matter. You can either fetch the height and width in seperate variables – or even fetch the height and width in a string pre-formatted for use in an image tag.

Here’s the example in source:

1
2
3
4
5
6
7
8
9
10
11
12
$load_ext = get_loaded_extensions();
 
if (!in_array(gd, $load_ext)) {
echo "GD is NOT available";
die();
}
 
$nikonFile   = './aarhus_demo_photo.jpg';
list($width, $height, $type, $img_txt)  = getimagesize($nikonFile);
 
echo "The image is ".$width . " by ".$height."\n";
echo "<img $img_txt src='$nikonFile'/>\n";

And here’s how it looks (source).

Reading Exif data with PHP

Within most photos from digital cameras besides the actual image, there’s a little ”information block” call EXIF data. If you have the correct PHP extension installed on your server – the one called ”exif” – it’s pretty easy to read the EXIF data and display them, as you like.

First, let’s check if the extension is available?

1
2
3
4
5
6
$load_ext = get_loaded_extensions();
if (!in_array(exif, $load_ext)) {
echo "Exif is NOT available";
} else {
echo "Exif extension is available.";
};

If you have the extension available, reading the data is a simple matter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$nikonFile   = './aarhus_demo_photo.jpg';
$exifNikon   = getExifData($nikonFile);
 
$olympusFile = './zanzibar_demo_photo.jpg';
$exifOlympus = getExifData($olympusFile);
 
echo "&lt;h2&gt;Olympus EXIF data&lt;/h2&gt;";
echo "&lt;pre&gt;";
print_r($exifOlympus);
echo "&lt;/pre&gt;";
 
echo "&lt;h2&gt;Nikon EXIF data&lt;/h2&gt;";
echo "&lt;pre&gt;";
print_r($exifNikon);
echo "&lt;/pre&gt;";
 
function getExifData($file) {
$exif = exif_read_data($file, 0, true);
return $exif;
}

As you may notice, there’s a photo from an Olympus camera and a Nikon camera as an example. The data available from each photo isn’t quite the same. Most generic fields are available, but some fields are camera specific (or maker specific).

See the example above in action (source). The two images are from Aarhus (Nikon) and Zanzibar (Olympus). You can read more on EXIF in Wikipedia.

Simple benchmarks in PHP

If you’re doing some basic profiling of your PHP scripts, the build-in microtime function can help you make some simple benchmarking fast. Here’s a rough example to show you how it could be used. The doSomething function is the function we want to benchmark.

1
2
3
4
5
6
7
8
9
10
11
12
13
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
 
doSomething();
 
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 6);
if ($debug) print ("&lt;p&gt;Processing took approximately $total_time seconds&lt;/p&gt;");

The above example is pretty rough. If you want to see a slightly more complete benchmark example, I do have it available (with source too).