diff --git a/app/logical/media_file/image.rb b/app/logical/media_file/image.rb index cb0b75e9d..21a772f2b 100644 --- a/app/logical/media_file/image.rb +++ b/app/logical/media_file/image.rb @@ -10,9 +10,12 @@ class MediaFile::Image < MediaFile JPEG_OPTIONS = { Q: 90, background: 255, strip: true, interlace: true, optimize_coding: true } # http://jcupitt.github.io/libvips/API/current/libvips-resample.html#vips-thumbnail - if Vips.at_least_libvips?(8, 8) - THUMBNAIL_OPTIONS = { size: :down, linear: false, no_rotate: true, export_profile: SRGB_PROFILE, import_profile: SRGB_PROFILE } - CROP_OPTIONS = { crop: :attention, linear: false, no_rotate: true, export_profile: SRGB_PROFILE, import_profile: SRGB_PROFILE } + if Vips.at_least_libvips?(8, 10) + THUMBNAIL_OPTIONS = { size: :down, linear: false, no_rotate: true } + CROP_OPTIONS = { crop: :attention, linear: false, no_rotate: true } + elsif Vips.at_least_libvips?(8, 8) + THUMBNAIL_OPTIONS = { size: :down, linear: false, no_rotate: true, export_profile: "srgb" } + CROP_OPTIONS = { crop: :attention, linear: false, no_rotate: true, export_profile: "srgb" } else THUMBNAIL_OPTIONS = { size: :down, linear: false, auto_rotate: false, export_profile: SRGB_PROFILE, import_profile: SRGB_PROFILE } CROP_OPTIONS = { crop: :attention, linear: false, auto_rotate: false, export_profile: SRGB_PROFILE, import_profile: SRGB_PROFILE } @@ -35,6 +38,14 @@ class MediaFile::Image < MediaFile is_animated_gif? || is_animated_png? end + def channels + image.bands + end + + def colorspace + image.interpretation + end + # @see https://github.com/jcupitt/libvips/wiki/HOWTO----Image-shrinking # @see http://jcupitt.github.io/libvips/API/current/Using-vipsthumbnail.md.html def preview(width, height) diff --git a/test/files/test-cmyk-no-profile.jpg b/test/files/test-cmyk-no-profile.jpg new file mode 100644 index 000000000..e9609986e Binary files /dev/null and b/test/files/test-cmyk-no-profile.jpg differ diff --git a/test/files/test-grey-no-profile.jpg b/test/files/test-grey-no-profile.jpg new file mode 100644 index 000000000..255b45cb7 Binary files /dev/null and b/test/files/test-grey-no-profile.jpg differ diff --git a/test/unit/media_file_test.rb b/test/unit/media_file_test.rb index b02432fd2..9d3e4dced 100644 --- a/test/unit/media_file_test.rb +++ b/test/unit/media_file_test.rb @@ -179,4 +179,34 @@ class MediaFileTest < ActiveSupport::TestCase assert_equal(false, MediaFile.open("test/files/test-300x300.mp4").has_audio?) end end + + context "a greyscale image without an embedded color profile" do + should "successfully generate a thumbnail" do + @image = MediaFile.open("test/files/test-grey-no-profile.jpg") + @preview = @image.preview(150, 150) + + assert_equal(1, @image.channels) + assert_equal(:"b-w", @image.colorspace) + assert_equal([535, 290], @image.dimensions) + + assert_equal(3, @preview.channels) + assert_equal(:srgb, @preview.colorspace) + assert_equal([150, 81], @preview.dimensions) + end + end + + context "a CMYK image without an embedded color profile" do + should "successfully generate a thumbnail" do + @image = MediaFile.open("test/files/test-cmyk-no-profile.jpg") + @preview = @image.preview(150, 150) + + assert_equal(4, @image.channels) + assert_equal(:cmyk, @image.colorspace) + assert_equal([197, 256], @image.dimensions) + + assert_equal(3, @preview.channels) + assert_equal(:srgb, @preview.colorspace) + assert_equal([115, 150], @preview.dimensions) + end + end end