Last active
April 19, 2019 07:26
-
-
Save karasugawasu/cd2d073f2eab92f7feedbbe296edd099 to your computer and use it in GitHub Desktop.
Mastodon 画像の変換とサイズ変更
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
# lib/paperclip/img_converter.rb | |
# こちらのインスタンスより・・ https://github.com/imas/mastodon/blob/imastodon/lib/paperclip/img_converter.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
# app/models/media_attachment.rb | |
# 参考 https://github.com/imas/mastodon | |
# 35行目あたり | |
IMAGE_STYLES = { | |
original: { | |
geometry: '1920x1920>', | |
file_geometry_parser: FastGeometryParser, | |
}, | |
#112行目あたり | |
before_create :prepare_description, unless: :local? | |
before_create :set_shortcode | |
before_post_process :set_type, :set_extension | |
after_post_process :set_extension | |
before_save :set_meta | |
#148行目あたり | |
def file_processors(f) | |
if f.file_content_type == 'image/gif' | |
[:gif_transcoder] | |
elsif f.file_content_type == 'image/png' | |
[:img_converter, :thumbnail] | |
elsif VIDEO_MIME_TYPES.include? f.file_content_type | |
[:video_transcoder] | |
else | |
[:thumbnail] | |
end | |
end | |
#178行目あたり | |
def set_type | |
self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : :image | |
end | |
def set_extension | |
extension = appropriate_extension(file) | |
basename = Paperclip::Interpolations.basename(file, :original) | |
file.instance_write :file_name, [basename, extension].delete_if(&:blank?).join('.') | |
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
// app/javascript/mastodon/utils/resize_image.js | |
//1920まで許可 | |
const MAX_IMAGE_DIMENSION = 1920; | |
//processImage 最後 | |
//pngだったら問答無用でjpegに v2.4.0rc3 | |
if (type === 'image/png') { | |
canvas.toBlob(resolve, 'image/jpeg',1); | |
}else{ | |
canvas.toBlob(resolve, type); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment