Skip to content

Instantly share code, notes, and snippets.

@adipasquale
Created December 15, 2022 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adipasquale/80458a76d7e2d98a4051ad3ac10cc486 to your computer and use it in GitHub Desktop.
Save adipasquale/80458a76d7e2d98a4051ad3ac10cc486 to your computer and use it in GitHub Desktop.
Monkey patch Active Storage Attachment to add a rotate method - with ImageProcessing and VIPS
# config/initializers/active_storage_attachment_rotation.rb
# frozen_string_literal: true
module Rotation
def rotate!(degrees: 90)
rotated_tempfile = nil
blob.open do |original_tempfile|
rotated_tempfile = ImageProcessing::Vips.source(original_tempfile).rotate(degrees).call
end
rotated_blob = ActiveStorage::Blob.create_and_upload!(
io: rotated_tempfile,
filename: filename
)
previous_blob = blob
update!(blob: rotated_blob)
previous_blob.purge_later
end
end
ActiveSupport.on_load(:active_storage_attachment) do
ActiveStorage::Attachment.include Rotation
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment