Module: SafeImage::ImageMagickBackend

Defined in:
lib/safe_image/image_magick_backend.rb

Constant Summary collapse

DEFAULT_PROFILE =
File.expand_path("RT_sRGB.icm", __dir__)
DECODERS =
{
  "jpg" => "jpeg",
  "jpeg" => "jpeg",
  "png" => "png",
  "gif" => "gif",
  "webp" => "webp",
  "heic" => "heic",
  "heif" => "heic",
  "avif" => "heic",
  "ico" => "ico",
  "jxl" => "jxl"
}.freeze
IMAGEMAGICK_LIMIT_ARGS =
[
  "-limit", "memory", "256MiB",
  "-limit", "map", "512MiB",
  "-limit", "disk", "1GiB",
  "-limit", "area", "128MP",
  "-limit", "time", "20",
  "-limit", "thread", "2"
].freeze
ALLOWED_FONTS =
%w[NimbusSans-Regular DejaVu-Sans Liberation-Sans Arial Helvetica Adwaita-Sans].freeze

Class Method Summary collapse

Class Method Details

.convert(input:, output:, format:, quality: nil, timeout: Runner::DEFAULT_TIMEOUT) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/safe_image/image_magick_backend.rb', line 111

def convert(input:, output:, format:, quality: nil, timeout: Runner::DEFAULT_TIMEOUT)
  command = convert_command
  input = PathSafety.ensure_imagemagick_input_file!(input)
  output = PathSafety.ensure_safe_output_path!(output).to_s
  output = PathSafety.ensure_imagemagick_safe!(output)
  ext = File.extname(input).delete_prefix(".").downcase
  decoder = DECODERS.fetch(ext) { raise UnsupportedFormatError, "unsupported ImageMagick input format: #{ext.inspect}" }
  normalized_format = format.to_s.downcase
  normalized_format = "jpg" if normalized_format == "jpeg"
  output_arg = output_spec(normalized_format, output)
  quality = validate_quality!(quality)

  argv = [command, *IMAGEMAGICK_LIMIT_ARGS, "#{decoder}:#{input}[0]", "-auto-orient", "-interlace", "none"]
  argv.concat(["-background", "white", "-flatten"]) if normalized_format == "jpg"
  argv.concat(["-quality", quality.to_s]) if quality
  argv << output_arg
  run_image_command(argv, output, ext, normalized_format, timeout)
end

.convert_commandObject



276
277
278
279
280
# File 'lib/safe_image/image_magick_backend.rb', line 276

def convert_command
  Runner.available?("magick") ? "magick" : Runner.resolve_executable!("convert") && "convert"
rescue UnsupportedFormatError
  raise UnsupportedFormatError, "ImageMagick convert/magick not available"
end

.convert_ico_to_png(input:, output:, timeout: Runner::DEFAULT_TIMEOUT) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/safe_image/image_magick_backend.rb', line 134

def convert_ico_to_png(input:, output:, timeout: Runner::DEFAULT_TIMEOUT)
  command = convert_command
  input = PathSafety.ensure_imagemagick_input_file!(input)
  output = PathSafety.ensure_safe_output_path!(output).to_s
  output = PathSafety.ensure_imagemagick_safe!(output)
  argv = [command, *IMAGEMAGICK_LIMIT_ARGS, "ico:#{input}[-1]", "-auto-orient", "-background", "transparent", output_spec("png", output)]
  run_image_command(argv, output, "ico", "png", timeout)
end

.convert_to_jpeg(input:, output:, quality: nil, timeout: Runner::DEFAULT_TIMEOUT) ⇒ Object



130
131
132
# File 'lib/safe_image/image_magick_backend.rb', line 130

def convert_to_jpeg(input:, output:, quality: nil, timeout: Runner::DEFAULT_TIMEOUT)
  convert(input: input, output: output, format: "jpg", quality: quality, timeout: timeout)
end

.dominant_color(path, timeout: Runner::DEFAULT_TIMEOUT) ⇒ Object

Averages the whole image down to one pixel and reports it as an RRGGBB hex string, mirroring Discourse’s Upload#calculate_dominant_color!.

Raises:



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/safe_image/image_magick_backend.rb', line 173

def dominant_color(path, timeout: Runner::DEFAULT_TIMEOUT)
  command = convert_command
  path = PathSafety.ensure_imagemagick_input_file!(path)
  ext = File.extname(path).delete_prefix(".").downcase
  decoder = DECODERS.fetch(ext) { raise UnsupportedFormatError, "unsupported ImageMagick input format: #{ext.inspect}" }
  stdout, = Runner.run!(
    [
      command, *IMAGEMAGICK_LIMIT_ARGS, "#{decoder}:#{path}[0]",
      "-depth", "8",
      "-resize", "1x1",
      "-define", "histogram:unique-colors=true",
      "-format", "%c",
      "histogram:info:"
    ],
    timeout: timeout
  )

  # Typical output: `1: (110,116,93) #6F745E srgb(110,116,93)`. Alpha adds
  # two more hex digits; grayscale images report one channel (two digits,
  # four with alpha) instead of three.
  digits = stdout[/#(\h+)/, 1]
  hex =
    case digits&.length
    when 6, 8 then digits[0, 6]
    when 2, 4 then digits[0, 2] * 3
    end
  raise InvalidImageError, "could not parse dominant color from ImageMagick output: #{stdout.strip.inspect}" if hex.nil?
  hex.upcase
end

.downsize(input:, output:, dimensions:, format:, timeout: Runner::DEFAULT_TIMEOUT) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/safe_image/image_magick_backend.rb', line 89

def downsize(input:, output:, dimensions:, format:, timeout: Runner::DEFAULT_TIMEOUT)
  command = convert_command

  input = PathSafety.ensure_imagemagick_input_file!(input)
  output = PathSafety.ensure_safe_output_path!(output).to_s
  output = PathSafety.ensure_imagemagick_safe!(output)
  ext = File.extname(input).delete_prefix(".").downcase
  decoder = DECODERS.fetch(ext) { raise UnsupportedFormatError, "unsupported ImageMagick input format: #{ext.inspect}" }
  dimensions = validate_dimensions!(dimensions)
  argv = [
    command, *IMAGEMAGICK_LIMIT_ARGS, "#{decoder}:#{input}[0]",
    "-auto-orient",
    "-gravity", "center",
    "-background", "transparent",
    "-interlace", "none",
    "-resize", dimensions,
  ]
  argv.concat(["-profile", DEFAULT_PROFILE]) if File.file?(DEFAULT_PROFILE)
  argv << output_spec(format, output)
  run_image_command(argv, output, ext, format, timeout)
end

.fix_orientation(input:, output: input, timeout: Runner::DEFAULT_TIMEOUT) ⇒ Object



228
229
230
231
232
233
234
235
236
237
# File 'lib/safe_image/image_magick_backend.rb', line 228

def fix_orientation(input:, output: input, timeout: Runner::DEFAULT_TIMEOUT)
  command = convert_command
  input = PathSafety.ensure_imagemagick_input_file!(input)
  output = PathSafety.ensure_safe_output_path!(output).to_s
  output = PathSafety.ensure_imagemagick_safe!(output)
  ext = File.extname(input).delete_prefix(".").downcase
  decoder = DECODERS.fetch(ext) { raise UnsupportedFormatError, "unsupported ImageMagick input format: #{ext.inspect}" }
  argv = [command, *IMAGEMAGICK_LIMIT_ARGS, "#{decoder}:#{input}[0]", "-auto-orient", output_spec(ext, output)]
  run_image_command(argv, output, ext, ext, timeout)
end

.frame_count(path, timeout: Runner::DEFAULT_TIMEOUT, max_pixels: nil) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/safe_image/image_magick_backend.rb', line 143

def frame_count(path, timeout: Runner::DEFAULT_TIMEOUT, max_pixels: nil)
  raise UnsupportedFormatError, "ImageMagick identify not available" unless Runner.available?("identify")
  path = PathSafety.ensure_imagemagick_input_file!(path)
  ext = File.extname(path).delete_prefix(".").downcase
  decoder = DECODERS.fetch(ext) { raise UnsupportedFormatError, "unsupported ImageMagick input format: #{ext.inspect}" }
  stdout, = Runner.run!(["identify", *IMAGEMAGICK_LIMIT_ARGS, "-ping", "-format", "%w %h %n\n", "#{decoder}:#{path}"], timeout: timeout)
  width, height, frames = stdout.each_line.first.to_s.split.map(&:to_i)
  if max_pixels && width.to_i * height.to_i > Integer(max_pixels)
    raise LimitError, "image has #{width * height} pixels, exceeds #{max_pixels}"
  end
  frames.to_i
end

.letter_avatar(output:, size:, background_rgb:, letter:, pointsize:, font: "NimbusSans-Regular", timeout: Runner::DEFAULT_TIMEOUT) ⇒ Object

Raises:

  • (ArgumentError)


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/safe_image/image_magick_backend.rb', line 203

def letter_avatar(output:, size:, background_rgb:, letter:, pointsize:, font: "NimbusSans-Regular", timeout: Runner::DEFAULT_TIMEOUT)
  command = convert_command
  output = PathSafety.ensure_safe_output_path!(output).to_s
  output = PathSafety.ensure_imagemagick_safe!(output)
  rgb = Array(background_rgb).map { |v| Integer(v) }
  raise ArgumentError, "background_rgb must have three channels" unless rgb.length == 3
  glyph = letter.to_s.each_grapheme_cluster.first.to_s.gsub("%", "%%")
  font_name = font.to_s
  raise ArgumentError, "unsupported font: #{font_name.inspect}" unless ALLOWED_FONTS.include?(font_name)

  argv = [
    command, *IMAGEMAGICK_LIMIT_ARGS,
    "-size", "#{Integer(size)}x#{Integer(size)}",
    "xc:rgb(#{rgb[0]},#{rgb[1]},#{rgb[2]})",
    "-pointsize", Integer(pointsize).to_s,
    "-fill", "#FFFFFFCC",
    "-font", font_name,
    "-gravity", "Center",
    "-annotate", "-0+34", glyph,
    "-depth", "8",
    output_spec("png", output)
  ]
  run_image_command(argv, output, "generated", "png", timeout)
end

.orientation(path, timeout: Runner::DEFAULT_TIMEOUT) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/safe_image/image_magick_backend.rb', line 156

def orientation(path, timeout: Runner::DEFAULT_TIMEOUT)
  raise UnsupportedFormatError, "ImageMagick identify not available" unless Runner.available?("identify")
  path = PathSafety.ensure_imagemagick_input_file!(path)
  ext = File.extname(path).delete_prefix(".").downcase
  decoder = DECODERS.fetch(ext) { raise UnsupportedFormatError, "unsupported ImageMagick input format: #{ext.inspect}" }
  stdout, = Runner.run!(
    ["identify", *IMAGEMAGICK_LIMIT_ARGS, "-ping", "-format", "%[EXIF:Orientation]", "#{decoder}:#{path}[0]"],
    timeout: timeout
  )
  value = stdout.to_s.strip
  value.empty? ? 1 : value.to_i
rescue CommandError
  1
end

.output_spec(format, output) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/safe_image/image_magick_backend.rb', line 239

def output_spec(format, output)
  ext = File.extname(output).delete_prefix(".").downcase
  ext = "jpg" if ext == "jpeg"
  normalized = format.to_s.downcase
  normalized = "jpg" if normalized == "jpeg"
  raise UnsupportedFormatError, "output extension #{ext.inspect} does not match format #{normalized.inspect}" unless ext == normalized

  coder = {
    "jpg" => "jpeg",
    "png" => "png",
    "gif" => "gif",
    "webp" => "webp",
    "avif" => "avif",
    "ico" => "ico",
    "jxl" => "jxl"
  }.fetch(normalized) { raise UnsupportedFormatError, "unsupported ImageMagick output format: #{normalized.inspect}" }
  "#{coder}:#{output}"
end

.probe(path, timeout: Runner::DEFAULT_TIMEOUT, max_pixels: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/safe_image/image_magick_backend.rb', line 32

def probe(path, timeout: Runner::DEFAULT_TIMEOUT, max_pixels: nil)
  raise UnsupportedFormatError, "ImageMagick identify not available" unless Runner.available?("identify")
  path = PathSafety.ensure_imagemagick_input_file!(path)
  ext = File.extname(path).delete_prefix(".").downcase
  decoder = DECODERS.fetch(ext) { raise UnsupportedFormatError, "unsupported ImageMagick input format: #{ext.inspect}" }
  stdout, = Runner.run!(["identify", *IMAGEMAGICK_LIMIT_ARGS, "-ping", "-format", "%m %w %h %n\n", "#{decoder}:#{path}"], timeout: timeout)
  _magick_format, width, height, frames = stdout.each_line.first.to_s.split
  width = width.to_i
  height = height.to_i
  if max_pixels && width * height > Integer(max_pixels)
    raise LimitError, "image has #{width * height} pixels, exceeds #{max_pixels}"
  end
  { input_format: ext == "jpeg" ? "jpg" : ext, width: width, height: height, frames: frames.to_i, duration_ms: 0.0 }
end

.resize_like(input:, output:, width:, height:, format:, quality:, crop: false, timeout: Runner::DEFAULT_TIMEOUT) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/safe_image/image_magick_backend.rb', line 51

def resize_like(input:, output:, width:, height:, format:, quality:, crop: false, timeout: Runner::DEFAULT_TIMEOUT)
  command = convert_command

  input = PathSafety.ensure_imagemagick_input_file!(input)
  output = PathSafety.ensure_safe_output_path!(output).to_s
  output = PathSafety.ensure_imagemagick_safe!(output)
  ext = File.extname(input).delete_prefix(".").downcase
  decoder = DECODERS.fetch(ext) { raise UnsupportedFormatError, "unsupported ImageMagick input format: #{ext.inspect}" }

  quality = validate_quality!(quality)
  argv = [command, *IMAGEMAGICK_LIMIT_ARGS, "#{decoder}:#{input}[0]", "-auto-orient"]
  if crop == :north
    argv.concat([
      "-gravity", "north",
      "-background", "transparent",
      "-thumbnail", "#{Integer(width)}x#{Integer(height)}^",
      "-crop", "#{Integer(width)}x#{Integer(height)}+0+0",
      "-unsharp", "2x0.5+0.7+0",
      "-interlace", "none"
    ])
  else
    argv.concat([
      "-gravity", "center",
      "-background", "transparent",
      "-thumbnail", "#{Integer(width)}x#{Integer(height)}^",
      "-extent", "#{Integer(width)}x#{Integer(height)}",
      "-interpolate", "catrom",
      "-unsharp", "2x0.5+0.7+0",
      "-interlace", "none"
    ])
  end
  argv.concat(["-profile", DEFAULT_PROFILE]) if File.file?(DEFAULT_PROFILE)
  argv.concat(["-quality", quality.to_s]) if quality
  argv << output_spec(format, output)

  run_image_command(argv, output, ext, format, timeout)
end

.run_image_command(argv, output, input_format, output_format, timeout) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/safe_image/image_magick_backend.rb', line 282

def run_image_command(argv, output, input_format, output_format, timeout)
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  Runner.run!(argv, timeout: timeout)
  duration_ms = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000

  # Output dimensions via the fast native header read, or identify when
  # libvips is not installed (this backend must work without it).
  info = VipsGlue.available? ? Native.probe(output) : probe(output)
  {
    input_format: input_format == "jpeg" ? "jpg" : input_format,
    output_format: output_format == "jpeg" ? "jpg" : output_format,
    width: info.fetch(:width),
    height: info.fetch(:height),
    duration_ms: duration_ms
  }
end

.thumbnail(input:, output:, width:, height:, format:, quality:, timeout: Runner::DEFAULT_TIMEOUT) ⇒ Object



47
48
49
# File 'lib/safe_image/image_magick_backend.rb', line 47

def thumbnail(input:, output:, width:, height:, format:, quality:, timeout: Runner::DEFAULT_TIMEOUT)
  resize_like(input: input, output: output, width: width, height: height, format: format, quality: quality, crop: :centre, timeout: timeout)
end

.validate_dimensions!(dimensions) ⇒ Object

Raises:

  • (ArgumentError)


265
266
267
268
269
270
271
272
273
274
# File 'lib/safe_image/image_magick_backend.rb', line 265

def validate_dimensions!(dimensions)
  dimensions = dimensions.to_s
  patterns = [
    /\A\d+(?:\.\d+)?%\z/,
    /\A\d+x\d+[!<>^]?\z/,
    /\A\d+@\z/
  ]
  raise ArgumentError, "unsupported ImageMagick geometry: #{dimensions.inspect}" unless patterns.any? { |pattern| pattern.match?(dimensions) }
  dimensions
end

.validate_quality!(quality) ⇒ Object

Raises:

  • (ArgumentError)


258
259
260
261
262
263
# File 'lib/safe_image/image_magick_backend.rb', line 258

def validate_quality!(quality)
  return nil if quality.nil?
  quality = Integer(quality)
  raise ArgumentError, "quality must be 1..100" unless (1..100).cover?(quality)
  quality
end