painter 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/painter.rb CHANGED
@@ -9,6 +9,7 @@ require 'painter/pixel'
9
9
  require 'painter/text'
10
10
  require 'painter/point'
11
11
  require 'painter/line'
12
+ require 'painter/image'
12
13
 
13
14
  class Painter
14
15
  attr_reader :image_pointer
@@ -33,10 +34,13 @@ class Painter
33
34
  end
34
35
  def self.open(path, format = nil)
35
36
  instance = new
36
- instance.initializeFromOpen(path, format)
37
- yield instance
38
- ensure
39
- instance.release!
37
+ raise "File not found: #{path}" unless File.exists?(path)
38
+ begin
39
+ instance.initializeFromOpen(path, format)
40
+ yield instance
41
+ ensure
42
+ instance.release!
43
+ end
40
44
  end
41
45
 
42
46
  def release!
@@ -89,7 +93,7 @@ class Painter
89
93
  end
90
94
 
91
95
  # Operations
92
- {'pixel' => 'Pixel', 'rect' => 'Rect', 'text' => 'Text', 'line' => 'Line'}.each do |op, klass|
96
+ {'pixel' => 'Pixel', 'rect' => 'Rect', 'text' => 'Text', 'line' => 'Line', 'image' => 'Image'}.each do |op, klass|
93
97
  class_eval <<-END
94
98
  def #{op}
95
99
  #{op}_instance = #{klass}.new(self)
@@ -0,0 +1,24 @@
1
+ class Painter
2
+ class Image < Operation
3
+ pretty_accessor :width, :height, :toWidth, :toHeight, :source, :format
4
+
5
+ def fromTopLeft(x,y)
6
+ @_fromTopLeft = Point.new(x,y)
7
+ end
8
+
9
+ def toTopLeft(x,y)
10
+ @_toTopLeft = Point.new(x,y)
11
+ end
12
+
13
+ def draw
14
+ Painter.open(@_source, @_format) do |source_image|
15
+ CGD2::gdImageCopyResampled(@image.image_pointer,
16
+ source_image.image_pointer,
17
+ @_toTopLeft.x, @_toTopLeft.y,
18
+ @_fromTopLeft.x, @_fromTopLeft.y,
19
+ (@_toWidth || @_width), (@_toHeight || @_height),
20
+ @_width, @_height)
21
+ end
22
+ end
23
+ end
24
+ end
data/spec/integration.rb CHANGED
@@ -4,10 +4,13 @@ describe 'integration' do
4
4
  it "should draw an image" do
5
5
  srand(Time.now.to_i)
6
6
  Painter.write("#{File.dirname(__FILE__)}/output/captcha.png", 200, 100, :png) do |painter|
7
- painter.rect do |rect|
8
- rect.color 255, 255, 255
9
- rect.topLeft 0, 0
10
- rect.bottomRight 200, 100
7
+ painter.image do |image|
8
+ image.source "#{File.dirname(__FILE__)}/fixtures/sample.jpg"
9
+ image.format :jpg
10
+ image.fromTopLeft 0, 0
11
+ image.toTopLeft 0, 0
12
+ image.width 200
13
+ image.height 100
11
14
  end
12
15
  painter.text do |text|
13
16
  text.font "#{File.dirname(__FILE__)}/fixtures/Vera.ttf"
Binary file
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: painter
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ben Sandofsky
@@ -43,6 +43,7 @@ extra_rdoc_files: []
43
43
  files:
44
44
  - lib/painter/cgd2.rb
45
45
  - lib/painter/color.rb
46
+ - lib/painter/image.rb
46
47
  - lib/painter/image_struct.rb
47
48
  - lib/painter/line.rb
48
49
  - lib/painter/operation.rb