一覧 最終更新 HOME ログイン

freedomcat.com

第十回Wikiばな用名札作成スクリプト

これはなに?

背景画像ファイルback.png(881)と来場者名簿(サンプル)org_test.csv(1415)を読み込んで、twitterからアイコン画像を取ってきて、こんな感じのファイルhttp://twitpic.com/1tk28zを生成するスクリプト。


山田 花子

出力例

山田
花子

準備

  1. 背景画像とstaff画像は./src ディレクトリに置いとく。
  2. 出力ファイルを保存する./nafudaディレクトリを作成しとく。
  3. ソースファイルの設定
    1. Nafuda.newにcsv fileのパス, Noの列, お名前の列,twitterIDの列を指定する
    2. Twitter::HTTPAuth.newにtwitterのIDとパスワードを指定する

注意

ソース

require 'rubygems'
require 'csv'
require 'kconv'
require 'twitter'
require 'oauth'
require 'net/http'
require 'uri'
require 'pp'
require 'RMagick'
include Magick

class RMagickApi
	def initialize(number,name,twitterid)
		@number = number
		@name = name
		@twitterid = twitterid
		@nafuda_file = "./nafuda/"+@number+".jpg"
	end

	def double_byte_type(text)
			if(text.split(//u).size == text.length) then
					return false 
			else
					return true
			end
	end

	def get_font_size(text)
			count = text.split(//u).size
			length = text.length
			case count 
			when 1
					if(count == length) then # 半角
						font_size = 150
					else					 # 全角
						font_size = 90 
					end
			when 2
					if(count == length) then # 半角
						font_size = 140
					else					 # 全角
						font_size = 90 
					end
			when 3
					if(count == length) then # 半角
						font_size = 130
					else					 # 全角
						font_size = 86 
					end
			when 4
					if(count == length) then # 半角
						font_size = 120
					else					 # 全角
						font_size = 70 
					end
			when 5
					if(count == length) then # 半角
						font_size = 110 
					else					 # 全角
						font_size =  56 
					end
			when 6
					if(count == length) then # 半角
						font_size = 86 
					else					 # 全角
						font_size = 48 
					end
			when 7
					if(count == length) then # 半角
						font_size = 80 
					else					 # 全角
						font_size = 40 
					end
			when 8
					if(count == length) then # 半角
						font_size = 70 
					else					 # 全角
						font_size = 32 
					end
			when 9
					if(count == length) then # 半角
						font_size = 62 
					else					 # 全角
						font_size = 28 
					end
			when 10  
					if(count == length) then # 半角
						font_size = 54 
					else					 # 全角
						font_size = 26	 
					end
			when 11 
					if(count == length) then # 半角
						font_size = 50 
					else					 # 全角
						font_size = 24 
					end
			when 12 
					if(count == length) then # 半角
						font_size = 45 
					else					 # 全角
						font_size = 22 
					end
			when 13 
					if(count == length) then # 半角
						font_size = 42 
					else					 # 全角
						font_size = 20
					end
			else
					font_size = 12
			end
			return font_size
	end

	def write_number
		img_out = ImageList.new(@nafuda_file)
		draw = Magick::Draw.new
		draw_name(img_out, draw, 30, 300, 460 , @number)
		img_out.write(@nafuda_file)
	end

	def write_name
		img_out = ImageList.new('./src/back.png')
		draw = Magick::Draw.new

		nameAry = @name.split(" ")
		index = 0
		last_font_size = 0
		nameAry.each {|f|

			font_size =	get_font_size(f)

			if( nameAry.length == 1 ) then 
				draw_center_name(img_out, draw, font_size, f)

			elsif( index== 0 ) then
				last_font_size = font_size*1.2
				draw_last_name(img_out, draw, last_font_size, f)

			elsif( index==1 ) then
				draw_first_name(img_out, draw, font_size*0.8, last_font_size, f)
			end

			index+=1
		}
		img_out.write(@nafuda_file)
	end

	def draw_center_name(img_out,draw,font_size,name)
			if( double_byte_type(name) ) then
				draw_name(img_out,draw,font_size,10, 140,name)
			else
				draw_name(img_out,draw, font_size,10,140,name)
			end
	end

	def draw_last_name(img_out,draw,font_size,name)
		draw_name(img_out,draw,font_size,10, 115,name)
	end

	def draw_first_name(img_out, draw, font_size, last_font_size, name)
		draw_name(img_out,draw,font_size,10, 115+(last_font_size+10), name)
	end

	def draw_name(img_out,draw, font_size, x, y , name)
		#annotate(img,	width,	height,	x,	y,	text)
		draw.annotate(img_out,10,30, x, y,name){
			self.fill = "#000000" #色
			self.font = "c:\\Windows\\fonts\\meiryo.ttc"
			self.stroke = 'transparent'
			self.pointsize = font_size
			self.gravity = Magick::NorthWestGravity
		}
	end

	def resize_icon
		imgs_tmp = Magick::Image.read(@icon_file)
		img_tmp = imgs_tmp.first
		img_tmp.resize_to_fit!(86,86)
		img_tmp.write(@icon_file)
	end

	def write_icon
		cover_img_to_nafuda( @icon_file,14,20)
	end

	def write_staff
		cover_img_to_nafuda("./src/staff.png",140,80)
	end

	def cover_img_to_nafuda(img_cover_filename,x,y)
		img_back = ImageList.new(@nafuda_file)
		img_cover = ImageList.new(img_cover_filename)
		img_out = img_back.composite(img_cover, Magick::NorthWestGravity,x,y,AtopCompositeOp)
		img_out.write(@nafuda_file)
	end

	def set_icon(filename)
			@icon_file = filename
			p @icon_file
	end
end

class TwitterApi
	def initialize
		httpAuth = Twitter::HTTPAuth.new('twitter id','twitter pass')
		@client = Twitter::Base.new(httpAuth)
	end

	def get_profile(id)
			p id
		@profile = @client.user(id)
	end

	def get_profile_image
		p @profile.profile_image_url
	end

	def save_profile_image(id)
		get_profile(id)
		url = @profile.profile_image_url
		p "icon_url:" , url
		filename = File.basename(url)	
		filename.slice!(0,filename.length-4)
		filename = "./tmp/"+id + filename
		open(filename, 'wb') do |file|
			file.puts Net::HTTP.get_response(URI.parse(url)).body
		end
		return filename
	end

	def dump
		@profile.each do |status|
			pp status
		end
	end

	def save(file_name)
		File.open(file_name,'w'){ |file|
			@profile.each do |status|
				file.puts status
			end
		}
	end
end

class Nafuda
	NUMBER_CELL = 0
	NAME_CELL = 1
	TWITTERID_CELL= 2
	STAFF_CELL = 3

	def initialize(csvfile,no_cell,name_cell,twitterid_cell,staff_cell)

		@nafuda_csv = CSV.readlines(csvfile)

		@column = Array.new([no_cell,name_cell,twitterid_cell,staff_cell])

		@twapi = TwitterApi.new
	end

	def make_nafuda 
		lines=0
		@nafuda_csv.each{ |row|
			cells=0
			csvrow = Array.new
			row.each{ |cell|
				set_csvrow(csvrow,cells,cell)
				cells+=1
			}
			if( lines!=0 )then
				img = RMagickApi.new(csvrow[NUMBER_CELL],csvrow[NAME_CELL],csvrow[TWITTERID_CELL])
				img.write_name
				img.write_number

				if( csvrow[TWITTERID_CELL] != nil ) then
					img.set_icon( @twapi.save_profile_image(csvrow[TWITTERID_CELL] ))
					img.resize_icon
					img.write_icon
				end

				if( csvrow[STAFF_CELL]!=nil ) then
					img.write_staff
				end
			end
			lines +=1
		}
	end

	def set_csvrow(csvrow,cells,cell)
		case cells
		when @column[NUMBER_CELL] 
			csvrow[NUMBER_CELL] = Kconv.toutf8(cell)

		when @column[NAME_CELL] 
			csvrow[NAME_CELL] = Kconv.toutf8(cell)

		when @column[TWITTERID_CELL] 
			if( cell != nil) then 
				p "twitterid "+cell
				csvrow[TWITTERID_CELL] = Kconv.toutf8(cell)
			end

		when @column[STAFF_CELL] 
			if( cell != nil) then 
				p "staff"
				csvrow[STAFF_CELL] = Kconv.toutf8(cell)
			end
		end
	end
end

# csv file, No, お名前,twitterID,スタッフ
nafuda = Nafuda.new('./src/org.csv',0,1,8,11)
nafuda.make_nafuda

更新履歴

[script]