From a9f3655932207448299af9d42450675a9044cc4a Mon Sep 17 00:00:00 2001 From: Marc Doerflinger Date: Sun, 30 Jun 2024 20:15:50 +0000 Subject: [PATCH 1/4] add vips dependencies (#94) --- .devcontainer/Dockerfile | 2 +- Dockerfile | 2 +- Gemfile | 2 ++ Gemfile.lock | 1 + app/controllers/recipe_images_controller.rb | 15 +++++++++------ 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 3927733a..19f603df 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -13,7 +13,7 @@ RUN if [ "${INSTALL_NODE}" = "true" ]; then su vscode -c "umask 0002 && . /usr/l # [Optional] Uncomment this section to install additional OS packages. RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ - && apt-get -y install --no-install-recommends tesseract-ocr tesseract-ocr-deu libleptonica-dev imagemagick postgresql-client + && apt-get -y install --no-install-recommends tesseract-ocr tesseract-ocr-deu libleptonica-dev imagemagick postgresql-client libvips42 # [Optional] Uncomment this line to install global node packages. # RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g " 2>&1 diff --git a/Dockerfile b/Dockerfile index f3932133..37b77476 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,7 @@ ADD .ruby-version $HOME/ ADD Gemfile* $HOME/ RUN apk update && apk upgrade && \ - apk add --update --no-cache nodejs yarn build-base libxml2-dev tzdata postgresql-dev && \ + apk add --update --no-cache nodejs yarn build-base libxml2-dev tzdata postgresql-dev vips && \ rm -rf /var/cache/apk/* # # speed up install of nokogiri gem diff --git a/Gemfile b/Gemfile index 5149c858..0b3f2144 100644 --- a/Gemfile +++ b/Gemfile @@ -90,3 +90,5 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] #gem 'mailgun-ruby', '~>1.1.6' #gem 'devise', '~> 4.4', '>= 4.4.1' #gem 'bulma-rails', '~>0.6.2' + +gem "ruby-vips", "~> 2.2" diff --git a/Gemfile.lock b/Gemfile.lock index 45f6a57f..0000c108 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -458,6 +458,7 @@ DEPENDENCIES rails (~> 7.1.2) rspec-rails rtesseract (~> 3.1) + ruby-vips (~> 2.2) selenium-webdriver solargraph spring diff --git a/app/controllers/recipe_images_controller.rb b/app/controllers/recipe_images_controller.rb index 2fe7dedd..0338babf 100644 --- a/app/controllers/recipe_images_controller.rb +++ b/app/controllers/recipe_images_controller.rb @@ -1,6 +1,8 @@ +require 'vips' + class RecipeImagesController < ApplicationController include Secured - + def new @recipe = Recipe.find(params[:recipe_id]) @page_title = @recipe.name @@ -17,11 +19,12 @@ def create params[:image].each{ |image| if image.content_type == "image/jpeg" # downgrade image quality to 60 to reduce size of image - img = MiniMagick::Image.read(image.read) - img.quality(60) - tmpfile = Tempfile.new('img') - img.write(tmpfile) - tmpfile.close + # img = MiniMagick::Image.read(image.read) + # img.quality(60) + # tmpfile = Tempfile.new('img') + # img.write(tmpfile) + # tmpfile.close + im = Vips::Image.new_from_file "./10x10.tif" # "./10x10.jpg" recipe.recipe_images.attach( io: tmpfile.open, From 9c10c8a81e95bf14b705d1b6605cabb9a07d77bb Mon Sep 17 00:00:00 2001 From: Marc Doerflinger Date: Sun, 30 Jun 2024 20:54:17 +0000 Subject: [PATCH 2/4] switch image quality downscale to vips (#94) --- app/controllers/recipe_images_controller.rb | 18 +++++++++--------- config/environments/development.rb | 5 +++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/app/controllers/recipe_images_controller.rb b/app/controllers/recipe_images_controller.rb index 0338babf..9c1e9c04 100644 --- a/app/controllers/recipe_images_controller.rb +++ b/app/controllers/recipe_images_controller.rb @@ -18,18 +18,18 @@ def create params[:image].each{ |image| if image.content_type == "image/jpeg" - # downgrade image quality to 60 to reduce size of image - # img = MiniMagick::Image.read(image.read) - # img.quality(60) - # tmpfile = Tempfile.new('img') - # img.write(tmpfile) - # tmpfile.close - im = Vips::Image.new_from_file "./10x10.tif" # "./10x10.jpg" + # convert jpeg to webp using vips + tmpfile = Tempfile.new('img', :encoding => 'ascii-8bit') + im = Vips::Image.new_from_buffer image.read, "" + tmpfile.write(im.webpsave_buffer(Q: 60)) + tmpfile.close + + webp_filename = image.original_filename.sub(/\.jpg$/, '.webp').sub(/\.jpeg$/, '.webp') recipe.recipe_images.attach( io: tmpfile.open, - filename: image.original_filename, - content_type: image.content_type + filename: webp_filename, + content_type: 'image/webp' ) tmpfile.unlink else diff --git a/config/environments/development.rb b/config/environments/development.rb index 5959da16..ae5221c2 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -1,5 +1,5 @@ require "active_support/core_ext/integer/time" - + Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. @@ -42,7 +42,8 @@ end # Store uploaded files on the local file system (see config/storage.yml for options). - config.active_storage.service = :amazondev + # config.active_storage.service = :amazondev + config.active_storage.service = :local # Don't care if the mailer can't send. config.action_mailer.raise_delivery_errors = false From cf3f46214aa4a5a8dcda4ced61e7c760fdfd0a6a Mon Sep 17 00:00:00 2001 From: Marc Doerflinger Date: Sun, 30 Jun 2024 21:10:34 +0000 Subject: [PATCH 3/4] switch to libvips (#94) --- app/views/ocr/show.html.erb | 2 +- app/views/recipe_images/delete_select.html.erb | 2 +- app/views/recipes/show.html.erb | 4 ++-- config/initializers/active_storage.rb | 1 + config/initializers/new_framework_defaults_7_0.rb | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 config/initializers/active_storage.rb diff --git a/app/views/ocr/show.html.erb b/app/views/ocr/show.html.erb index 8c373db1..71b03c3e 100644 --- a/app/views/ocr/show.html.erb +++ b/app/views/ocr/show.html.erb @@ -12,7 +12,7 @@ data-ocr-selection-ocrurl="<%= ocr_index_path(:format => :json) %>" > <% @recipe.recipe_images.each do |image| %> - <%= image_tag image.variant(resize: "100x100"), class: 'image_inline border-2 border-solid border-transparent', data: { 'imgid': image.id, 'imgurl': rails_blob_path(image), action: 'click->ocr-selection#select' } %> + <%= image_tag image.variant(resize_to_fit: [100,100]), class: 'image_inline border-2 border-solid border-transparent', data: { 'imgid': image.id, 'imgurl': rails_blob_path(image), action: 'click->ocr-selection#select' } %> <% end %>
<%= render :partial => 'imgregion', :locals => { image: @recipe.recipe_images.first } %> diff --git a/app/views/recipe_images/delete_select.html.erb b/app/views/recipe_images/delete_select.html.erb index 8fcb6263..25b3a76c 100644 --- a/app/views/recipe_images/delete_select.html.erb +++ b/app/views/recipe_images/delete_select.html.erb @@ -10,7 +10,7 @@

<% @recipe.recipe_images.each do |image| %> - <%= image_tag image.variant(resize: "100x100"), class: 'image_inline border-2 border-solid border-transparent', data: { 'filename': image.filename, action: 'click->recipeimagesdelete#select' } %> + <%= image_tag image.variant(resize_to_fit: [100,100]), class: 'image_inline border-2 border-solid border-transparent', data: { 'filename': image.filename, action: 'click->recipeimagesdelete#select' } %> <% end %>
<%= form_tag(delete_recipe_recipe_images_path(@recipe), method: :put, data: { 'recipeimagesdelete-target': 'form'}) do %> diff --git a/app/views/recipes/show.html.erb b/app/views/recipes/show.html.erb index 66016149..734786f1 100644 --- a/app/views/recipes/show.html.erb +++ b/app/views/recipes/show.html.erb @@ -62,8 +62,8 @@
<% @recipe.recipe_images.each do |image| %> <% end %> diff --git a/config/initializers/active_storage.rb b/config/initializers/active_storage.rb new file mode 100644 index 00000000..7635d143 --- /dev/null +++ b/config/initializers/active_storage.rb @@ -0,0 +1 @@ +Rails.application.config.active_storage.variant_processor = :vips diff --git a/config/initializers/new_framework_defaults_7_0.rb b/config/initializers/new_framework_defaults_7_0.rb index 08f1266c..9fcdcc58 100644 --- a/config/initializers/new_framework_defaults_7_0.rb +++ b/config/initializers/new_framework_defaults_7_0.rb @@ -80,7 +80,7 @@ # generate variants to use image processing macros and ruby-vips # operations. See the upgrading guide for detail on the changes required. # The `:mini_magick` option is not deprecated; it's fine to keep using it. -Rails.application.config.active_storage.variant_processor = :mini_magick +# Rails.application.config.active_storage.variant_processor = :mini_magick # If you're upgrading and haven't set `cookies_serializer` previously, your cookie serializer # was `:marshal`. Convert all cookies to JSON, using the `:hybrid` formatter. From fcb6edc09dc225c7861b432857d63ff529359a12 Mon Sep 17 00:00:00 2001 From: Marc Doerflinger Date: Sun, 30 Jun 2024 21:35:04 +0000 Subject: [PATCH 4/4] autorotate uploaded image (#94) --- app/controllers/recipe_images_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/recipe_images_controller.rb b/app/controllers/recipe_images_controller.rb index 9c1e9c04..c1251eb5 100644 --- a/app/controllers/recipe_images_controller.rb +++ b/app/controllers/recipe_images_controller.rb @@ -21,6 +21,7 @@ def create # convert jpeg to webp using vips tmpfile = Tempfile.new('img', :encoding => 'ascii-8bit') im = Vips::Image.new_from_buffer image.read, "" + im = im.autorot tmpfile.write(im.webpsave_buffer(Q: 60)) tmpfile.close