Sign in through GitHub

Please read for an updated status on RailsCasts:

Learn more or hide this

Elsopuro's Profile

GitHub User: Elsopuro

Comments by

Avatar

How to reprocess image for a style only i need?
Because i need manualy crop image for thumb4 and thumb6 ?

Model

ruby
class Post < ActiveRecord::Base
  
  attr_accessor :crop_x, :crop_y, :crop_w, :crop_h, :crop_style
  
  after_update :reprocess_image, :if => :cropping?

  has_attached_file :image, :styles => {
    :thumb1 => ["300x200#", :jpg],
    :thumb4 => { :geometry => "270x340#",  :processors => [:cropper]},
    :thumb5 => ["50x50#", :jpg],
    :thumb6 => { :geometry => "740x276#",  :processors => [:cropper]}
  }


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

  def image_geometry(style = :original)
    @geometry ||= {}
    @geometry[style] ||= Paperclip::Geometry.from_file(image.path(style))
  end

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

end

Copper

ruby
module Paperclip
  class Cropper < Thumbnail
    def transformation_command
      if crop_command && crop_this_style?
        puts ">>>>> CROP THIS STYLE "
         crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ')
      elsif crop_command && !crop_this_style?
        puts ">>>>> NO CORP"
      else
        puts ">>>>> STANDART CROP"
        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

    def crop_this_style?
      @attachment.instance.crop_style.present? && (@attachment.options[:styles][@attachment.instance.crop_style.to_sym][:geometry].to_s == @target_geometry.to_s)
    end

  end
end

Slim template

ruby
- selected_style = params[:selected_style].to_sym

- content_for(:head) do
  = stylesheet_link_tag "jquery.Jcrop"
  = javascript_include_tag "jquery.Jcrop.min"
  javascript:
    $(function() {
      $('#cropbox').Jcrop({
        onChange: update_crop,
        onSelect: update_crop,
        setSelect: [0, 0, #{@post.image_geometry(selected_style).width}, #{@post.image_geometry(selected_style).height}],
        aspectRatio: #{@post.image_geometry(selected_style).width}/#{@post.image_geometry(selected_style).height}
      });
    });

    function update_crop(coords) {
      var rx = #{@post.image_geometry(selected_style).width}/coords.w;
      var ry = #{@post.image_geometry(selected_style).height}/coords.h;
      $('#preview').css({
        width: Math.round(rx * #{@post.image_geometry(:real).width}) + 'px',
        height: Math.round(ry * #{@post.image_geometry(:real).height}) + 'px',
        marginLeft: '-' + Math.round(rx * coords.x) + 'px',
        marginTop: '-' + Math.round(ry * coords.y) + 'px'
      });
      var ratio = #{@post.image_geometry(:original).width} / #{@post.image_geometry(:real).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));
    }


div style="width:#{@post.image_geometry(selected_style).width}px; height:#{@post.image_geometry(selected_style).height}px; overflow:hidden"
  = image_tag @post.image.url(:real), :id => "preview"

= image_tag @post.image.url(:real), :id => "cropbox"

= form_for @post, :url => admin_post_path(@post), :method => :put do |f|
  - for attribute in [:crop_x, :crop_y, :crop_w, :crop_h]
    = f.hidden_field attribute, :id => attribute
    br
  = f.hidden_field :crop_style, :value => selected_style
  p
    = f.submit