mirror of
https://github.com/Intervention/image.git
synced 2025-01-29 17:57:50 +01:00
added new method resizeCanvas
This commit is contained in:
parent
8ab5f29e8e
commit
abfb53f92d
26
README.md
26
README.md
@ -53,6 +53,7 @@ Add the facade of this package to the `$aliases` array.
|
||||
* 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::resizeCanvas - Resize image canvas
|
||||
* 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)
|
||||
* Image::contrast - Changes contrast of current image (-100 = min contrast, 0 = no change, +100 = max contrast)
|
||||
@ -106,6 +107,31 @@ $img->reset();
|
||||
$img->save('public/bar.jpg', 60);
|
||||
```
|
||||
|
||||
#### Resize canvas
|
||||
|
||||
```php
|
||||
// create Image from file
|
||||
$img = Image::make('public/foo.jpg');
|
||||
|
||||
// resize image canvas
|
||||
$img->resizeCanvas(300, 200);
|
||||
|
||||
// resize only the width of the canvas
|
||||
$img->resizeCanvas(300, null);
|
||||
|
||||
// resize only the height of the canvas
|
||||
$img->resizeCanvas(null, 200);
|
||||
|
||||
// resize the canvas by cutting out bottom right position
|
||||
$img->resizeCanvas(300, 200, 'bottom-right');
|
||||
|
||||
// resize the canvas relative by setting the third parameter to true
|
||||
$img->resizeCanvas(10, -10, 'center', true);
|
||||
|
||||
// set a background-color for the emerging area
|
||||
$img->resizeCanvas(1280, 720, 'center', false, 'ff00ff');
|
||||
```
|
||||
|
||||
#### Crop image
|
||||
|
||||
```php
|
||||
|
@ -379,6 +379,135 @@ class Image
|
||||
return $this->resize($width, $height, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resize image canvas
|
||||
*
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
* @param string $anchor
|
||||
* @param boolean $relative
|
||||
* @param mixed $bgcolor
|
||||
* @return Image
|
||||
*/
|
||||
public function resizeCanvas($width, $height, $anchor = null, $relative = false, $bgcolor = null)
|
||||
{
|
||||
// check of only width or height is set
|
||||
$width = is_null($width) ? $this->width : intval($width);
|
||||
$height = is_null($height) ? $this->height : intval($height);
|
||||
|
||||
// check on relative width/height
|
||||
if ($relative) {
|
||||
$width = $this->width + $width;
|
||||
$height = $this->height + $height;
|
||||
}
|
||||
|
||||
// check for negative width
|
||||
if ($width <= 0) {
|
||||
$width = $this->width + $width;
|
||||
}
|
||||
|
||||
// check for negative height
|
||||
if ($height <= 0) {
|
||||
$height = $this->height + $height;
|
||||
}
|
||||
|
||||
// create new canvas
|
||||
$image = @imagecreatetruecolor($width, $height);
|
||||
|
||||
if ($width > $this->width || $height > $this->height) {
|
||||
$bgcolor = is_null($bgcolor) ? '000000' : $bgcolor;
|
||||
imagefill($image, 0, 0, $this->parseColor($bgcolor));
|
||||
}
|
||||
|
||||
if ($width >= $this->width) {
|
||||
$src_w = $this->width;
|
||||
} else {
|
||||
$src_w = $width;
|
||||
}
|
||||
|
||||
if ($height >= $this->height) {
|
||||
$src_h = $this->height;
|
||||
} else {
|
||||
$src_h = $height;
|
||||
}
|
||||
|
||||
// define anchor
|
||||
switch ($anchor) {
|
||||
case 'top-left':
|
||||
$src_x = 0;
|
||||
$src_y = 0;
|
||||
break;
|
||||
|
||||
case 'top':
|
||||
case 'top-center':
|
||||
case 'top-middle':
|
||||
$src_x = ($width < $this->width) ? intval(($this->width - $width) / 2) : 0;
|
||||
$src_y = 0;
|
||||
break;
|
||||
|
||||
case 'top-right':
|
||||
$src_x = ($width < $this->width) ? intval($this->width - $width) : 0;
|
||||
$src_y = 0;
|
||||
break;
|
||||
|
||||
case 'left':
|
||||
case 'left-center':
|
||||
case 'left-middle':
|
||||
$src_x = 0;
|
||||
$src_y = ($height < $this->height) ? intval(($this->height - $height) / 2) : 0;
|
||||
break;
|
||||
|
||||
case 'right':
|
||||
case 'right-center':
|
||||
case 'right-middle':
|
||||
$src_x = ($width < $this->width) ? intval($this->width - $width) : 0;
|
||||
$src_y = ($height < $this->height) ? intval(($this->height - $height) / 2) : 0;
|
||||
break;
|
||||
|
||||
case 'bottom-left':
|
||||
$src_x = 0;
|
||||
$src_y = ($height < $this->height) ? intval($this->height - $height) : 0;
|
||||
break;
|
||||
|
||||
case 'bottom':
|
||||
case 'bottom-center':
|
||||
case 'bottom-middle':
|
||||
$src_x = ($width < $this->width) ? intval(($this->width - $width) / 2) : 0;
|
||||
$src_y = ($height < $this->height) ? intval($this->height - $height) : 0;
|
||||
break;
|
||||
|
||||
case 'bottom-right':
|
||||
$src_x = ($width < $this->width) ? intval($this->width - $width) : 0;
|
||||
$src_y = ($height < $this->height) ? intval($this->height - $height) : 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
case 'center':
|
||||
case 'middle':
|
||||
case 'center-center':
|
||||
case 'middle-middle':
|
||||
$src_x = ($width < $this->width) ? intval(($this->width - $width) / 2) : 0;
|
||||
$src_y = ($height < $this->height) ? intval(($this->height - $height) / 2) : 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// define dest. pos
|
||||
$dst_x = ($width <= $this->width) ? 0 : intval(($width - $this->width) / 2);
|
||||
$dst_y = ($height <= $this->height) ? 0 : intval(($height - $this->height) / 2);
|
||||
|
||||
// copy content from resource
|
||||
@imagecopy($image, $this->resource, $dst_x , $dst_y , $src_x , $src_y , $src_w , $src_h);
|
||||
|
||||
// set new content as recource
|
||||
$this->resource = $image;
|
||||
|
||||
// set new dimensions
|
||||
$this->width = $width;
|
||||
$this->height = $height;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Crop the current image
|
||||
*
|
||||
|
@ -145,6 +145,165 @@ class ImageTest extends PHPUnit_Framework_Testcase
|
||||
$this->assertEquals($img->height, $original_height);
|
||||
}
|
||||
|
||||
public function testResizeCanvas()
|
||||
{
|
||||
$img = $this->getTestImage();
|
||||
$img->resizeCanvas(300, 200); // pin center
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 300);
|
||||
$this->assertEquals($img->height, 200);
|
||||
$this->assertEquals('#ffe8bc', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#ffaf1c', $img->pickColor(299, 199, 'hex'));
|
||||
|
||||
$img = $this->getTestImage();
|
||||
$img->resizeCanvas(300, 200, 'top-left');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 300);
|
||||
$this->assertEquals($img->height, 200);
|
||||
$this->assertEquals('#ffffff', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#fee3ae', $img->pickColor(299, 199, 'hex'));
|
||||
|
||||
$img = $this->getTestImage();
|
||||
$img->resizeCanvas(300, 200, 'top');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 300);
|
||||
$this->assertEquals($img->height, 200);
|
||||
$this->assertEquals('#fffbf2', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#ffc559', $img->pickColor(299, 199, 'hex'));
|
||||
|
||||
$img = $this->getTestImage();
|
||||
$img->resizeCanvas(300, 200, 'top-right');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 300);
|
||||
$this->assertEquals($img->height, 200);
|
||||
$this->assertEquals('#ffe2ae', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#ffac12', $img->pickColor(299, 199, 'hex'));
|
||||
|
||||
$img = $this->getTestImage();
|
||||
$img->resizeCanvas(300, 200, 'left');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 300);
|
||||
$this->assertEquals($img->height, 200);
|
||||
$this->assertEquals('#fefdf9', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#ffca6a', $img->pickColor(299, 199, 'hex'));
|
||||
|
||||
$img = $this->getTestImage();
|
||||
$img->resizeCanvas(300, 200, 'right');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 300);
|
||||
$this->assertEquals($img->height, 200);
|
||||
$this->assertEquals('#ffca66', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#ffa600', $img->pickColor(299, 199, 'hex'));
|
||||
|
||||
$img = $this->getTestImage();
|
||||
$img->resizeCanvas(300, 200, 'bottom-left');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 300);
|
||||
$this->assertEquals($img->height, 200);
|
||||
$this->assertEquals('#ffedcc', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#ffb42b', $img->pickColor(299, 199, 'hex'));
|
||||
|
||||
$img = $this->getTestImage();
|
||||
$img->resizeCanvas(300, 200, 'bottom');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 300);
|
||||
$this->assertEquals($img->height, 200);
|
||||
$this->assertEquals('#ffd179', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#ffa600', $img->pickColor(299, 199, 'hex'));
|
||||
|
||||
$img = $this->getTestImage();
|
||||
$img->resizeCanvas(300, 200, 'bottom-right');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 300);
|
||||
$this->assertEquals($img->height, 200);
|
||||
$this->assertEquals('#ffb42a', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#ffa600', $img->pickColor(299, 199, 'hex'));
|
||||
|
||||
// resize relative from center 5px border in magenta
|
||||
$img = $this->getTestImage();
|
||||
$img->resizeCanvas(10, 10, 'center', true, 'ff00ff');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 810);
|
||||
$this->assertEquals($img->height, 610);
|
||||
$this->assertEquals('#ff00ff', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#ff00ff', $img->pickColor(809, 609, 'hex'));
|
||||
|
||||
// resize just width
|
||||
$img = $this->getTestImage();
|
||||
$img->resizeCanvas(300, null);
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 300);
|
||||
$this->assertEquals($img->height, 600);
|
||||
$this->assertEquals('#fffbf2', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#ffa600', $img->pickColor(299, 599, 'hex'));
|
||||
|
||||
// resize just height
|
||||
$img = $this->getTestImage();
|
||||
$img->resizeCanvas(null, 200);
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 800);
|
||||
$this->assertEquals($img->height, 200);
|
||||
$this->assertEquals('#fefdf9', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#ffa600', $img->pickColor(799, 199, 'hex'));
|
||||
|
||||
// smaller width, larger height
|
||||
$img = $this->getTestImage();
|
||||
$img->resizeCanvas(300, 800);
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 300);
|
||||
$this->assertEquals($img->height, 800);
|
||||
$this->assertEquals('#000000', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#000000', $img->pickColor(299, 799, 'hex'));
|
||||
|
||||
// larger width, smaller height
|
||||
$img = $this->getTestImage();
|
||||
$img->resizeCanvas(900, 200);
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 900);
|
||||
$this->assertEquals($img->height, 200);
|
||||
$this->assertEquals('#000000', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#000000', $img->pickColor(899, 199, 'hex'));
|
||||
|
||||
// test negative values (for relative resize)
|
||||
$img = $this->getTestImage();
|
||||
$img->resizeCanvas(-200, -200);
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 600);
|
||||
$this->assertEquals($img->height, 400);
|
||||
$this->assertEquals('#fffefc', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#ffa600', $img->pickColor(599, 399, 'hex'));
|
||||
}
|
||||
|
||||
public function testCropImage()
|
||||
{
|
||||
$img = $this->getTestImage();
|
||||
|
Loading…
x
Reference in New Issue
Block a user