Skip to content

Commit b2189fb

Browse files
Merge pull request #79 from Pastou/master
Needed to crop between the center and the top.
2 parents 118202e + 0176883 commit b2189fb

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,45 @@ This will scale the image to as close as it can to the passed dimensions, and th
118118

119119
In the case of the example above, an image of 400px × 600px will be resized down to 200px × 300px, and then 50px will be taken off the top and bottom, leaving you with 200px × 200px.
120120

121+
Crop modes:
122+
123+
Few crop mode options are available in order for you to choose how you want to handle the eventual exceeding width or height after resizing down your image.
124+
The default crop mode used is the `CROPCENTER`.
125+
As a result those pieces of code are equivalent:
126+
127+
```php
128+
$image = new ImageResize('image.jpg');
129+
$image->crop(200, 200);
130+
$image->save('image2.jpg');
131+
```
132+
133+
```php
134+
$image = new ImageResize('image.jpg');
135+
$image->crop(200, 200, ImageResize::CROPCENTER);
136+
$image->save('image2.jpg');
137+
```
138+
139+
In the case you have an image of 400px × 600px and you want to crop it to 200px × 200px the image will be resized down to 200px × 300px, then you can indicate how you want to handle those 100px exceeding passing the value of the crop mode you want to use.
140+
141+
For instance passing the crop mode `CROPTOP` will result as 100px taken off the bottom leaving you with 200px × 200px.
142+
143+
144+
```php
145+
$image = new ImageResize('image.jpg');
146+
$image->crop(200, 200, ImageResize::CROPTOP);
147+
$image->save('image2.jpg');
148+
```
149+
150+
On the contrary passing the crop mode `CROPBOTTOM` will result as 100px taken off the top leaving you with 200px × 200px.
151+
152+
```php
153+
$image = new ImageResize('image.jpg');
154+
$image->crop(200, 200, ImageResize::CROPBOTTOM);
155+
$image->save('image2.jpg');
156+
```
157+
158+
Freecrop:
159+
121160
There is also a way to define custom crop position.
122161
You can define $x and $y in ```freecrop``` method:
123162

lib/ImageResize.php

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class ImageResize
1313
const CROPBOTTOM = 3;
1414
const CROPLEFT = 4;
1515
const CROPRIGHT = 5;
16+
const CROPTOPCENTER = 6;
1617

1718
public $quality_jpg = 85;
1819
public $quality_png = 6;
@@ -570,6 +571,9 @@ protected function getCropPosition($expectedSize, $position = self::CROPCENTER)
570571
case self::CROPCENTRE:
571572
$size = $expectedSize / 2;
572573
break;
574+
case self::CROPTOPCENTER:
575+
$size = $expectedSize / 4;
576+
break;
573577
}
574578
return $size;
575579
}

0 commit comments

Comments
 (0)