Created
December 15, 2022 14:06
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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