Skip to content

Commit

Permalink
Merge pull request #480 from doerfli/feature/vips
Browse files Browse the repository at this point in the history
Feature/vips
  • Loading branch information
doerfli authored Jul 4, 2024
2 parents f8734e1 + fcb6edc commit 252b55c
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 <your-package-here>" 2>&1
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ DEPENDENCIES
rails (~> 7.1.2)
rspec-rails
rtesseract (~> 3.1)
ruby-vips (~> 2.2)
selenium-webdriver
solargraph
spring
Expand Down
20 changes: 12 additions & 8 deletions app/controllers/recipe_images_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'vips'

class RecipeImagesController < ApplicationController
include Secured

def new
@recipe = Recipe.find(params[:recipe_id])
@page_title = @recipe.name
Expand All @@ -16,17 +18,19 @@ 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)
# 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

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
Expand Down
2 changes: 1 addition & 1 deletion app/views/ocr/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
<div>
<%= render :partial => 'imgregion', :locals => { image: @recipe.recipe_images.first } %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/recipe_images/delete_select.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</p>
<div class="not-prose">
<% @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 %>
</div>
<%= form_tag(delete_recipe_recipe_images_path(@recipe), method: :put, data: { 'recipeimagesdelete-target': 'form'}) do %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/recipes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
<div class="recipe_images not-prose">
<% @recipe.recipe_images.each do |image| %>
<div class="image">
<a class="not-prose" data-action="click->recipe#imageLgShow" data-imagefullurl="<%= url_for(image) %>" data-imagelgurl="<%= url_for(image.variant(resize: "800x2400")) %>">
<%= image_tag(image.variant(resize: "100x100")) %>
<a class="not-prose" data-action="click->recipe#imageLgShow" data-imagefullurl="<%= url_for(image) %>" data-imagelgurl="<%= url_for(image.variant(resize_to_fit: [800,2400])) %>">
<%= image_tag(image.variant(resize_to_fit: [100,100])) %>
</a>
</div>
<% end %>
Expand Down
5 changes: 3 additions & 2 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/initializers/active_storage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rails.application.config.active_storage.variant_processor = :vips
2 changes: 1 addition & 1 deletion config/initializers/new_framework_defaults_7_0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 252b55c

Please sign in to comment.