Created
March 28, 2019 07:49
-
-
Save himanshu-myupchar/02b6b50a5139610a5fe59742ca7fca2d to your computer and use it in GitHub Desktop.
Convert any image format to webp
This file contains hidden or 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
class Post | |
has_attached_file :asset, | |
:storage => :s3, | |
:processors => [:webp], | |
:url => ":s3_alias_url", | |
:path => ":id/:filename", | |
:s3_region => "your-region", | |
styles: { | |
webp: { | |
format: 'webp' | |
}, | |
original: [] | |
}, | |
only_process: [:webp], | |
:use_timestamp => false, | |
:s3_credentials => Proc.new { |a| bucket = 'bucketname';} | |
validates_attachment_content_type :asset, :content_type => ["image/jpeg", "image/png", "image/gif"] | |
end | |
module Paperclip | |
class Webp < Processor | |
def initialize file, options = {}, attachment = nil | |
super | |
@file = file | |
@attachment = attachment | |
@current_format = File.extname(@file.path) | |
@format = options[:format] | |
@basename = File.basename(@file.path, @current_format) | |
end | |
def make | |
temp_file = Tempfile.new([@basename, @format]) | |
temp_file.binmode | |
run_string = "convert #{fromfile} -quality 50 #{tofile(temp_file)}" | |
Paperclip.run(run_string) | |
temp_file | |
end | |
def fromfile | |
File.expand_path(@file.path) | |
end | |
def tofile(destination) | |
File.expand_path(destination.path) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment