2016-05-19 11 views
0

Gibt es eine Möglichkeit, neue Dimensionen auf my_image zu setzen und unter AWS S3 zu speichern?Schienen neue Abmessungen einstellen Büroklammer

Ich habe

my_image = Post.last.photo.image 

geometry = Paperclip::Geometry.from_file(my_image.url) 

so möchte ich neue Geometrie setzen und es

geometry = 'my params' 
my_image.save 

Natürlich habe ich meine

has_attached_file :image, :styles => { :large => "220x" ..other styles} 

Mein Hauptziel setzen neue Dimensionen beim Zuschneiden sparen Foto mit Jcrop

so, wenn ich neue params erhalten wie my_params =>"crop_x"=>"83", "crop_y"=>"24", "crop_w"=>"76", "crop_h"=>"76" Set es als neue Art innerhalb has_attached_file

Antwort

1

Sie können so leicht tun,

Declare Attribut Accessoren für die Ernte Dimensionen

attr_accessor :crop_x, :crop_y, :crop_w, :crop_h 

Dann sind Sie Überschreiben Sie den Prozessor, erstellen Sie cropper.rb in /lib/paperclip_processors und fügen Sie folgenden Code

module Paperclip 
    class Cropper < Thumbnail 
    def transformation_command 
     if crop_command 
     crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ') # super returns an array like this: ["-resize", "100x", "-crop", "100x100+0+0", "+repage"] 
     else 
     super 
     end 
    end 

    def crop_command 
     target = @attachment.instance 
     if target.cropping? 
     ["-crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}"] 
     end 
    end 
    end 
end 

Jetzt im Modell können Sie diese

has_attached_file :image, styles: { large: "800X800>" }, default_url: "/images/:style/missing.png", :processors => [:cropper] 
after_save :reprocess_image, if: :cropping? 

def cropping? 
    !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank? 
end 

def reprocess_image 
    image.assign(image) 
    image.save 
end 

Hoffnung tun, das hilft!

+0

aber wie kann ich es während meiner '@ post' Aktualisierung tun? , cuz Bildaktualisierung innerhalb meines post.it verschachtelten Formulars – user