image - Rails paperclip crop save ratio -
how save ratio while crop image?
i use paperclip , image style blog_article_main
940x400x works ok.
but while crop image ratio changed.
my cropper
module paperclip class cropper < thumbnail def transformation_command if crop_command crop_command + super.join(' ').sub(/ -crop \s+/, '').split(' ') else super end end def crop_command target = @attachment.instance if @attachment if target.cropping? ["-crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}"] end end end end
my js
javascript: $(function () { $('#cropbox').jcrop({ onchange: update_crop, onselect: update_crop, bgcolor: 'black', bgopacity: .2, setselect: [100, 100, 400, 400], //aspectratio: 16 / 9, maxsize:[940,400] }); }); function update_crop(coords) { var rx = 100/coords.w; var ry = 100/coords.h; $('#preview').css({ width: math.round(rx * "#{@photo.photo_geometry(:blog_article_main).width}" ) + 'px', height: math.round(ry * "{#@photo.photo_geometry(:blog_article_main).height}" ) + 'px', marginleft: '-' + math.round(rx * coords.x) + 'px', margintop: '-' + math.round(ry * coords.y) + 'px' }); var ratio = "#{@photo.photo_geometry(:original).width}" / "#{@photo.photo_geometry(:blog_article_main).width}" ; $("#crop_x").val(math.round(coords.x * ratio)); $("#crop_y").val(math.round(coords.y * ratio)); $("#crop_w").val(math.round(coords.w * ratio)); $("#crop_h").val(math.round(coords.h * ratio)); }
and after image has cropped ratio changed
def photo_geometry(style = :original) @geometry ||= {} @geometry[style] ||= paperclip::geometry.from_file(self.image(style)) end @photo.photo_geometry(:blog_article_main)=> 940x851
Comments
Post a Comment