Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'rmagick' into stage

  • Loading branch information...
commit 4102157c2be35b4fa762736840d6557e88afb51d 2 parents 3af354d + fadfcc4
Anders Kindberg (Bengtsson) authored March 14, 2012
2  Gemfile
... ...
@@ -1,8 +1,10 @@
1 1
 source :rubygems
2 2
 
  3
+gem "curb", "~> 0.8.0"
3 4
 gem "rack"
4 5
 gem "rake"
5 6
 gem "mime-types"
  7
+gem "rmagick", "~> 2.13"
6 8
 
7 9
 group :development, :test do
8 10
   gem "heroku"
4  Gemfile.lock
@@ -3,6 +3,7 @@ GEM
3 3
   specs:
4 4
     addressable (2.2.7)
5 5
     awesome_print (0.3.2)
  6
+    curb (0.8.0)
6 7
     diff-lcs (1.1.3)
7 8
     git (1.2.5)
8 9
     heroku (2.21.2)
@@ -23,6 +24,7 @@ GEM
23 24
     rake (0.9.2.2)
24 25
     rest-client (1.6.7)
25 26
       mime-types (>= 1.16)
  27
+    rmagick (2.13.1)
26 28
     rspec (2.8.0)
27 29
       rspec-core (~> 2.8.0)
28 30
       rspec-expectations (~> 2.8.0)
@@ -41,11 +43,13 @@ PLATFORMS
41 43
 
42 44
 DEPENDENCIES
43 45
   awesome_print
  46
+  curb (~> 0.8.0)
44 47
   heroku
45 48
   jeweler
46 49
   mime-types
47 50
   rack
48 51
   rack-test
49 52
   rake
  53
+  rmagick (~> 2.13)
50 54
   rspec
51 55
   shotgun
12  lib/imageproxy/command.rb
@@ -2,18 +2,6 @@ module Imageproxy
2 2
   class Command
3 3
     protected
4 4
 
5  
-    def execute_command(command_line)
6  
-      stdin, stdout, stderr, wait_thr = Open3.popen3(command_line)
7  
-      raise "Child process exited with non-zero exit code" unless wait_thr.nil? || wait_thr.value.success?
8  
-      [output_to_string(stdout), output_to_string(stderr)].join("")
9  
-    end
10  
-
11  
-    def curl(url, options={})
12  
-      user_agent = options[:user_agent] || "imageproxy"
13  
-      timeout = options[:timeout] ? "-m #{options[:timeout]} " : ""
14  
-      %|curl #{timeout}-L -s -A "#{user_agent}" "#{url}"|
15  
-    end
16  
-
17 5
     def to_path(obj)
18 6
       obj.respond_to?(:path) ? obj.path : obj.to_s
19 7
     end
115  lib/imageproxy/convert.rb
@@ -12,63 +12,80 @@ def initialize(options)
12 12
     end
13 13
 
14 14
     def execute(user_agent=nil, timeout=nil)
15  
-      execute_command %'#{curl options.source, :user_agent => user_agent, :timeout => timeout} | convert - #{convert_options} #{new_format}#{file.path}'
16  
-      file
17  
-    end
18  
-
19  
-    def convert_options
20  
-      convert_options = []
21  
-      convert_options << "-resize #{resize_thumbnail_options(options.resize)}" if options.resize
22  
-      convert_options << "-thumbnail #{resize_thumbnail_options(options.thumbnail)}" if options.thumbnail
23  
-      convert_options << "-flop" if options.flip == "horizontal"
24  
-      convert_options << "-flip" if options.flip == "vertical"
25  
-      convert_options << rotate_options if options.rotate
26  
-      convert_options << "-colors 256" if options.format == "png8"
27  
-      convert_options << "-quality #{options.quality}" if options.quality
28  
-      convert_options << interlace_options if options.progressive
29  
-      convert_options.join " "
30  
-    end
  15
+      user_agent = user_agent || "imageproxy"
31 16
 
32  
-    def resize_thumbnail_options(size)
33  
-      case options.shape
34  
-        when "cut"
35  
-          "#{size}^ -gravity center -extent #{size}"
36  
-        when "preserve"
37  
-          size
38  
-        when "pad"
39  
-          background = options.background ? %|"#{options.background}"| : %|none -matte|
40  
-          "#{size} -background #{background} -gravity center -extent #{size}"
41  
-        else
42  
-          size
  17
+      request = Curl::Easy.perform(options.source) do |curl|
  18
+        curl.timeout = timeout if timeout
  19
+        curl.useragent = user_agent if user_agent
43 20
       end
44  
-    end
  21
+      original_image = request.body_str
45 22
 
46  
-    def rotate_options
47  
-      if options.rotate.to_f % 90 == 0
48  
-        "-rotate #{options.rotate}"
49  
-      else
50  
-        background = options.background ? %|"#{options.background}"| : %|none|
51  
-        "-background #{background} -matte -rotate #{options.rotate}"
52  
-      end
53  
-    end
  23
+      image = Magick::Image.from_blob(original_image).first
  24
+
  25
+      if options.resize
  26
+        x, y = options.resize.split('x').collect(&:to_i)
54 27
 
55  
-    def interlace_options
56  
-      case options.progressive
57  
-        when "true"
58  
-          "-interlace JPEG"
59  
-        when "false"
60  
-          "-interlace none"
  28
+        if options.shape == "cut"
  29
+          image.crop_resized!(x, y, Magick::CenterGravity)
61 30
         else
62  
-          ""
  31
+          image.change_geometry(options.resize) do |proportional_x, proportional_y, img|
  32
+            img.resize!(proportional_x, proportional_y)
  33
+          end
  34
+        end
63 35
       end
64  
-    end
65 36
 
66  
-    def new_format
67  
-      options.format ? "#{options.format}:" : ""
  37
+      image.to_blob
68 38
     end
69 39
 
70  
-    def file
71  
-      @tempfile ||= Tempfile.new("imageproxy").tap(&:close)
72  
-    end
  40
+    #def convert_options
  41
+    #  convert_options = []
  42
+    #  convert_options << "-resize #{resize_thumbnail_options(options.resize)}" if options.resize
  43
+    #  convert_options << "-thumbnail #{resize_thumbnail_options(options.thumbnail)}" if options.thumbnail
  44
+    #  convert_options << "-flop" if options.flip == "horizontal"
  45
+    #  convert_options << "-flip" if options.flip == "vertical"
  46
+    #  convert_options << rotate_options if options.rotate
  47
+    #  convert_options << "-colors 256" if options.format == "png8"
  48
+    #  convert_options << "-quality #{options.quality}" if options.quality
  49
+    #  convert_options << interlace_options if options.progressive
  50
+    #  convert_options.join " "
  51
+    #end
  52
+    #
  53
+    #def resize_thumbnail_options(size)
  54
+    #  case options.shape
  55
+    #    when "cut"
  56
+    #      "#{size}^ -gravity center -extent #{size}"
  57
+    #    when "preserve"
  58
+    #      size
  59
+    #    when "pad"
  60
+    #      background = options.background ? %|"#{options.background}"| : %|none -matte|
  61
+    #      "#{size} -background #{background} -gravity center -extent #{size}"
  62
+    #    else
  63
+    #      size
  64
+    #  end
  65
+    #end
  66
+    #
  67
+    #def rotate_options
  68
+    #  if options.rotate.to_f % 90 == 0
  69
+    #    "-rotate #{options.rotate}"
  70
+    #  else
  71
+    #    background = options.background ? %|"#{options.background}"| : %|none|
  72
+    #    "-background #{background} -matte -rotate #{options.rotate}"
  73
+    #  end
  74
+    #end
  75
+    #
  76
+    #def interlace_options
  77
+    #  case options.progressive
  78
+    #    when "true"
  79
+    #      "-interlace JPEG"
  80
+    #    when "false"
  81
+    #      "-interlace none"
  82
+    #    else
  83
+    #      ""
  84
+    #  end
  85
+    #end
  86
+    #
  87
+    #def new_format
  88
+    #  options.format ? "#{options.format}:" : ""
  89
+    #end
73 90
   end
74 91
 end
26  lib/imageproxy/server.rb
@@ -31,14 +31,12 @@ def call(env)
31 31
           check_domain options
32 32
           check_size options
33 33
 
34  
-          file = convert_file(options, user_agent)
35  
-          class << file
36  
-            alias to_path path
37  
-          end
38  
-
39  
-          raise "Empty image file" unless File.stat(file.path).size > 0
40  
-          file.open
41  
-          [200, {"Cache-Control" => "max-age=#{cachetime}, must-revalidate"}.merge(content_type(file, options)), file]
  34
+          blob = convert_file(options, user_agent)
  35
+
  36
+          raise "Empty image file" if blob.empty?
  37
+          ctype = {'Content-Type' => 'image/jpeg'}
  38
+
  39
+          [200, {"Cache-Control" => "max-age=#{cachetime}, must-revalidate", "Content-Length" => blob.bytesize.to_s}.merge(ctype), StringIO.new(blob)]
42 40
         when "identify"
43 41
           check_signature request, options
44 42
           check_domain options
@@ -118,12 +116,12 @@ def requested_size(req_size)
118 116
       end
119 117
     end
120 118
 
121  
-    def content_type(file, options)
122  
-      format = options.format
123  
-      format = identify_format(file) unless format
124  
-      format = options.source unless format
125  
-      format ? { "Content-Type" => MIME::Types.of(format).first.content_type } : {}
126  
-    end
  119
+    #def content_type(file, options)
  120
+    #  format = options.format
  121
+    #  format = identify_format(file) unless format
  122
+    #  format = options.source unless format
  123
+    #  format ? { "Content-Type" => MIME::Types.of(format).first.content_type } : {}
  124
+    #end
127 125
 
128 126
     def identify_format(file)
129 127
       Imageproxy::IdentifyFormat.new(file).execute

0 notes on commit 4102157

Please sign in to comment.
Something went wrong with that request. Please try again.