pixelholder 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/pixelholder.rb +151 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e866d7e6ec967b2d10e2d66c271fb30f71346da1
4
+ data.tar.gz: d400e93d8bf8b6fc553b4a76ce321d2bb6474e38
5
+ SHA512:
6
+ metadata.gz: 535eb88b79e16b6d06f98a7523074d7c506160a75ace54dd242a7e58e5e1ed25ca2854433805bb55779659dab7e1048b4e2e40e8a642549eb86c27a719e21803
7
+ data.tar.gz: a407826f1e62bb4c138ae65988ab9e88dcb2cb90a032fcd62a2deb04ba8218457b2b8845d2e7a02b916e6749c832fa70b8197483204ca3a7b632428991ea28db
@@ -0,0 +1,151 @@
1
+ # pixelholder.rb
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ require 'flickr_fu'
6
+ require 'RMagick'
7
+
8
+ class PixelHolder
9
+
10
+ # Initialize the object
11
+ def initialize(subject, dimensions, extra_options)
12
+ # In case we ever decide to allow extra formats for whatever reason
13
+ @image_format = extra_options[:image_format] ? extra_options[:image_format] : 'jpg'
14
+
15
+ # Set image dimensions values
16
+ dimensions = dimensions.downcase.split('x')
17
+ default_dimension = 200
18
+
19
+ @width = dimensions[0].to_i
20
+
21
+ if dimensions[1].nil? then @height = @width
22
+ else
23
+ @height = dimensions[1].to_i
24
+ if(@height == 0) then @height = default_dimension end
25
+ end
26
+
27
+ # Create image with background
28
+ subject = subject.downcase.split(':')
29
+ background_type = subject[0] if subject.length > 1
30
+
31
+ case background_type
32
+ when 'color'
33
+ generate_canvas(subject[1])
34
+ when 'gradient'
35
+ gradient_colors = subject[1].split(',')
36
+
37
+ if gradient_colors[2].nil? || gradient_colors[2] == 'v'
38
+ end_x = @width
39
+ end_y = 0
40
+ else
41
+ end_x = 0
42
+ end_y = @height
43
+ end
44
+
45
+ fill_content = Magick::GradientFill.new(0, 0, end_x, end_y, get_hex(gradient_colors[0]), get_hex(gradient_colors[1]))
46
+
47
+ generate_canvas('000', fill_content)
48
+ else
49
+ seed = extra_options[:seed] ? extra_options[:seed].to_i : 0
50
+
51
+ flickr = Flickr.new('config/flickr.yml')
52
+
53
+ # Grab all images with Creative Commons licencing
54
+ photos = flickr.photos.search(:tags => subject[0], :tag_mode => 'all', :license => '4,5,6,7', :media => 'photo')
55
+ photo = photos[seed]
56
+
57
+ generate_canvas()
58
+
59
+ unless photo.nil?
60
+ image_background = Magick::ImageList.new
61
+ image_url = open(photo.url(:large))
62
+ image_format = @image_format
63
+
64
+ # Read the Flickr image as a blob
65
+ image_background.from_blob(image_url.read) do
66
+ self.format = image_format
67
+ self.quality = 100
68
+ end
69
+
70
+ # Make the image meet the dimensions
71
+ image_background.resize_to_fill!(@width, @height)
72
+ @canvas.composite!(image_background, Magick::CenterGravity, Magick::CopyCompositeOp)
73
+ end
74
+ end
75
+
76
+ # Text overlay
77
+ if extra_options[:text]
78
+ generate_overlay_text(extra_options[:text], extra_options[:text_color])
79
+ end
80
+
81
+ end
82
+
83
+ # Returns a blob string of the generated image
84
+ def get_blob
85
+ @canvas.to_blob
86
+ end
87
+
88
+ # Returns a hex color code
89
+ def get_hex(color)
90
+ "##{generate_color(color).downcase}"
91
+ end
92
+
93
+ # Returns the appropriate color code string
94
+ def generate_color(color)
95
+ return color.ljust(6, color) if (1..2).include?(color.length)
96
+ return color.scan(/((.))/).flatten.join if color.length == 3
97
+ return color.ljust(6, '0') if (4..5).include?(color.length)
98
+ return color[0..5] if color.length > 6
99
+ return color
100
+ end
101
+
102
+ # Generates an Imagick canvas
103
+ def generate_canvas(background = '000', fill_content = nil)
104
+ background = get_hex(background)
105
+ image_format = @image_format
106
+
107
+ if fill_content.nil?
108
+ @canvas = Magick::Image.new(@width, @height) do
109
+ self.background_color = background
110
+ self.format = image_format
111
+ end
112
+ else
113
+ @canvas = Magick::Image.new(@width, @height, fill_content) do
114
+ self.format = image_format
115
+ end
116
+ end
117
+ end
118
+
119
+ # Add a text overlay to the image
120
+ def generate_overlay_text(string, color = false)
121
+ if string == 'add_dimensions' then string = "#{@width} #{215.chr} #{@height}" end
122
+
123
+ overlay_text = string
124
+ overlay = Magick::Draw.new
125
+
126
+ overlay.fill = color ? get_hex(color) : get_hex('fff')
127
+ overlay.stroke = "rgba(0,0,0,0.15)"
128
+ overlay.stroke_width = 20
129
+ overlay.font_weight = Magick::BoldWeight
130
+ overlay.pointsize = 500
131
+ overlay.gravity = Magick::CenterGravity
132
+ overlay.interline_spacing = -500
133
+
134
+ font_size = overlay.get_type_metrics(overlay_text)
135
+
136
+ overlay_canvas = Magick::Image.new(font_size.width, font_size.height) do
137
+ self.background_color = 'transparent'
138
+ end
139
+
140
+ overlay.annotate(overlay_canvas, 0, 0, 0, 0, overlay_text)
141
+
142
+ overlay_height = @height * 0.8
143
+
144
+ overlay_canvas.resize_to_fit!(@width * 0.8, overlay_height)
145
+
146
+ @canvas.composite!(overlay_canvas, Magick::CenterGravity, Magick::OverCompositeOp)
147
+ end
148
+
149
+ private :generate_canvas, :generate_overlay_text
150
+
151
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pixelholder
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Dingli
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple placeholder image generator
14
+ email: cldingli@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/pixelholder.rb
20
+ homepage: http://github.com/chrisdingli/pixelholder-rubygem
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.1
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: A placeholder image generator
44
+ test_files: []