class Image < ActiveRecord::Base include ImageUploader[:image]
acts_as_paranoid
belongs_to :imageable, polymorphic: trueend
require "image_processing/mini_magick"
class ImageUploader < Shrine include ImageProcessing::MiniMagick
process(:store) do |io, context| if cropping_params_present?(context) cropped = crop(io.download, context[:crop_width], context[:crop_height], context[:crop_x], context[:crop_y], gravity: "NorthWest") size_400 = resize_to_limit(cropped, 400, 400) size_200 = resize_to_limit(size_400, 200, 200)
{cropped_original: cropped, medium: size_400, small: size_200} else {original: io} end end
def after_upload(io, context) Pusher.trigger("processing_complete_signal", 'processing_complete', { message: 'ok' }) end
def cropping_params_present?(context) if context[:crop_x] && context[:crop_y] && context[:crop_width] && context[:crop_height] true else false end endend
<div id="image-wrapper"> <%= render 'users/image', user: @user %></div><%= form_for user, :url => update_avatar_user_path, :html => { :multipart => true, :class => "directUpload" }, remote: true do |f| %> <%= f.fields_for :image do |img| %> <%= img.hidden_field :image, value: img.object.cached_image_data %> <%= img.file_field :image, name: "file" %> <% end %> <div id="progress" class="progress"> <div class="progress-bar progress-bar-success progress-bar-striped"></div> </div> <%= f.submit %><% end %>
<div id="image-wrapper"> <%= render 'users/image', user: @user %></div><%= form_for user, :url => update_avatar_user_path, :html => { :multipart => true, :class => "directUpload" } do |f| %> <%= f.fields_for :image do |img| %> <%= img.hidden_field :crop_width, id: 'crop_width' %> <%= img.hidden_field :crop_height, id: 'crop_height' %> <%= img.hidden_field :crop_x, id: 'crop_x' %> <%= img.hidden_field :crop_y, id: 'crop_y' %> <%= img.hidden_field :image %> <% end %> <div id="progress" class="progress"> <div class="progress-bar progress-bar-success progress-bar-striped"></div> </div> <%= f.submit %><% end %>
<script>
$(function() { var progress_bar = $('.progress-bar'); var form = $('.directUpload'); $('.directUpload input:file').fileupload({ add: function(e, data) { var options = { extension: data.files[0].name.match(/(\.\w+)?$/)[0], // set extension _: Date.now(), // prevent caching } $.getJSON('/images/cache/presign', options, function(result) { data.formData = result['fields']; data.url = result['url']; data.submit(); }); }, progress: function(e, data) { var progress = parseInt(data.loaded / data.total * 100, 10); var percentage = progress.toString() + '%' progress_bar.css("width", percentage).html(percentage); }, done: function(e, data) { progress_bar.text("Nahrávání hotovo"); var image_attrs = { id: data.formData.key.match(/cache\/(.+)/)[1], // we have to remove the prefix part storage: 'cache', metadata: { size: data.files[0].size, filename: data.files[0].name.match(/[^\/\\]+$/)[0], // IE returns full path mime_type: data.files[0].type } }
var image_data = $("<input />", { type:'hidden', name: 'user[image_attributes][image]', value: image_attrs }) form.append(image_data);
$("input[name='file']").remove(); $(".directUpload").submit(); } }); });
</script>
--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/bb7c5d07-84be-494d-8199-c27cfca4048e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.