Tag Archives: gd2

Ruby Line / Curve…Thing

Remember these?

During a brief period in elementary school drawing these things was quite the cool thing to do. I’ve been messing around with the GD2 module a bit lately and I thought it’d be fun and easy to write a little script to generate one.

It was.

The drawing algorithm

@nodes.times do |i|
    adjustment = i * @node_size
    pen.move_to adjustment, 0
    pen.line_to 0, @size - adjustment
end

Example Usage:

#takes in the output image size and the number of nodes
c = Curve.new 180, 30
c.draw
c.export "test.gif"

Download it!

*Note: If your output size doesn’t divide evenly by the number of nodes then the image will actually reduce the size of the output image.

Ruby Resize Utility

I wanted to demo an ad rotator the other day at work and I wanted to load up a bunch of images of various sizes to make it look legit. It would have taken forever to do it ad by ad, so I wrote a little ruby script to automatically resize all the images in a directory. Then I used my directory listing utility to copy the files into my clipboard. Pasted into the database from there!

(Requires GD2)

Usage:

 r = Resizer.new input_path, output_path, width, height
 r.crop_files

Download it!

JavaScript Tile Puzzle

I was tinkering a bit while watching the tube tonight and I thought it might be fun to make a simple JavaScript tile puzzle. Might look terrible in other browsers.

First step was to write a little ruby script (using the GD Graphics Library) to read in a file and cut it up into individual tiles. (Photoshop is for wussies)

if __file__ = $0
  #pass the image name, tile size through the command line
  t = Tile_Cropper.new ARGV[0], ARGV[1].to_i
  t.crop_tiles
end

Then I used a slightly modified method I read about in Ben Nadel’s blog to scramble the tiles.


	
	
	
	
		
	
	

A little bit of CSS magic:

#puzzle {
	width:      500px;
	margin:     0px;
	padding:    0px;
}
.piece {
	list-style: none;
	display:    inline;
	margin:     0px;
	padding:    0px;
}
img {
	border:     1px solid gray;
}

And finally I used scriptaculous and prototype for the obnoxious drag-and-drop sorting. Not that I’m complaining. the Sortable methods weren’t intended to be used for something like this.

function init() {
	Sortable.create('puzzle');
}

Complete the puzzle to find out what I was watching!. Here, I’ll give you a hint. It’s Dexter.

Download the code.