Module: SafeImage::DiscourseCompat

Defined in:
lib/safe_image/discourse_compat.rb

Overview

Compatibility-shaped API for the operations Discourse currently performs in OptimizedImage, UploadCreator, ShrinkUploadedImage and FileHelper.

Class Method Summary collapse

Class Method Details

.animated?(path, max_pixels: nil) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/safe_image/discourse_compat.rb', line 220

def animated?(path, max_pixels: nil)
  frame_count(path, max_pixels: max_pixels).to_i > 1
end

.compat_backend_name(backend, info) ⇒ Object



192
193
194
195
# File 'lib/safe_image/discourse_compat.rb', line 192

def compat_backend_name(backend, info)
  base = backend.to_sym == :vips ? "libvips-direct" : "imagemagick"
  info[:encoder] == "cjpegli" ? "#{base}+cjpegli" : base
end

.compat_probe(path, backend:, max_pixels: nil) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/safe_image/discourse_compat.rb', line 247

def compat_probe(path, backend:, max_pixels: nil)
  path = Pathname.new(path).expand_path.to_s
  if backend.to_sym == :vips
    SafeImage.probe(path, max_pixels: max_pixels)
  else
    info = ImageMagickBackend.probe(path, max_pixels: max_pixels)
    Result.new(
      input: path,
      output: nil,
      input_format: info.fetch(:input_format),
      output_format: nil,
      width: info.fetch(:width),
      height: info.fetch(:height),
      filesize: File.size(path),
      backend: "imagemagick",
      duration_ms: info.fetch(:duration_ms),
      optimizer: nil
    )
  end
end

.convert(from, to, format:, quality: nil, optimize: true, max_pixels: nil, encoder: :auto, chroma_subsampling: :auto) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/safe_image/discourse_compat.rb', line 137

def convert(from, to, format:, quality: nil, optimize: true, max_pixels: nil, encoder: :auto, chroma_subsampling: :auto)
  probe = compat_probe(from, backend: :imagemagick, max_pixels: max_pixels)
  output = PathSafety.ensure_safe_output_path!(to).to_s
  normalized_format = format.to_s.downcase == "jpeg" ? "jpg" : format.to_s.downcase

  info =
    if use_jpegli_for_convert?(probe.input, normalized_format, encoder)
      JpegliBackend.convert(
        input: probe.input,
        output: output,
        quality: quality || JpegliBackend::DEFAULT_QUALITY,
        chroma_subsampling: chroma_subsampling
      )
    else
      if encoder.to_sym == :cjpegli
        raise UnsupportedFormatError, "cjpegli cannot directly encode #{File.extname(probe.input).delete_prefix(".").downcase.inspect}; use encoder: :auto or another encoder"
      end
      ImageMagickBackend.convert(input: probe.input, output: output, format: format, quality: quality)
    end

  Optimizer.optimize(output, mode: :lossless, strip_metadata: true, quality: normalized_format == "jpg" ? quality : nil) if optimize && info[:encoder] != "cjpegli"
  result_from_info(probe.input, output, info, info[:encoder] == "cjpegli" ? "cjpegli" : "imagemagick")
end

.convert_favicon_to_png(from, to, optimize: true, max_pixels: nil) ⇒ Object



208
209
210
211
212
213
214
# File 'lib/safe_image/discourse_compat.rb', line 208

def convert_favicon_to_png(from, to, optimize: true, max_pixels: nil)
  frame_count(from, max_pixels: max_pixels) if max_pixels
  output = PathSafety.ensure_safe_output_path!(to).to_s
  info = ImageMagickBackend.convert_ico_to_png(input: Pathname.new(from).expand_path.to_s, output: output)
  Optimizer.optimize(output, mode: :lossless, strip_metadata: true) if optimize
  result_from_info(from, output, info, "imagemagick")
end

.convert_to_jpeg(from, to, quality: nil, optimize: true, max_pixels: nil, encoder: :auto, chroma_subsampling: :auto) ⇒ Object



197
198
199
# File 'lib/safe_image/discourse_compat.rb', line 197

def convert_to_jpeg(from, to, quality: nil, optimize: true, max_pixels: nil, encoder: :auto, chroma_subsampling: :auto)
  convert(from, to, format: "jpg", quality: quality, optimize: optimize, max_pixels: max_pixels, encoder: encoder, chroma_subsampling: chroma_subsampling)
end

.crop(from, to, width, height, quality: nil, backend: :imagemagick, optimize: true, max_pixels: nil, encoder: :auto, chroma_subsampling: :auto) ⇒ Object



43
44
45
46
47
48
49
50
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
88
89
90
91
# File 'lib/safe_image/discourse_compat.rb', line 43

def crop(from, to, width, height, quality: nil, backend: :imagemagick, optimize: true, max_pixels: nil, encoder: :auto, chroma_subsampling: :auto)
  probe = compat_probe(from, backend: backend, max_pixels: max_pixels)
  output = PathSafety.ensure_safe_output_path!(to).to_s
  format = File.extname(output).delete_prefix(".").downcase

  info =
    if backend.to_sym == :vips && use_jpegli_for_generated_jpeg?(format, backend, encoder)
      with_temp_png(output) do |tmp_path|
        VipsBackend.crop_north(
          input: probe.input,
          output: tmp_path,
          width: width,
          height: height,
          format: "png",
          quality: 100,
          max_pixels: max_pixels
        )
        JpegliBackend.encode(
          input: tmp_path,
          output: output,
          quality: quality || JpegliBackend::DEFAULT_QUALITY,
          chroma_subsampling: JpegliBackend.validate_chroma_subsampling!(chroma_subsampling, input_format: probe.input_format),
          input_format: probe.input_format
        )
      end
    elsif backend.to_sym == :vips
      VipsBackend.crop_north(
        input: probe.input,
        output: output,
        width: width,
        height: height,
        format: format,
        quality: quality || 85,
        max_pixels: max_pixels
      )
    else
      ImageMagickBackend.resize_like(
        input: probe.input,
        output: output,
        width: width,
        height: height,
        format: format,
        quality: quality,
        crop: :north
      )
    end
  Optimizer.optimize(output, mode: :lossless, strip_metadata: true, quality: quality) if optimize
  result_from_info(probe.input, output, info, compat_backend_name(backend, info))
end

.downsize(from, to, dimensions, backend: :imagemagick, optimize: true, max_pixels: nil, quality: 85, encoder: :auto, chroma_subsampling: :auto) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/safe_image/discourse_compat.rb', line 93

def downsize(from, to, dimensions, backend: :imagemagick, optimize: true, max_pixels: nil, quality: 85, encoder: :auto, chroma_subsampling: :auto)
  probe = compat_probe(from, backend: backend, max_pixels: max_pixels)
  output = PathSafety.ensure_safe_output_path!(to).to_s
  format = File.extname(output).delete_prefix(".").downcase
  info =
    if backend.to_sym == :vips && use_jpegli_for_generated_jpeg?(format, backend, encoder)
      with_temp_png(output) do |tmp_path|
        VipsBackend.downsize(
          input: probe.input,
          output: tmp_path,
          dimensions: dimensions,
          format: "png",
          quality: 100,
          max_pixels: max_pixels
        )
        JpegliBackend.encode(
          input: tmp_path,
          output: output,
          quality: quality,
          chroma_subsampling: JpegliBackend.validate_chroma_subsampling!(chroma_subsampling, input_format: probe.input_format),
          input_format: probe.input_format
        )
      end
    elsif backend.to_sym == :vips
      VipsBackend.downsize(
        input: probe.input,
        output: output,
        dimensions: dimensions,
        format: format,
        quality: quality,
        max_pixels: max_pixels
      )
    else
      ImageMagickBackend.downsize(
        input: probe.input,
        output: output,
        dimensions: dimensions,
        format: format
      )
    end
  Optimizer.optimize(output, mode: :lossless, strip_metadata: true) if optimize
  result_from_info(probe.input, output, info, compat_backend_name(backend, info))
end

.fix_orientation(from, to = from, max_pixels: nil) ⇒ Object



201
202
203
204
205
206
# File 'lib/safe_image/discourse_compat.rb', line 201

def fix_orientation(from, to = from, max_pixels: nil)
  probe = compat_probe(from, backend: :imagemagick, max_pixels: max_pixels)
  output = PathSafety.ensure_safe_output_path!(to).to_s
  info = ImageMagickBackend.fix_orientation(input: probe.input, output: output)
  result_from_info(probe.input, output, info, "imagemagick")
end

.frame_count(path, max_pixels: nil) ⇒ Object



216
217
218
# File 'lib/safe_image/discourse_compat.rb', line 216

def frame_count(path, max_pixels: nil)
  ImageMagickBackend.frame_count(path, max_pixels: max_pixels)
end

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



224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/safe_image/discourse_compat.rb', line 224

def letter_avatar(output:, size:, background_rgb:, letter:, pointsize: 280, font: "NimbusSans-Regular")
  output = PathSafety.ensure_safe_output_path!(output).to_s
  info = ImageMagickBackend.letter_avatar(
    output: output,
    size: size,
    background_rgb: background_rgb,
    letter: letter,
    pointsize: pointsize,
    font: font
  )
  result_from_info("generated", output, info, "imagemagick")
end

.optimize_image!(path, allow_lossy_png: false, strip_metadata: true, quality: nil, strict: true) ⇒ Object



237
238
239
240
241
242
243
244
245
# File 'lib/safe_image/discourse_compat.rb', line 237

def optimize_image!(path, allow_lossy_png: false, strip_metadata: true, quality: nil, strict: true)
  Optimizer.optimize(
    path,
    mode: allow_lossy_png ? :lossy : :lossless,
    strip_metadata: ,
    quality: quality,
    strict: strict
  )
end

.resize(from, to, width, height, quality: nil, backend: :imagemagick, optimize: true, max_pixels: nil, encoder: :auto, chroma_subsampling: :auto) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/safe_image/discourse_compat.rb', line 13

def resize(from, to, width, height, quality: nil, backend: :imagemagick, optimize: true, max_pixels: nil, encoder: :auto, chroma_subsampling: :auto)
  if backend.to_sym == :vips
    return SafeImage.thumbnail(
      input: from,
      output: to,
      width: width,
      height: height,
      quality: quality || 85,
      backend: backend,
      optimize: optimize,
      max_pixels: max_pixels,
      encoder: encoder,
      chroma_subsampling: chroma_subsampling
    )
  end

  probe = compat_probe(from, backend: :imagemagick, max_pixels: max_pixels)
  output = PathSafety.ensure_safe_output_path!(to).to_s
  info = ImageMagickBackend.thumbnail(
    input: probe.input,
    output: output,
    width: width,
    height: height,
    format: File.extname(output).delete_prefix(".").downcase,
    quality: quality
  )
  Optimizer.optimize(output, mode: :lossless, strip_metadata: true, quality: quality) if optimize
  result_from_info(probe.input, output, info, "imagemagick")
end

.result_from_info(input, output, info, backend) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/safe_image/discourse_compat.rb', line 268

def result_from_info(input, output, info, backend)
  Result.new(
    input: input.to_s,
    output: output.to_s,
    input_format: info.fetch(:input_format),
    output_format: info.fetch(:output_format),
    width: info.fetch(:width),
    height: info.fetch(:height),
    filesize: File.size(output),
    backend: backend,
    duration_ms: info.fetch(:duration_ms),
    optimizer: nil
  )
end

.use_jpegli_for_convert?(input, normalized_format, encoder) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


161
162
163
164
165
166
167
168
# File 'lib/safe_image/discourse_compat.rb', line 161

def use_jpegli_for_convert?(input, normalized_format, encoder)
  encoder = encoder.to_sym
  return false unless normalized_format == "jpg"
  return false if encoder == :imagemagick
  raise ArgumentError, "unknown encoder: #{encoder.inspect}" unless %i[auto cjpegli].include?(encoder)
  return true if encoder == :cjpegli && JpegliBackend.suitable_direct_input?(input)
  encoder == :auto && JpegliBackend.available? && JpegliBackend.suitable_direct_input?(input)
end

.use_jpegli_for_generated_jpeg?(format, backend, encoder) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


170
171
172
173
174
175
176
177
178
# File 'lib/safe_image/discourse_compat.rb', line 170

def use_jpegli_for_generated_jpeg?(format, backend, encoder)
  encoder = encoder.to_sym
  normalized_format = format.to_s.downcase == "jpeg" ? "jpg" : format.to_s.downcase
  return false unless normalized_format == "jpg"
  return false if %i[vips imagemagick magick].include?(encoder)
  raise ArgumentError, "unknown encoder: #{encoder.inspect}" unless %i[auto cjpegli].include?(encoder)
  raise ArgumentError, "encoder: :cjpegli currently requires backend: :vips" if encoder == :cjpegli && backend.to_sym != :vips
  encoder == :cjpegli || (backend.to_sym == :vips && JpegliBackend.available?)
end

.with_temp_png(output) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/safe_image/discourse_compat.rb', line 180

def with_temp_png(output)
  output_path = Pathname.new(output)
  output_path.dirname.mkpath
  Tempfile.create([output_path.basename(".*").to_s, ".safe-image.png"], output_path.dirname.to_s) do |tmp|
    tmp_path = Pathname.new(tmp.path)
    tmp.close
    yield tmp_path
  ensure
    FileUtils.rm_f(tmp_path) if defined?(tmp_path) && tmp_path
  end
end