Skip to content

Commit f613ba8

Browse files
authored
Merge pull request rails#51480 from albus522/dg/improve-vips-analyzer
Be a lot more memory efficient analyzing images with ruby-vips
2 parents 30753c1 + 8d13216 commit f613ba8

File tree

1 file changed

+5
-9
lines changed
  • activestorage/lib/active_storage/analyzer/image_analyzer

1 file changed

+5
-9
lines changed

activestorage/lib/active_storage/analyzer/image_analyzer/vips.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ def read_image
1919

2020
download_blob_to_tempfile do |file|
2121
image = instrument("vips") do
22+
# ruby-vips will raise Vips::Error if it can't find an appropriate loader for the file
2223
::Vips::Image.new_from_file(file.path, access: :sequential)
24+
rescue ::Vips::Error
25+
logger.info "Skipping image analysis because Vips doesn't support the file"
26+
nil
2327
end
2428

25-
if valid_image?(image)
29+
if image
2630
yield image
2731
else
28-
logger.info "Skipping image analysis because Vips doesn't support the file"
2932
{}
3033
end
3134
rescue ::Vips::Error => error
@@ -40,12 +43,5 @@ def rotated_image?(image)
4043
rescue ::Vips::Error
4144
false
4245
end
43-
44-
def valid_image?(image)
45-
image.avg
46-
true
47-
rescue ::Vips::Error
48-
false
49-
end
5046
end
5147
end

0 commit comments

Comments
 (0)