Add support for WebP and HEIC formats through conversion
This commit is contained in:
parent
40db10417c
commit
138d5972dd
3 changed files with 42 additions and 3 deletions
|
@ -170,8 +170,10 @@ const resizeImage = (img, type = 'image/png') => new Promise((resolve, reject) =
|
|||
.catch(reject);
|
||||
});
|
||||
|
||||
const WITHOUT_RESIZING_FORMATS = ['image/gif', 'image/heic', 'image/heif'];
|
||||
|
||||
export default inputFile => new Promise((resolve) => {
|
||||
if (!inputFile.type.match(/image.*/) || inputFile.type === 'image/gif') {
|
||||
if (!inputFile.type.match(/image.*/) || WITHOUT_RESIZING_FORMATS.indexOf(inputFile.type) >= 0) {
|
||||
resolve(inputFile);
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ class MediaAttachment < ApplicationRecord
|
|||
|
||||
MAX_DESCRIPTION_LENGTH = 1_500
|
||||
|
||||
IMAGE_FILE_EXTENSIONS = %w(.jpg .jpeg .png .gif).freeze
|
||||
IMAGE_FILE_EXTENSIONS = %w(.jpg .jpeg .png .gif .webp .heif .heic).freeze
|
||||
VIDEO_FILE_EXTENSIONS = %w(.webm .mp4 .m4v .mov).freeze
|
||||
AUDIO_FILE_EXTENSIONS = %w(.ogg .oga .mp3 .wav .flac .opus .aac .m4a .3gp .wma).freeze
|
||||
|
||||
|
@ -47,7 +47,8 @@ class MediaAttachment < ApplicationRecord
|
|||
small
|
||||
).freeze
|
||||
|
||||
IMAGE_MIME_TYPES = %w(image/jpeg image/png image/gif).freeze
|
||||
IMAGE_MIME_TYPES = %w(image/jpeg image/png image/gif image/webp image/heif image/heic).freeze
|
||||
IMAGE_CONVERTIBLE_MIME_TYPES = %w(image/webp image/heif image/heic).freeze
|
||||
VIDEO_MIME_TYPES = %w(video/webm video/mp4 video/quicktime video/ogg).freeze
|
||||
VIDEO_CONVERTIBLE_MIME_TYPES = %w(video/webm video/quicktime).freeze
|
||||
AUDIO_MIME_TYPES = %w(audio/wave audio/wav audio/x-wav audio/x-pn-wave audio/ogg audio/mpeg audio/mp3 audio/webm audio/flac audio/aac audio/m4a audio/x-m4a audio/mp4 audio/3gpp video/x-ms-asf).freeze
|
||||
|
@ -290,6 +291,8 @@ class MediaAttachment < ApplicationRecord
|
|||
[:transcoder, :blurhash_transcoder, :type_corrector]
|
||||
elsif AUDIO_MIME_TYPES.include?(instance.file_content_type)
|
||||
[:image_extractor, :transcoder, :type_corrector]
|
||||
elsif IMAGE_CONVERTIBLE_MIME_TYPES.include?(instance.file_content_type)
|
||||
[:img_converter, :lazy_thumbnail, :blurhash_transcoder, :type_corrector]
|
||||
else
|
||||
[:lazy_thumbnail, :blurhash_transcoder, :type_corrector]
|
||||
end
|
||||
|
|
34
lib/paperclip/img_converter.rb
Normal file
34
lib/paperclip/img_converter.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Paperclip
|
||||
class ImgConverter < Paperclip::Processor
|
||||
def initialize(file, options = {}, attachment = nil)
|
||||
super
|
||||
|
||||
@current_format = File.extname(@file.path)
|
||||
@basename = File.basename(@file.path, @current_format)
|
||||
end
|
||||
|
||||
def make
|
||||
dst_format, dst_content_type = opaque? ? ['jpg', 'image/jpeg'] : ['png', 'image/png']
|
||||
dst_name = "#{@basename}.#{dst_format}"
|
||||
|
||||
attachment.instance.file_file_name = dst_name
|
||||
attachment.instance.file_content_type = dst_content_type
|
||||
|
||||
options[:format] = dst_format
|
||||
options[:content_type] = dst_content_type
|
||||
|
||||
dst = Paperclip::TempfileFactory.new.generate(dst_name)
|
||||
convert(':src :dst', src: File.expand_path(@file.path), dst: File.expand_path(dst.path))
|
||||
|
||||
dst
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def opaque?
|
||||
identify('-format "%[opaque]" :src', src: File.expand_path(@file.path)).strip.downcase == 'true'
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue