Skip to content

Commit 9d72e46

Browse files
committed
Support resize-on-load for resize to cover
We can now rename it back to `#resize_to_cover`, since vips backend requires all `resize_to_*` operations to support resize-on-load.
1 parent c2a9a73 commit 9d72e46

File tree

6 files changed

+40
-38
lines changed

6 files changed

+40
-38
lines changed

doc/minimagick.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ the [MiniMagick] gem (which is installed with the image_processing gem).
1515
* [`#resize_to_fit`](#resize_to_fit)
1616
* [`#resize_to_fill`](#resize_to_fill)
1717
* [`#resize_and_pad`](#resize_and_pad)
18-
* [`#cover`](#cover)
18+
* [`#resize_to_cover`](#resize_to_cover)
1919
* [`#crop`](#crop)
2020
* [`#rotate`](#rotate)
2121
* [`#composite`](#composite)
@@ -190,15 +190,15 @@ It accepts `:gravity` for specifying the [gravity] to apply while cropping
190190
pipeline.resize_and_pad!(400, 400, gravity: "north-west")
191191
```
192192

193-
#### `#cover`
193+
#### `#resize_to_cover`
194194

195195
Resizes the image to cover the specified dimensions while retaining the
196196
original aspect ratio. The overflowing areas will not be cropped.
197197

198198
```rb
199199
pipeline = ImageProcessing::MiniMagick.source(image) # 600x800
200200

201-
result = pipeline.cover!(300, 300)
201+
result = pipeline.resize_to_cover!(300, 300)
202202

203203
MiniMagick::Image.new(result.path).dimensions #=> [300, 400]
204204
```

doc/vips.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The `ImageProcessing::Vips` module contains processing macros that use the
1414
* [`#resize_to_fit`](#resize_to_fit)
1515
* [`#resize_to_fill`](#resize_to_fill)
1616
* [`#resize_and_pad`](#resize_and_pad)
17-
* [`#cover`](#cover)
17+
* [`#resize_to_cover`](#resize_to_cover)
1818
* [`#crop`](#crop)
1919
* [`#rotate`](#rotate)
2020
* [`#composite`](#composite)
@@ -221,23 +221,23 @@ pipeline.resize_to_fill!(400, 400, linear: true)
221221

222222
See [`vips_thumbnail()`] and [`vips_gravity()`] for more details.
223223

224-
#### `#cover`
224+
#### `#resize_to_cover`
225225

226226
Resizes the image to cover the specified dimensions while retaining the
227227
original aspect ratio. The overflowing areas will not be cropped.
228228

229229
```rb
230230
pipeline = ImageProcessing::Vips.source(image) # 600x800
231231

232-
result = pipeline.cover!(300, 300)
232+
result = pipeline.resize_to_cover!(300, 300)
233233

234234
Vips::Image.new_from_file(result.path).size #=> [300, 400]
235235
```
236236

237237
Any additional options (except `crop`) are forwarded to [`Vips::Image#thumbnail_image`]:
238238

239239
```rb
240-
pipeline.cover!(400, 400, linear: true)
240+
pipeline.resize_to_cover!(400, 400, linear: true)
241241
```
242242

243243
See [`vips_thumbnail()`] for more details.

lib/image_processing/mini_magick.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def resize_and_pad(width, height, background: :transparent, gravity: "Center", *
8888

8989
# Resizes the image to cover the specified dimensions, without
9090
# cropping the excess.
91-
def cover(width, height, **options)
91+
def resize_to_cover(width, height, **options)
9292
thumbnail("#{width}x#{height}^", **options)
9393
end
9494

lib/image_processing/vips.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ def resize_and_pad(width, height, gravity: "centre", extend: nil, background: ni
9292

9393
# Resizes the image to cover the specified dimensions, without
9494
# cropping the excess.
95-
def cover(width, height, **options)
95+
def resize_to_cover(width, height, **options)
96+
image = self.image.is_a?(String) ? ::Vips::Image.new_from_file(self.image) : self.image
97+
9698
image_ratio = Rational(image.width, image.height)
9799
thumbnail_ratio = Rational(width, height)
98100

test/mini_magick_test.rb

+13-13
Original file line numberDiff line numberDiff line change
@@ -367,57 +367,57 @@
367367
end
368368
end
369369

370-
describe "#cover" do
370+
describe "#resize_to_cover" do
371371
before do
372372
@portrait_pipeline = ImageProcessing::MiniMagick.source(@portrait)
373373
@landscape_pipeline = ImageProcessing::MiniMagick.source(@landscape)
374374
@square_pipeline = ImageProcessing::MiniMagick.source(@square)
375375
end
376376

377377
it "resizes the portrait image to fill out the given landscape dimensions" do
378-
assert_dimensions [300, 400], @portrait_pipeline.cover!(300, 200)
378+
assert_dimensions [300, 400], @portrait_pipeline.resize_to_cover!(300, 200)
379379
end
380380

381381
it "resizes the portrait image to fill out the given portrait dimensions" do
382-
assert_dimensions [225, 300], @portrait_pipeline.cover!(200, 300)
382+
assert_dimensions [225, 300], @portrait_pipeline.resize_to_cover!(200, 300)
383383
end
384384

385385
it "resizes the portrait image to fill out the given square dimensions" do
386-
assert_dimensions [300, 400], @portrait_pipeline.cover!(300, 300)
386+
assert_dimensions [300, 400], @portrait_pipeline.resize_to_cover!(300, 300)
387387
end
388388

389389
it "resizes the landscape image to fill out the given portrait dimensions" do
390-
assert_dimensions [400, 300], @landscape_pipeline.cover!(200, 300)
390+
assert_dimensions [400, 300], @landscape_pipeline.resize_to_cover!(200, 300)
391391
end
392392

393393
it "resizes the landscape image to fill out the given landscape dimensions" do
394-
assert_dimensions [300, 225], @landscape_pipeline.cover!(300, 200)
394+
assert_dimensions [300, 225], @landscape_pipeline.resize_to_cover!(300, 200)
395395
end
396396

397397
it "resizes the landscape image to fill out the given square dimensions" do
398-
assert_dimensions [400, 300], @landscape_pipeline.cover!(300, 300)
398+
assert_dimensions [400, 300], @landscape_pipeline.resize_to_cover!(300, 300)
399399
end
400400

401401
it "resizes the square image to fill out the given portrait dimensions" do
402-
assert_dimensions [300, 300], @square_pipeline.cover!(200, 300)
402+
assert_dimensions [300, 300], @square_pipeline.resize_to_cover!(200, 300)
403403
end
404404

405405
it "resizes the square image to fill out the given landscape dimensions" do
406-
assert_dimensions [300, 300], @square_pipeline.cover!(300, 200)
406+
assert_dimensions [300, 300], @square_pipeline.resize_to_cover!(300, 200)
407407
end
408408

409409
it "resizes the square image to fill out the given square dimensions" do
410-
assert_dimensions [300, 300], @square_pipeline.cover!(300, 300)
410+
assert_dimensions [300, 300], @square_pipeline.resize_to_cover!(300, 300)
411411
end
412412

413413
it "produces correct image" do
414414
expected = fixture_image("cover.jpg")
415-
assert_similar expected, @portrait_pipeline.cover!(300, 200)
415+
assert_similar expected, @portrait_pipeline.resize_to_cover!(300, 200)
416416
end
417417

418418
it "accepts sharpening options" do
419-
sharpened = @portrait_pipeline.cover!(400, 400, sharpen: { sigma: 1 })
420-
normal = @portrait_pipeline.cover!(400, 400, sharpen: false)
419+
sharpened = @portrait_pipeline.resize_to_cover!(400, 400, sharpen: { sigma: 1 })
420+
normal = @portrait_pipeline.resize_to_cover!(400, 400, sharpen: false)
421421
assert sharpened.size > normal.size, "Expected sharpened thumbnail to have bigger filesize than not sharpened thumbnail"
422422
end
423423
end

test/vips_test.rb

+16-16
Original file line numberDiff line numberDiff line change
@@ -323,68 +323,68 @@
323323
end
324324
end
325325

326-
describe "#cover" do
326+
describe "#resize_to_cover" do
327327
before do
328328
@portrait_pipeline = ImageProcessing::Vips.source(@portrait)
329329
@landscape_pipeline = ImageProcessing::Vips.source(@landscape)
330330
@square_pipeline = ImageProcessing::Vips.source(@square)
331331
end
332332

333333
it "resizes the portrait image to fill out the given landscape dimensions" do
334-
assert_dimensions [300, 400], @portrait_pipeline.cover!(300, 200)
334+
assert_dimensions [300, 400], @portrait_pipeline.resize_to_cover!(300, 200)
335335
end
336336

337337
it "resizes the portrait image to fill out the given portrait dimensions" do
338-
assert_dimensions [225, 300], @portrait_pipeline.cover!(200, 300)
338+
assert_dimensions [225, 300], @portrait_pipeline.resize_to_cover!(200, 300)
339339
end
340340

341341
it "resizes the portrait image to fill out the given square dimensions" do
342-
assert_dimensions [300, 400], @portrait_pipeline.cover!(300, 300)
342+
assert_dimensions [300, 400], @portrait_pipeline.resize_to_cover!(300, 300)
343343
end
344344

345345
it "resizes the landscape image to fill out the given portrait dimensions" do
346-
assert_dimensions [400, 300], @landscape_pipeline.cover!(200, 300)
346+
assert_dimensions [400, 300], @landscape_pipeline.resize_to_cover!(200, 300)
347347
end
348348

349349
it "resizes the landscape image to fill out the given landscape dimensions" do
350-
assert_dimensions [300, 225], @landscape_pipeline.cover!(300, 200)
350+
assert_dimensions [300, 225], @landscape_pipeline.resize_to_cover!(300, 200)
351351
end
352352

353353
it "resizes the landscape image to fill out the given square dimensions" do
354-
assert_dimensions [400, 300], @landscape_pipeline.cover!(300, 300)
354+
assert_dimensions [400, 300], @landscape_pipeline.resize_to_cover!(300, 300)
355355
end
356356

357357
it "resizes the square image to fill out the given portrait dimensions" do
358-
assert_dimensions [300, 300], @square_pipeline.cover!(200, 300)
358+
assert_dimensions [300, 300], @square_pipeline.resize_to_cover!(200, 300)
359359
end
360360

361361
it "resizes the square image to fill out the given landscape dimensions" do
362-
assert_dimensions [300, 300], @square_pipeline.cover!(300, 200)
362+
assert_dimensions [300, 300], @square_pipeline.resize_to_cover!(300, 200)
363363
end
364364

365365
it "resizes the square image to fill out the given square dimensions" do
366-
assert_dimensions [300, 300], @square_pipeline.cover!(300, 300)
366+
assert_dimensions [300, 300], @square_pipeline.resize_to_cover!(300, 300)
367367
end
368368

369369
it "produces correct image" do
370370
expected = fixture_image("cover.jpg")
371-
assert_similar expected, @portrait_pipeline.cover!(300, 200)
371+
assert_similar expected, @portrait_pipeline.resize_to_cover!(300, 200)
372372
end
373373

374374
it "accepts thumbnail options except :crop" do
375-
attention = @portrait_pipeline.cover!(400, 400, crop: :attention)
376-
centre = @portrait_pipeline.cover!(400, 400, crop: :centre)
375+
attention = @portrait_pipeline.resize_to_cover!(400, 400, crop: :attention)
376+
centre = @portrait_pipeline.resize_to_cover!(400, 400, crop: :centre)
377377
assert_similar centre, attention
378378
end
379379

380380
it "accepts sharpening options" do
381-
sharpened = @portrait_pipeline.cover!(400, 400, sharpen: ImageProcessing::Vips::Processor::SHARPEN_MASK)
382-
normal = @portrait_pipeline.cover!(400, 400, sharpen: false)
381+
sharpened = @portrait_pipeline.resize_to_cover!(400, 400, sharpen: ImageProcessing::Vips::Processor::SHARPEN_MASK)
382+
normal = @portrait_pipeline.resize_to_cover!(400, 400, sharpen: false)
383383
assert sharpened.size > normal.size, "Expected sharpened thumbnail to have bigger filesize than not sharpened thumbnail"
384384
end
385385

386386
it "sharpening uses integer precision" do
387-
sharpened = @portrait_pipeline.cover(400, 400).call(save: false)
387+
sharpened = @portrait_pipeline.resize_to_cover(400, 400).call(save: false)
388388
assert_equal :uchar, sharpened.format
389389
end
390390
end

0 commit comments

Comments
 (0)