Skip to content

Commit d2bbebc

Browse files
committed
feat(AssetFieldType): implement version image optimization, remove version format conversion feature, add support for SVGs
1 parent 59afa03 commit d2bbebc

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

app/cells/plugins/core/asset_info/show.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- if asset_is_image?
99
.mdl-card__title
1010
%h2.mdl-card__title-text
11-
= image_tag(asset['versions']['original']['url'], style: 'max-height: 200px;')
11+
= image_tag(asset['versions']['original']['url'], style: 'min-height: 50px; max-height: 200px;')
1212
.mdl-card__supporting-text
1313
%dl
1414
%dt Original Filename

app/uploaders/asset_uploader.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'image_processing/mini_magick'
2+
require 'image_optim'
23

34
class AssetUploader < Shrine
45
include ImageProcessing::MiniMagick
@@ -25,14 +26,14 @@ class AssetUploader < Shrine
2526
end
2627

2728
process(:store) do |io, context|
28-
# TODO: Perform image optimizations (build plugin), support versions without processors or formatters
29+
# TODO: support versions without processors
2930
context[:generated_hex] = SecureRandom.hex(8)
3031
versions = { original: io.download }
3132

3233
if image?(io)
3334
versions.merge!(context[:config][:metadata][:versions].transform_values do |version|
3435
processed_version = send("#{version[:process][:method]}!", io.download, *version[:process][:config].values)
35-
convert!(processed_version, version[:format])
36+
optimize_image!(processed_version)
3637
end)
3738
end
3839

@@ -52,4 +53,10 @@ def generate_location(io, context)
5253
def image?(io)
5354
MimeMagic.new(io.data['metadata']['mime_type']).mediatype == 'image'
5455
end
56+
57+
def optimize_image!(image)
58+
image_optim = ImageOptim.new(pngout: false, svgo: false, allow_lossy: true) # TODO: implement per-Field/version image_optim configuration
59+
pathname = image_optim.optimize_image!(image)
60+
pathname ? pathname.open : image
61+
end
5562
end

lib/tasks/cortex/core/media.rake

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ namespace :cortex do
3333
title: fieldTitle.id
3434
},
3535
versions: {
36-
large: { process: { method: 'resize_to_limit', config: { width: '1800', height: '1800' } }, format: :jpg },
37-
medium: { process: { method: 'resize_to_limit', config: { width: '800', height: '800' } }, format: :jpg },
38-
default: { process: { method: 'resize_to_limit', config: { width: '300', height: '300' } }, format: :jpg },
39-
mini: { process: { method: 'resize_to_limit', config: { width: '100', height: '100' } }, format: :jpg },
40-
micro: { process: { method: 'resize_to_limit', config: { width: '50', height: '50' } }, format: :jpg },
36+
large: { process: { method: 'resize_to_limit', config: { width: '1800', height: '1800' } } },
37+
medium: { process: { method: 'resize_to_limit', config: { width: '800', height: '800' } } },
38+
default: { process: { method: 'resize_to_limit', config: { width: '300', height: '300' } } },
39+
mini: { process: { method: 'resize_to_limit', config: { width: '100', height: '100' } } },
40+
micro: { process: { method: 'resize_to_limit', config: { width: '50', height: '50' } } },
41+
rss: { process: { method: 'resize_to_limit', config: { width: '840', height: '840' } } },
4142
},
4243
keep_files: [:destroyed, :replaced],
4344
path: 'media/<%= attachment %>/<%= original_name %>-<%= style %>-<%= generated_hex %>.<%= extension %>',

0 commit comments

Comments
 (0)