(4 ratings)   
By: jordan.husney.com
I went milling about for source for progress bars that would be suitably embedded within a Ruby on Rails application I'm working on and came up with nada. Sure, sure there are lots of bars that have to do with live uploading and whatnot but nothing...
Added: 23 June 2008    Views: 97  
PathComputers    Programming    Ruby
Keywords: computers   programming   ruby   coder   code   language   bar  
Do you like this tutorial? Now you can support our team to add more :     
 
 
 

I went milling about for source for progress bars that would be suitably embedded within a Ruby on Rails application I'm working on and came up with nada. Sure, sure there are lots of bars that have to do with live uploading and whatnot ; but nothing that grabbed me with the interface I was looking for (call function, return data).

Like cheating on a lover, I went looking into PHP territory and found a really quite wonderful and straightforward example here by Bård (Bob) Johannessen.

It only took about 20 minutes to adapt to a halfway decent ruby class and now I'm rollin'. Here are some output examples:



Here's the source:

#!/bin/env ruby18
# copyright 2005, Jordan Husney <jordan@husney.com>
# (http://jordan.husney.com/main.html)
#
# derived directly from the following source and protected under
# the GNU GENERAL PUBLIC LICENSE, Version 2, June 1991
#
# http://db.org/demo/2003/02/17/progress-bar/?view=source
#
# php+gd dynamic progress bar image script.
# copyright 2003, B. Johannessen <bob@db.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version, and provided that the above
# copyright and permission notice is included with all distributed
# copies of this or derived software.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of the GNU General Public License is available from the GNU
# website at the following URL: http://www.gnu.org/licenses/gpl.txt


require 'rubygems'
require 'gd2'

class RubyProgressbar
include GD2

IMAGE_DIR = "./images/"
IMAGE_STYLES = [ 'winxp', 'osx', 'led', 'solaris' ]

public

def initialize
@image = nil
end

def create(style, width, completed, total)
# Check arugments:
raise ArgumentError, "style '#{style}' unknown" if
not IMAGE_STYLES.include?(style)
raise ArgumentError, "width must be between 64 and 1280 pixels" if
width < 64 || width > 1280
raise ArgumentError,
"completed must be greater than 0 and less than total" if
completed < 0 || completed > total
raise ArgumentError, "total must be greater than 0" if
total < 1

# Open images according to defined style:
begin
bg = Image.import(IMAGE_DIR + style + '-bg.png')
fill = Image.import(IMAGE_DIR + style + '-fill.png')
bg_cap = Image.import(IMAGE_DIR + style + '-bg-cap.png')
fill_cap = Image.import(IMAGE_DIR + style + '-fill-cap.png')
rescue Exception => e
raise "Unable to load required images for style #{style}!"
end

# calculate the fill width:
fill_width = (((width - bg_cap.width) * completed) / total).round -
fill_cap.width

# create the new image, and copy the fragments into it:
image = Image::TrueColor.new(width, bg.height)
image.copy_from(bg, 0, 0, 0, 0, bg.w, (width - bg_cap.w))
image.copy_from(bg_cap, (width - bg_cap.w), 0, 0, 0, bg_cap.w, bg_cap.h)
image.copy_from(fill, 0, 0, 0, 0, fill_width, fill.h)
image.copy_from(fill_cap, fill_width, 0, 0, 0, fill_cap.w, fill_cap.h)

@image = image

# return success
return true
end

def destroy
@image = nil
end

def gif()
raise Exception, "image not created" if @image.nil?
return @image.gif()
end

def jpeg(compression = nil)
raise Exception, "image not created" if @image.nil?
return @image.jpeg(compression)
end

def png(compression = nil)
raise Exception, "image not created" if @image.nil?
return @image.png(compression)
end

end

It can be used thusly:

require 'ruby_progressbar'

bar = RubyProgressbar.new
# OSX style, 320px wide, 3 of 10 units completed on the bar:
bar.create("osx", 320, 3, 10)
f = File.new("output.png", "w")
f.write(bar.png)
f.close

About the Author :
jordan.husney.com
Progress Bars with GD2 and Ruby
Articles and Tutorials Directory by www.learnfobia.com
 Rate this tutorial : Rate 1Rate 2Rate 3Rate 4Rate 5
  |    Add to Favorites
  |    Send to Friend
  |    Print
Comments