mirror of
https://github.com/Intervention/image.git
synced 2025-08-19 04:01:30 +02:00
added crop method
This commit is contained in:
17
README.md
17
README.md
@@ -49,6 +49,7 @@ Add the facade of this package to the `$aliases` array.
|
||||
* Image::make - Open a new image resource from image file or create a new empty image
|
||||
* Image::canvas - Create a new empty image resource
|
||||
* Image::resize - Resize current image based on given width and/or height
|
||||
* Image::crop - Crop the current image
|
||||
* Image::grab - Cut out a detail of the image in given ratio and resize to output size
|
||||
* Image::insert - Insert another image on top of the current image
|
||||
* Image::brightness - Changes brightness of current image (-100 = min brightness, 0 = no change, +100 = max brightness)
|
||||
@@ -99,6 +100,22 @@ $img->reset();
|
||||
$img->save('public/bar.jpg', 60);
|
||||
```
|
||||
|
||||
#### Crop image
|
||||
|
||||
```php
|
||||
// create Image from file
|
||||
$img = Image::make('public/foo.jpg');
|
||||
|
||||
// crop 300x200 pixel cutout at position x:200, y:100
|
||||
$img->crop(300, 200, 200, 100);
|
||||
|
||||
// crop 300x200 pixel cutout centered on current image
|
||||
$img->crop(300, 200);
|
||||
|
||||
// save image
|
||||
$img->save();
|
||||
```
|
||||
|
||||
#### Smart resizing
|
||||
|
||||
```php
|
||||
|
@@ -315,6 +315,36 @@ class Image
|
||||
return $this->resize($width, $height, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Crop the current image
|
||||
*
|
||||
* @param integer $width
|
||||
* @param integer $height
|
||||
* @param integer $pos_x
|
||||
* @param integer $pos_y
|
||||
*
|
||||
* @return Image
|
||||
*/
|
||||
public function crop($width, $height, $pos_x = null, $pos_y = null)
|
||||
{
|
||||
$width = is_numeric($width) ? intval($width) : null;
|
||||
$height = is_numeric($height) ? intval($height) : null;
|
||||
$pos_x = is_numeric($pos_x) ? intval($pos_x) : null;
|
||||
$pos_y = is_numeric($pos_y) ? intval($pos_y) : null;
|
||||
|
||||
if (is_null($pos_x) && is_null($pos_y)) {
|
||||
// center position of width/height rectangle
|
||||
$pos_x = floor(($this->width - intval($width)) / 2);
|
||||
$pos_y = floor(($this->height - intval($height)) / 2);
|
||||
}
|
||||
|
||||
if (is_null($width) || is_null($height)) {
|
||||
throw new Exception('width and height of cutout needs to be defined');
|
||||
}
|
||||
|
||||
return $this->modify(0, 0, $pos_x , $pos_y, $width, $height, $width, $height);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cut out a detail of the image in given ratio and resize to output size
|
||||
*
|
||||
|
@@ -107,6 +107,25 @@ class ImageTest extends PHPUnit_Framework_Testcase
|
||||
$this->assertEquals($img->height, $original_height);
|
||||
}
|
||||
|
||||
public function testCropImage()
|
||||
{
|
||||
$img = $this->getTestImage();
|
||||
$img->crop(100, 100);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 100);
|
||||
$this->assertEquals($img->height, 100);
|
||||
$this->assertEquals('#ffbe46', $img->pickColor(99, 99, 'hex'));
|
||||
|
||||
$img = $this->getTestImage();
|
||||
$img->crop(100, 100, 650, 400);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 100);
|
||||
$this->assertEquals($img->height, 100);
|
||||
$this->assertEquals('#ffa600', $img->pickColor(99, 99, 'hex'));
|
||||
}
|
||||
|
||||
public function testLegacyResize()
|
||||
{
|
||||
// auto height
|
||||
|
Reference in New Issue
Block a user