background_color

13 views
Skip to first unread message

Wilaya PointCom

unread,
May 11, 2013, 4:55:00 PM5/11/13
to dragonf...@googlegroups.com

Hi,

 is it possible to fit with Dragonfly gem an image to a fixed space (500x375 in my case) and fill gaps with specific color ?

I tried many things from here, including something like image.thumb('500x375').process(:background_color => 'white') but it didn't work...

Is there any way?

Thanks a lot for your help.

Mark Evans

unread,
May 12, 2013, 7:54:25 AM5/12/13
to dragonf...@googlegroups.com
Hi
not out of the box, but you can create a custom processor to do it.
also you can run arbitrary convert commands using image.convert("….")
Mark

Wilaya PointCom

unread,
May 12, 2013, 5:51:02 PM5/12/13
to dragonf...@googlegroups.com
Thanks !

Wilaya PointCom

unread,
May 18, 2013, 4:15:00 AM5/18/13
to dragonf...@googlegroups.com
For info, I also followed this source, and it works perfectly (see below code).
Nevertheless, I have a small question, I'm totally new in ruby and I'd like to pass the image size (here 500x375) in parameter.
How would I call it then ??
I tried : 
image_tag p.file.process(:convert_to_canvas_size(500x375)).url
But it didn't work ...


1. config/initializers/dragonfly.rb
require 'image_processor'
        app = Dragonfly[:images]
        app.processor.register(ImageProcessor)
2. image_processor.rb

class ImageProcessor
    def convert_to_canvas_size_500x375(temp_object)
        tempfile = new_tempfile
        args = " #{temp_object.path} -resize 500x375 -gravity center -background grey -extent 500x375 #{tempfile.path}"
        full_command = "convert #{args}"
        result = `#{full_command}`
        tempfile
    end
    
    private
    
    def new_tempfile(ext=nil)
        tempfile = ext ? Tempfile.new(['dragonfly', ".#{ext}"]) : Tempfile.new('dragonfly')
        tempfile.binmode
        tempfile.close
        tempfile
    end
end

3. call it :
image_tag p.file.process(:convert_to_canvas_size_500x375).url
Reply all
Reply to author
Forward
0 new messages