Skip to content

Commit e1bc3af

Browse files
authored
Merge pull request #419 from deluxetom/3.x-cover
Use `cover` method from Intervention
2 parents 35631a1 + 57b2e31 commit e1bc3af

File tree

7 files changed

+133
-44
lines changed

7 files changed

+133
-44
lines changed

docs/3.0/api/crop.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ title: Crop
55

66
# Crop
77

8-
## Fit `fit=crop`
8+
## Fit `fit=cover` - Crop to cover the dimensions
99

1010
Resizes the image to fill the width and height boundaries and crops any excess image data. The resulting image will match the width and height constraints without distorting the image.
1111

1212
~~~ html
13-
<img src="kayaks.jpg?w=300&h=300&fit=crop">
13+
<img src="kayaks.jpg?w=300&h=300&fit=cover">
1414
~~~
1515

1616
[![© Photo Joel Reynolds](https://glide.herokuapp.com/1.0/kayaks.jpg?w=300&h=300&fit=crop)](https://glide.herokuapp.com/1.0/kayaks.jpg?w=300&h=300&fit=crop)
1717

1818
### Crop Position
1919

20-
You can also set where the image is cropped by adding a crop position. Accepts `crop-top-left`, `crop-top`, `crop-top-right`, `crop-left`, `crop-center`, `crop-right`, `crop-bottom-left`, `crop-bottom` or `crop-bottom-right`. Default is `crop-center`, and is the same as `crop`.
20+
You can also set where the image is cropped by adding a crop position. Accepts `cover-top-left`, `cover-top`, `cover-top-right`, `cover-left`, `cover-center`, `cover-right`, `cover-bottom-left`, `cover-bottom` or `cover-bottom-right`. Default is `cover-center`, and is the same as `crop`.
2121

2222
~~~ html
23-
<img src="kayaks.jpg?w=300&h=300&fit=crop-left">
23+
<img src="kayaks.jpg?w=300&h=300&fit=cover-left">
2424
~~~
2525

26-
### Crop Focal Point
26+
## Fit `fit=crop-x%-y%` - Crop based on Focal Point
2727

2828
In addition to the crop position, you can be more specific about the exact crop position using a focal point. This is defined using two offset percentages: `crop-x%-y%`.
2929

@@ -45,4 +45,4 @@ Crops the image to specific dimensions prior to any other resize operations. Req
4545
<img src="kayaks.jpg?crop=100,100,915,155">
4646
~~~
4747

48-
[![© Photo Joel Reynolds](https://glide.herokuapp.com/1.0/kayaks.jpg?crop=100,100,915,155)](https://glide.herokuapp.com/1.0/kayaks.jpg?crop=100,100,915,155)
48+
[![© Photo Joel Reynolds](https://glide.herokuapp.com/1.0/kayaks.jpg?crop=100,100,915,155)](https://glide.herokuapp.com/1.0/kayaks.jpg?crop=100,100,915,155)

docs/3.0/api/size.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ Sets the height of the image, in pixels.
2929

3030
Sets how the image is fitted to its target dimensions.
3131

32-
### Accepts:
32+
### Accepts:
3333

3434
- `contain`: Default. Resizes the image to fit within the width and height boundaries without cropping, distorting or altering the aspect ratio.
35-
- `max`: Resizes the image to fit within the width and height boundaries without cropping, distorting or altering the aspect ratio, and will also not increase the size of the image if it is smaller than the output size.
35+
- `max`: Resizes the image to fit within the width and height boundaries without cropping, distorting or altering the aspect ratio, and will also not increase the size of the image if it is smaller than the output size.
3636
- `fill`: Resizes the image to fit within the width and height boundaries without cropping or distorting the image, and the remaining space is filled with the background color. The resulting image will match the constraining dimensions.
3737
- `fill-max`: Resizes the image to fit within the width and height boundaries without cropping but upscaling the image if it's smaller. The finished image will have remaining space on either width or height (except if the aspect ratio of the new image is the same as the old image). The remaining space will be filled with the background color. The resulting image will match the constraining dimensions.
3838
- `stretch`: Stretches the image to fit the constraining dimensions exactly. The resulting image will fill the dimensions, and will not maintain the aspect ratio of the input image.
39-
- `crop`: Resizes the image to fill the width and height boundaries and crops any excess image data. The resulting image will match the width and height constraints without distorting the image. See the [crop](api/crop/) page for more information.
39+
- `cover`: Resizes the image to fill the width and height boundaries and crops any excess image data. The resulting image will match the width and height constraints without distorting the image. See the [crop](api/crop/) page for more information.
40+
- `crop`: Resizes the image to fill the width and height boundaries and crops any excess image data. (alias for `cover`).
41+
- `crop-x%-y%`: Resizes the image to fill the width and height boundaries and crops based on a focal point defined by `x%` (left offset) and `y%` (top offset). The resulting image will match the width and height constraints without distorting the image. See the [crop](api/crop/) page for more information.
4042

4143
~~~ html
4244
<img src="kayaks.jpg?w=300&h=300&fit=stretch">

docs/3.0/api/watermarks.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ Sets how the watermark is fitted to its target dimensions.
5454
- `max`: Resizes the image to fit within the width and height boundaries without cropping, distorting or altering the aspect ratio, and will also not increase the size of the image if it is smaller than the output size.
5555
- `fill`: Resizes the image to fit within the width and height boundaries without cropping or distorting the image, and the remaining space is filled with the background color. The resulting image will match the constraining dimensions.
5656
- `stretch`: Stretches the image to fit the constraining dimensions exactly. The resulting image will fill the dimensions, and will not maintain the aspect ratio of the input image.
57-
- `crop`: Resizes the image to fill the width and height boundaries and crops any excess image data. The resulting image will match the width and height constraints without distorting the image. See the [crop](api/crop/) page for more information.
57+
- `cover`: Resizes the image to fill the width and height boundaries and crops any excess image data. The resulting image will match the width and height constraints without distorting the image. See the [crop](api/crop/) page for more information.
58+
- `crop-x%-y%`: Resizes the image to fill the width and height boundaries and crops based on a focal point defined by `x%` (left offset) and `y%` (top offset). The resulting image will match the width and height constraints without distorting the image. See the [crop](api/crop/) page for more information.
5859

5960
~~~ html
60-
<img src="kayaks.jpg?mark=logo.png&markw=200&markh=200&markfit=crop">
61+
<img src="kayaks.jpg?mark=logo.png&markw=200&markh=200&markfit=cover">
6162
~~~
6263

6364
## X-offset `markx`

docs/3.0/config/defaults-and-presets.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ $server = League\Glide\ServerFactory::create([
4242
'small' => [
4343
'w' => 200,
4444
'h' => 200,
45-
'fit' => 'crop',
45+
'fit' => 'cover',
4646
],
4747
'medium' => [
4848
'w' => 600,
4949
'h' => 400,
50-
'fit' => 'crop',
50+
'fit' => 'cover',
5151
]
5252
]
5353
]);
@@ -57,12 +57,12 @@ $server->setPresets([
5757
'small' => [
5858
'w' => 200,
5959
'h' => 200,
60-
'fit' => 'crop',
60+
'fit' => 'cover',
6161
],
6262
'medium' => [
6363
'w' => 600,
6464
'h' => 400,
65-
'fit' => 'crop',
65+
'fit' => 'cover',
6666
]
6767
]);
6868
~~~
@@ -89,4 +89,4 @@ It's even possible to use presets with additional parameters:
8989

9090
## Overriding defaults and presets
9191

92-
You can override the default and preset manipulations for a specific request by passing a new parameter (e.x. `mark=different.png`), or even disable it entirely by setting it to blank (e.x. `mark=`).
92+
You can override the default and preset manipulations for a specific request by passing a new parameter (e.x. `mark=different.png`), or even disable it entirely by setting it to blank (e.x. `mark=`).

docs/3.0/simple-example.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ In your templates simply define how the image will be manipulated. Using Glide's
1616
<h1><?=$user->name?></h1>
1717

1818
<!-- display profile image cropped to 300x400 -->
19-
<img src="/img/users/<?=$user->id?>.jpg?w=300&h=400&fit=crop">
19+
<img src="/img/users/<?=$user->id?>.jpg?w=300&h=400&fit=cover">
2020
~~~
2121

2222
<p class="message-notice">For simplicity this example has omitted HTTP signatures, however in a production environment it's very important to <a href="/2.0/config/security/">secure your images</a>.</p>

src/Manipulators/Size.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,15 @@ public function getFit(): string
107107
{
108108
$fit = (string) $this->getParam('fit');
109109

110-
if (in_array($fit, ['contain', 'fill', 'max', 'stretch', 'fill-max'], true)) {
110+
if (in_array($fit, ['contain', 'fill', 'max', 'stretch', 'fill-max', 'cover'], true)) {
111111
return $fit;
112112
}
113113

114-
if (preg_match('/^(crop)(-top-left|-top|-top-right|-left|-center|-right|-bottom-left|-bottom|-bottom-right|-[\d]{1,3}-[\d]{1,3}(?:-[\d]{1,3}(?:\.\d+)?)?)*$/', $fit)) {
114+
if (preg_match('/^(crop|cover)(-top-left|-top|-top-right|-left|-center|-right|-bottom-left|-bottom|-bottom-right?)*$/', $fit)) {
115+
return 'cover';
116+
}
117+
118+
if (preg_match('/^(crop)(-[\d]{1,3}-[\d]{1,3}(?:-[\d]{1,3}(?:\.\d+)?)?)*$/', $fit)) {
115119
return 'crop';
116120
}
117121

@@ -245,6 +249,10 @@ public function runResize(ImageInterface $image, string $fit, int $width, int $h
245249
return $this->runStretchResize($image, $width, $height);
246250
}
247251

252+
if ('cover' === $fit) {
253+
return $this->runCoverResize($image, $width, $height);
254+
}
255+
248256
if ('crop' === $fit) {
249257
return $this->runCropResize($image, $width, $height);
250258
}
@@ -344,6 +352,25 @@ public function runCropResize(ImageInterface $image, int $width, int $height): I
344352
return $image->crop($width, $height, $offset_x, $offset_y);
345353
}
346354

355+
/**
356+
* Perform crop resize image manipulation.
357+
*
358+
* @param ImageInterface $image The source image.
359+
* @param int $width The width.
360+
* @param int $height The height.
361+
* @param ?string $position The position of the crop
362+
*
363+
* @return ImageInterface The manipulated image.
364+
*/
365+
public function runCoverResize(ImageInterface $image, int $width, int $height, ?string $position = null): ImageInterface
366+
{
367+
$position ??= str_replace(['crop-', 'cover-'], '', (string) $this->getParam('fit'));
368+
369+
$position = empty($position) || in_array($position, ['crop', 'cover']) ? 'center' : $position;
370+
371+
return $image->cover($width, $height, $position);
372+
}
373+
347374
/**
348375
* Resolve the crop resize dimensions.
349376
*

0 commit comments

Comments
 (0)