-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2020 from tf/webp
Add experimental webp support
- Loading branch information
Showing
25 changed files
with
299 additions
and
65 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
json.call(image_file, :width, :height) | ||
json.processed_extension image_file.output_present?(:webp) ? 'webp' : 'JPG' | ||
json.created_at image_file.created_at.try(:utc).try(:iso8601, 0) |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
de: | ||
pageflow: | ||
webp_images: | ||
feature_name: "webp Bilder" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
en: | ||
pageflow: | ||
webp_images: | ||
feature_name: "webp images" |
5 changes: 5 additions & 0 deletions
5
db/migrate/20231024062501_add_output_presences_to_image_files.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddOutputPresencesToImageFiles < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :pageflow_image_files, :output_presences, :text | ||
end | ||
end |
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require 'vips' | ||
|
||
module Pageflow | ||
module PaperclipProcessors | ||
# @api private | ||
class Webp < Paperclip::Processor | ||
ANIMATED_FORMATS = %w[.gif].freeze | ||
|
||
def initialize(file, options = {}, attachment = nil) | ||
super | ||
|
||
geometry = options[:geometry].to_s | ||
@should_crop = geometry[-1, 1] == '#' | ||
|
||
@target_geometry = Paperclip::Geometry.parse(geometry) | ||
@whiny = options.fetch(:whiny, true) | ||
|
||
@current_format = File.extname(file.path) | ||
@basename = File.basename(@file.path, @current_format) | ||
end | ||
|
||
def make | ||
source = @file | ||
filename = [@basename, '.webp'].join | ||
destination = Paperclip::TempfileFactory.new.generate(filename) | ||
|
||
begin | ||
thumbnail = Vips::Image.thumbnail( | ||
ANIMATED_FORMATS.include?(@current_format) ? "#{source.path}[n=-1]" : source.path, | ||
width, | ||
size: :down, | ||
height: height, | ||
crop: crop | ||
) | ||
thumbnail.webpsave(destination.path) | ||
rescue Vips::Error => e | ||
if @whiny | ||
message = "There was an error processing the thumbnail for #{@basename}:\n" + e.message | ||
raise Paperclip::Error, message | ||
end | ||
end | ||
|
||
destination | ||
end | ||
|
||
private | ||
|
||
def crop | ||
return unless @should_crop | ||
|
||
@options[:crop] || :centre | ||
end | ||
|
||
def width | ||
@target_geometry.width | ||
end | ||
|
||
def height | ||
@target_geometry.height | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.