Basic word wrapping with Imagick

The following is a simple greedy word-wrapping algorithm for ImageMagick in PHP.

Released under public domain.

$words = explode(" ", $msg);
$lines = array();
$current_line = "";
foreach ($words as $word) {
  if ($current_line == "") {
    $test = $word;
  } else {
    $test = "$current_line $word";
  $metrics = $image->queryFontMetrics($draw, $test);
  if ($metrics['textWidth'] > $max_width) {
    if ($current_line == "") {
      $lines[] = $test;
      $current_line = "";
    } else {
      $lines[] = $current_line;
      $current_line = $word;
  } else {
    $current_line = $test;
if ($current_line != "") {
  $lines[] = $current_line;
$msg = implode("\n", $lines);

Dynamically generating a .wav in JavaScript

This is a little experiment to put together a sine waveform generator that generates .wav files on the fly in pure JavaScript and plays the .wav file using whatever plugin your browser has installed. Most likely, the plugin that you will need is QuickTime, since that seems to register itself as the default player on many systems.

Some combinations of parameters produce larger files, but be aware that it will stop working if the size of the .wav file grows too large.

Update: Now there’s HTML5 audio. Hooray!

Check out the generator here.

Spoofing a UDP packet in Python

Here’s a little experiment to spoof UDP packets using Python. The packets are constructed using the pyip module, and the packets are sen using raw socket mode. Raw socket mode requires root / administrator privileges.

The following code doesn’t calculate the UDP checksum, as the checksums that pyip generate don’t appear to be correct. Your system should take care of that.

import udp
import socket

# build UPD packet
udp_packet = udp.Packet() = 1024;
udp_packet.dport = 3024; = "\xFF\xFF\xFF\xFFrcon \"test\" test\0"
packet = udp.assemble(udp_packet, 0)

sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)
ret = sock.sendto(packet, ("", 0))
print "sent %d bytes" % ret

PHP function to draw wrapped text

Here’s a PHP code snippet that draws text bounded by a textbox. You’ll need GD with TrueType support.

The snippet supports rotation, vertical and horizontal alignments, configurable line heights and leading, word wrap, character wrap, and text outlining.

Continue reading

FileReference, FileReferenceList and timeouts

FileReference and FileReferenceList in Actionscript will cause a script timeout (or rather several script timeouts) with lengthy uploads. This is a problem because Flash is sometimes the only practical way to upload a large file with a progress bar (Java is fat, a memory hog, and is not as widespread as Flash). I managed to solve this “bug” by periodically calling a worthless function in Actionscript from JavaScript, successfully “tricking” the Actionscript engine to reset its timeout. I imagine it would work from within Actionscript too, but at the time I was working with a JavaScript< ->Actionscript JavaScript library.