Skip to content

Commit

Permalink
Use RMagick for resizing images
Browse files Browse the repository at this point in the history
For some reason, builds suddenly took a really long time when running the
convert command. RMagick seems to work better.
  • Loading branch information
aalin committed Dec 11, 2023
1 parent 3cfd1db commit 51816ea
Show file tree
Hide file tree
Showing 6 changed files with 3,392 additions and 69 deletions.
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ PATH
rack (>= 3.0.4.1, < 3.0.9.0)
rake (~> 13.0.6)
rbnacl (~> 7.1.1)
rmagick (~> 5.3.0)
rouge (~> 4.0.0)
sorbet-runtime (~> 0.5.10634)
source_map (~> 3.0.1)
Expand Down Expand Up @@ -120,6 +121,7 @@ GEM
parser (3.2.2.3)
ast (~> 2.4.1)
racc
pkg-config (1.5.6)
prettier (4.0.2)
syntax_tree (>= 4.0.1)
syntax_tree-haml (>= 2.0.0)
Expand Down Expand Up @@ -151,6 +153,8 @@ GEM
ffi
rbs (3.1.0)
rexml (3.2.5)
rmagick (5.3.0)
pkg-config (~> 1.4)
rouge (4.0.1)
ruby-prof (1.6.3)
ruby-progressbar (1.13.0)
Expand Down
8 changes: 6 additions & 2 deletions example/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ ARG BUNDLE_PATH=vendor/bundle
ENV BUNDLE_PATH ${BUNDLE_PATH}
ENV BUNDLE_WITHOUT ${BUNDLE_WITHOUT}
RUN gem install -N bundler -v ${BUNDLER_VERSION}
RUN apk update && apk add --no-cache curl bash jemalloc gcompat
RUN apk update && apk add --no-cache \
curl bash jemalloc gcompat libsodium \
imagemagick imlib2
SHELL ["/bin/bash", "-c"]
WORKDIR /app

FROM base AS install
RUN apk update && apk add --no-cache build-base gzip libwebp-tools imagemagick brotli libsodium
RUN apk update && apk add --no-cache \
build-base gzip brotli \
pkgconfig imagemagick imagemagick-dev imlib2 imlib2-dev
COPY Gemfile* .
RUN bundle install
COPY . .
Expand Down
74 changes: 7 additions & 67 deletions lib/mayu/resources/generators/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,78 +27,18 @@ def initialize(source_path, version)
def process(target_path)
return if File.exist?(target_path)

require "rmagick"

Console.logger.info(
self,
"Generating #{target_path} from #{@source_path}"
)

case @version.format
when :webp
convert_webp(
source_path: @source_path,
quality: 80, # typical quality,
width: @version.width,
target_path: target_path
)
else
convert_generic(
source_path: @source_path,
quality: 80, # typical quality,
width: @version.width,
target_path: target_path
)
end
end

private

sig do
params(
source_path: String,
quality: Integer,
width: Integer,
target_path: String
).void
end
def convert_webp(source_path:, quality:, width:, target_path:)
system(
Shellwords.shelljoin(
[
"cwebp",
"-q",
"#{quality}",
"-resize",
"#{width}",
"0",
source_path,
"-o",
target_path
]
) + " 2> /dev/null"
) or raise "Could not generate #{target_path} from #{source_path}"
end

sig do
params(
source_path: String,
quality: Integer,
width: Integer,
target_path: String
).void
end
def convert_generic(source_path:, quality:, width:, target_path:)
system(
Shellwords.shelljoin(
[
"convert",
source_path,
"-adaptive-resize",
"#{width}",
"-strip",
target_path
]
) + " 2> /dev/null"
) or raise "Could not generate #{target_path} from #{source_path}"
Magick::Image
.read(@source_path)
.first
.resize_to_fit(@version.width)
.write(target_path) { |options| options.quality = 80 }
end
end
end
Expand Down
1 change: 1 addition & 0 deletions mayu-live.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "kramdown", "~> 2.4.0"
spec.add_dependency "rouge", "~> 4.0.0"
spec.add_dependency "mayu-css", "~> 0.0.3"
spec.add_dependency "rmagick", "~> 5.3.0"
spec.add_dependency "source_map", "~> 3.0.1"
spec.add_dependency "syntax_tree", "~> 5.3.0"
spec.add_dependency "syntax_tree-haml", "~> 3.0.0"
Expand Down
Loading

0 comments on commit 51816ea

Please sign in to comment.