Created
August 11, 2011 09:36
-
-
Save JosephKu/1139280 to your computer and use it in GitHub Desktop.
Image uploading test tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# image_uploading_test.rb | |
# | |
# Written by Joseph Ku <chiehfang.ku@gmail.com> | |
# All right reserved. | |
require 'rmagick' | |
require 'rest_client' | |
def generate_image(image_filename, id, width, height) | |
data = Array.new(width) do | |
Array.new(height) do | |
[rand(255), rand(255), rand(255)] | |
end | |
end | |
img = Magick::Image.new(width, height) | |
data.each_with_index do |row, row_index| | |
row.each_with_index do |item, column_index| | |
#puts "setting #{row_index}/#{column_index} to #{item}" | |
img.pixel_color(row_index, column_index, "rgb(#{item.join(', ')})") | |
end | |
end | |
gc = Magick::Draw.new | |
gc.pointsize = 20 | |
gc.fill('black') | |
gc.text(5, 50, "#{width}x#{height}") | |
gc.text(30, 70, "##{id}") | |
gc.draw(img) | |
img.write(image_filename) | |
end | |
if __FILE__ == $0 | |
width = 100 | |
height = 100 | |
number = 1000 | |
time_start = Time.now | |
number.times do |n| | |
id = n + 1 | |
image_filename = "#{width}x#{height}-#{id}.jpg" | |
generate_image(image_filename, id, width, height) | |
target_host = 'http://localhost:3000/' | |
target_host = ARGV[0] unless ARGV[0] == nil | |
puts "Generate ##{id} image" | |
puts "Uploading #{image_filename} to #{target_host}..." | |
begin | |
RestClient.post(target_host, :upload => File.new(image_filename)) | |
rescue | |
puts "Failed!" | |
puts | |
puts "Abort." | |
exit -1 | |
end | |
puts "Uploading successfully." | |
puts | |
end | |
time_end = Time.now | |
puts "Elapsed time: #{time_end - time_start}" | |
puts | |
puts "Done!" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment