Skip to content

Commit

Permalink
crop
Browse files Browse the repository at this point in the history
  • Loading branch information
sokil committed Oct 10, 2014
1 parent 0f13b06 commit 05b46f8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ php-image
* [Installation](#installation)
* [Open image](#open-image)
* [Resize image](#resize-image)
* [Crop image](#crop-image)
* [Rotate image](#rotate-image)
* [Flip image](#flip-image)
* [Filters](#filters)
Expand Down Expand Up @@ -77,6 +78,19 @@ $image->addResizeStrategyNamespace('\Vendor\ResizeStrategy');
```
Classes searches in priority of adding.

Crop image
----------

To get part of image by specified wifth and height and in defined coordinates use:
```php
$x = 10;
$y = 10;
$width = 20;
$height = 20;

$image->crop($x, $y, $width, $height);
```

Rotate image
------------

Expand Down
19 changes: 19 additions & 0 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,25 @@ public function filter(\Sokil\Image\AbstractFilterStrategy $filterStrategy)
return $this;
}

public function crop($x, $y, $width, $height)
{
$croppedImageResource = imagecreatetruecolor($width, $height);
imagecopyresampled(
$croppedImageResource,
$this->_resource,
0,
0,
$x,
$y,
$width,
$height,
$width,
$height
);

$this->loadResource($croppedImageResource);
}

public function appendElementAtPosition(AbstractElement $element, $x, $y)
{
$element->draw($this->_resource, $x, $y);
Expand Down
10 changes: 10 additions & 0 deletions tests/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,14 @@ public function testAppendElement_TextElement()

$this->assertEquals(array(255, 0, 0, 0), $color);
}

public function testCrop()
{
$image = $this->_factory->openImage(__DIR__ . '/test.png');
$image->crop(10, 10, 10, 10);

$this->assertEquals(10, imagesx($image->getResource()));
$this->assertEquals(10, imagesy($image->getResource()));
$this->assertEquals([0, 0, 255, 0], Rgb::fromInt(imagecolorat($image->getResource(), 5, 5))->toArray());
}
}

0 comments on commit 05b46f8

Please sign in to comment.