Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save tonibergholm/571873 to your computer and use it in GitHub Desktop.

Select an option

Save tonibergholm/571873 to your computer and use it in GitHub Desktop.

Revisions

  1. @amasses amasses renamed this gist Oct 9, 2009. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @amasses amasses revised this gist Oct 9, 2009. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions app/controllers/gallery_images_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    class GalleryImagesController < ApplicationController
    def rotate
    @image = GalleryImage.find(params[:id])
    rotation = params[:deg].to_f
    rotation ||= 90 # Optional, otherwise, check for nil!

    @image.rotate!(rotation)
    flash[:notice] = "The image has been rotated"
    end

    end
  3. @amasses amasses revised this gist Oct 9, 2009. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  4. @amasses amasses created this gist Oct 9, 2009.
    35 changes: 35 additions & 0 deletions GalleryImage.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    class GalleryImage < ActiveRecord::Base
    belongs_to :property
    has_attached_file :image, :styles => {:small => "300x300#",
    :medium => "575x420>",
    :small_thumb => "95x95#",
    :thumb => "100x100#",
    :large => "550x550>",
    :featured => "296x477!"},
    :default_style => :small,
    :processors => [:rotator]

    attr_accessor :rotation_degrees, :rotate

    before_create :set_defaults


    def rotate!(degrees = 90)
    self.rotation += degrees
    self.rotation -= 360 if self.rotation >= 360
    self.rotation += 360 if self.rotation <= -360

    self.rotate = true
    self.image.reprocess!
    self.save
    end

    def rotating?
    !self.rotation.nil? and self.rotate
    end

    private
    def set_defaults
    self.rotation ||= 0
    end
    end
    18 changes: 18 additions & 0 deletions rotator.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    module Paperclip
    class Rotator < Thumbnail
    def transformation_command
    if rotate_command
    super + rotate_command
    else
    super
    end
    end

    def rotate_command
    target = @attachment.instance
    if target.rotating?
    " -rotate #{target.rotation}"
    end
    end
    end
    end