Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cover method from Intervention #419

Open
wants to merge 7 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions docs/3.0/api/crop.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@ title: Crop

# Crop

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

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.

~~~ html
<img src="kayaks.jpg?w=300&h=300&fit=crop">
<img src="kayaks.jpg?w=300&h=300&fit=cover">
~~~

[![© 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)

### Crop Position

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`.
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`.

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

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

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%`.

~~~ html
<img src="kayaks.jpg?w=300&h=300&fit=crop-25-75">
<img src="kayaks.jpg?w=300&h=300&fit=cover-25-75">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be crop-25-75. For numeric values for position we use runCropReize() right?

~~~

You may also choose to zoom into your focal point by providing a third value: a float between 1 and 100. Each full step is the equivalent of a 100% zoom. (eg. `x%-y%-2` is the equivalent of viewing the image at 200%). The suggested range is 1-10.

~~~ html
<img src="kayaks.jpg?w=300&h=300&fit=crop-25-75-2">
<img src="kayaks.jpg?w=300&h=300&fit=cover-25-75-2">
~~~

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

[![© 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)
[![© 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)
7 changes: 4 additions & 3 deletions docs/3.0/api/size.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ Sets the height of the image, in pixels.

Sets how the image is fitted to its target dimensions.

### Accepts:
### Accepts:

- `contain`: Default. Resizes the image to fit within the width and height boundaries without cropping, distorting or altering the aspect ratio.
- `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.
- `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.
- `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.
- `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.
- `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.
- `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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep the crop option stating it's an alias for cover.

- `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.
- `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.

~~~ html
<img src="kayaks.jpg?w=300&h=300&fit=stretch">
Expand Down
5 changes: 3 additions & 2 deletions docs/3.0/api/watermarks.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ Sets how the watermark is fitted to its target dimensions.
- `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.
- `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.
- `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.
- `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.
- `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.
- `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.

~~~ html
<img src="kayaks.jpg?mark=logo.png&markw=200&markh=200&markfit=crop">
<img src="kayaks.jpg?mark=logo.png&markw=200&markh=200&markfit=cover">
~~~

## X-offset `markx`
Expand Down
10 changes: 5 additions & 5 deletions docs/3.0/config/defaults-and-presets.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ $server = League\Glide\ServerFactory::create([
'small' => [
'w' => 200,
'h' => 200,
'fit' => 'crop',
'fit' => 'cover',
],
'medium' => [
'w' => 600,
'h' => 400,
'fit' => 'crop',
'fit' => 'cover',
]
]
]);
Expand All @@ -57,12 +57,12 @@ $server->setPresets([
'small' => [
'w' => 200,
'h' => 200,
'fit' => 'crop',
'fit' => 'cover',
],
'medium' => [
'w' => 600,
'h' => 400,
'fit' => 'crop',
'fit' => 'cover',
]
]);
~~~
Expand All @@ -89,4 +89,4 @@ It's even possible to use presets with additional parameters:

## Overriding defaults and presets

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=`).
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=`).
2 changes: 1 addition & 1 deletion docs/3.0/simple-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ In your templates simply define how the image will be manipulated. Using Glide's
<h1><?=$user->name?></h1>

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

<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>
Expand Down
31 changes: 29 additions & 2 deletions src/Manipulators/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,15 @@ public function getFit(): string
{
$fit = (string) $this->getParam('fit');

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

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)) {
if (preg_match('/^(crop|cover)(-top-left|-top|-top-right|-left|-center|-right|-bottom-left|-bottom|-bottom-right?)*$/', $fit)) {
return 'cover';
}

if (preg_match('/^(crop)(-[\d]{1,3}-[\d]{1,3}(?:-[\d]{1,3}(?:\.\d+)?)?)*$/', $fit)) {
return 'crop';
}

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

if ('cover' === $fit) {
return $this->runCoverResize($image, $width, $height);
}

if ('crop' === $fit) {
return $this->runCropResize($image, $width, $height);
}
Expand Down Expand Up @@ -344,6 +352,25 @@ public function runCropResize(ImageInterface $image, int $width, int $height): I
return $image->crop($width, $height, $offset_x, $offset_y);
}

/**
* Perform crop resize image manipulation.
*
* @param ImageInterface $image The source image.
* @param int $width The width.
* @param int $height The height.
* @param ?string $position The position of the crop
*
* @return ImageInterface The manipulated image.
*/
public function runCoverResize(ImageInterface $image, int $width, int $height, ?string $position = null): ImageInterface
{
$position ??= str_replace(['crop-', 'cover-'], '', (string) $this->getParam('fit'));

$position = empty($position) || in_array($position, ['crop', 'cover']) ? 'center' : $position;

return $image->cover($width, $height, $position);
}

/**
* Resolve the crop resize dimensions.
*
Expand Down
Loading