summaryrefslogtreecommitdiff
path: root/lib/booru/image_processing/jpeg_processor.rb
diff options
context:
space:
mode:
authorAppleDash <[email protected]>2020-08-07 19:27:33 -0400
committerAppleDash <[email protected]>2020-08-07 19:27:33 -0400
commit07c9e6fcf2ae7b0e22c2333ebb3ffe6a5e3a1d3c (patch)
treebec48d0f2665921e62d5fb17deacc439305ac1b6 /lib/booru/image_processing/jpeg_processor.rb
parent32a3f9346b8b39ab5b506019629fb416c86f2228 (diff)
Rewrite image processing engine pretty majorly
Diffstat (limited to 'lib/booru/image_processing/jpeg_processor.rb')
-rw-r--r--lib/booru/image_processing/jpeg_processor.rb38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/booru/image_processing/jpeg_processor.rb b/lib/booru/image_processing/jpeg_processor.rb
new file mode 100644
index 0000000..2fab4da
--- /dev/null
+++ b/lib/booru/image_processing/jpeg_processor.rb
@@ -0,0 +1,38 @@
+require 'shellwords'
+
+module Booru
+ module ImageProcessing
+ class JpegProcessor < Processor
+ include ShellHelper
+
+ def pre_process
+ # Perform lossy rotation on image if it has EXIF orientation field
+ # (web browsers do not respect that, see http://caniuse.com/#search=image-orientation)
+ image = ::MiniMagick::Image.open(@original_path)
+ orientation = image.exif['Orientation']
+ if orientation.to_i > 1 # 1 is the default orientation
+ image.auto_orient
+ image.strip # remove EXIF metadata to prevent image from being rotated again
+ image.write(@original_path)
+ end
+ end
+
+ def generate_version(dimensions, destination_path)
+ width, height = dimensions
+
+ # resize JPEG to desired size and jpegtran the resized version.
+ ::MiniMagick::Tool::Convert.new do |cmd|
+ cmd.resize "#{width}x#{height}"
+ cmd << @original_path
+ cmd << destination_path
+ end
+
+ jpegtran(destination_path)
+ end
+
+ def post_process
+ jpegtran(@original_path)
+ end
+ end
+ end
+end \ No newline at end of file