mirror of
https://github.com/Intervention/image.git
synced 2025-08-30 01:00:06 +02:00
added anchor param to insert method
This commit is contained in:
@@ -480,6 +480,7 @@ class Image
|
||||
// define anchor
|
||||
switch ($anchor) {
|
||||
case 'top-left':
|
||||
case 'left-top':
|
||||
$src_x = 0;
|
||||
$src_y = 0;
|
||||
break;
|
||||
@@ -487,11 +488,14 @@ class Image
|
||||
case 'top':
|
||||
case 'top-center':
|
||||
case 'top-middle':
|
||||
case 'center-top':
|
||||
case 'middle-top':
|
||||
$src_x = ($width < $this->width) ? intval(($this->width - $width) / 2) : 0;
|
||||
$src_y = 0;
|
||||
break;
|
||||
|
||||
case 'top-right':
|
||||
case 'right-top':
|
||||
$src_x = ($width < $this->width) ? intval($this->width - $width) : 0;
|
||||
$src_y = 0;
|
||||
break;
|
||||
@@ -499,6 +503,8 @@ class Image
|
||||
case 'left':
|
||||
case 'left-center':
|
||||
case 'left-middle':
|
||||
case 'center-left':
|
||||
case 'middle-left':
|
||||
$src_x = 0;
|
||||
$src_y = ($height < $this->height) ? intval(($this->height - $height) / 2) : 0;
|
||||
break;
|
||||
@@ -506,11 +512,14 @@ class Image
|
||||
case 'right':
|
||||
case 'right-center':
|
||||
case 'right-middle':
|
||||
case 'center-right':
|
||||
case 'middle-right':
|
||||
$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':
|
||||
case 'left-bottom':
|
||||
$src_x = 0;
|
||||
$src_y = ($height < $this->height) ? intval($this->height - $height) : 0;
|
||||
break;
|
||||
@@ -518,11 +527,14 @@ class Image
|
||||
case 'bottom':
|
||||
case 'bottom-center':
|
||||
case 'bottom-middle':
|
||||
case 'center-bottom':
|
||||
case 'middle-bottom':
|
||||
$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':
|
||||
case 'right-bottom':
|
||||
$src_x = ($width < $this->width) ? intval($this->width - $width) : 0;
|
||||
$src_y = ($height < $this->height) ? intval($this->height - $height) : 0;
|
||||
break;
|
||||
@@ -679,14 +691,89 @@ class Image
|
||||
/**
|
||||
* Insert another image on top of the current image
|
||||
*
|
||||
* @param mixed $file
|
||||
* @param mixed $file
|
||||
* @param integer $pos_x
|
||||
* @param integer $pos_y
|
||||
* @param string $anchor
|
||||
* @return Image
|
||||
*/
|
||||
public function insert($file, $pos_x = 0, $pos_y = 0)
|
||||
public function insert($file, $pos_x = 0, $pos_y = 0, $anchor = null)
|
||||
{
|
||||
$obj = is_a($file, 'Intervention\Image\Image') ? $file : (new Image($file));
|
||||
|
||||
// define anchor
|
||||
switch ($anchor) {
|
||||
|
||||
case 'top':
|
||||
case 'top-center':
|
||||
case 'top-middle':
|
||||
case 'center-top':
|
||||
case 'middle-top':
|
||||
$pos_x = intval((($this->width - $obj->width) / 2) + $pos_x);
|
||||
$pos_y = $pos_y;
|
||||
break;
|
||||
|
||||
case 'top-right':
|
||||
case 'right-top':
|
||||
$pos_x = intval($this->width - $obj->width - $pos_x);
|
||||
$pos_y = $pos_y;
|
||||
break;
|
||||
|
||||
case 'left':
|
||||
case 'left-center':
|
||||
case 'left-middle':
|
||||
case 'center-left':
|
||||
case 'middle-left':
|
||||
$pos_x = $pos_x;
|
||||
$pos_y = intval((($this->height - $obj->height) / 2) + $pos_y);
|
||||
break;
|
||||
|
||||
case 'right':
|
||||
case 'right-center':
|
||||
case 'right-middle':
|
||||
case 'center-right':
|
||||
case 'middle-right':
|
||||
$pos_x = intval($this->width - $obj->width - $pos_x);
|
||||
$pos_y = intval((($this->height - $obj->height) / 2) + $pos_y);
|
||||
break;
|
||||
|
||||
case 'bottom-left':
|
||||
case 'left-bottom':
|
||||
$pos_x = $pos_x;
|
||||
$pos_y = intval($this->height - $obj->height - $pos_y);
|
||||
break;
|
||||
|
||||
case 'bottom':
|
||||
case 'bottom-center':
|
||||
case 'bottom-middle':
|
||||
case 'center-bottom':
|
||||
case 'middle-bottom':
|
||||
$pos_x = intval((($this->width - $obj->width) / 2) + $pos_x);
|
||||
$pos_y = intval($this->height - $obj->height - $pos_y);
|
||||
break;
|
||||
|
||||
case 'bottom-right':
|
||||
case 'right-bottom':
|
||||
$pos_x = intval($this->width - $obj->width - $pos_x);
|
||||
$pos_y = intval($this->height - $obj->height - $pos_y);
|
||||
break;
|
||||
|
||||
case 'center':
|
||||
case 'middle':
|
||||
case 'center-center':
|
||||
case 'middle-middle':
|
||||
$pos_x = intval((($this->width - $obj->width) / 2) + $pos_x);
|
||||
$pos_y = intval((($this->height - $obj->height) / 2) + $pos_y);
|
||||
break;
|
||||
|
||||
default:
|
||||
case 'top-left':
|
||||
case 'left-top':
|
||||
$pos_x = intval($pos_x);
|
||||
$pos_y = intval($pos_y);
|
||||
break;
|
||||
}
|
||||
|
||||
imagecopy($this->resource, $obj->resource, $pos_x, $pos_y, 0, 0, $obj->width, $obj->height);
|
||||
|
||||
return $this;
|
||||
|
@@ -430,9 +430,206 @@ class ImageTest extends PHPUnit_Framework_Testcase
|
||||
|
||||
public function testInsertImage()
|
||||
{
|
||||
$img = $this->getTestImage();
|
||||
$img->insert('public/test.jpg', 10, 10);
|
||||
$img = Image::canvas(32, 32, '#ff0000'); // create canvas
|
||||
$watermark = Image::canvas(16, 16, '#0000ff'); // create watermark
|
||||
|
||||
// top-left anchor
|
||||
$img->insert($watermark, 0, 0, 'top-left');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#0000ff', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#ff0000', $img->pickColor(16, 16, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// top-left anchor coordinates
|
||||
$img->insert($watermark, 10, 10, 'top-left');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#ff0000', $img->pickColor(9, 9, 'hex'));
|
||||
$this->assertEquals('#0000ff', $img->pickColor(10, 10, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// top anchor
|
||||
$img->insert($watermark, 0, 0, 'top');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#ff0000', $img->pickColor(0, 0, 'hex'));
|
||||
$this->assertEquals('#0000ff', $img->pickColor(23, 15, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// top anchor coordinates
|
||||
$img->insert($watermark, 10, 10, 'top');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#0000ff', $img->pickColor(18, 10, 'hex'));
|
||||
$this->assertEquals('#ff0000', $img->pickColor(31, 26, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// top-right anchor
|
||||
$img->insert($watermark, 0, 0, 'top-right');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#ff0000', $img->pickColor(15, 0, 'hex'));
|
||||
$this->assertEquals('#0000ff', $img->pickColor(31, 0, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// top-right anchor coordinates
|
||||
$img->insert($watermark, 10, 10, 'top-right');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#ff0000', $img->pickColor(6, 9, 'hex'));
|
||||
$this->assertEquals('#0000ff', $img->pickColor(21, 25, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// left anchor
|
||||
$img->insert($watermark, 0, 0, 'left');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#0000ff', $img->pickColor(15, 23, 'hex'));
|
||||
$this->assertEquals('#ff0000', $img->pickColor(0, 7, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// left anchor coordinates
|
||||
$img->insert($watermark, 10, 10, 'left');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#0000ff', $img->pickColor(25, 31, 'hex'));
|
||||
$this->assertEquals('#ff0000', $img->pickColor(10, 17, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// right anchor
|
||||
$img->insert($watermark, 0, 0, 'right');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#0000ff', $img->pickColor(31, 23, 'hex'));
|
||||
$this->assertEquals('#ff0000', $img->pickColor(15, 15, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// right anchor coordinates
|
||||
$img->insert($watermark, 10, 10, 'right');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#0000ff', $img->pickColor(21, 31, 'hex'));
|
||||
$this->assertEquals('#ff0000', $img->pickColor(5, 18, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// bottom-left anchor
|
||||
$img->insert($watermark, 0, 0, 'bottom-left');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#0000ff', $img->pickColor(15, 31, 'hex'));
|
||||
$this->assertEquals('#ff0000', $img->pickColor(0, 15, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// bottom-left anchor coordinates
|
||||
$img->insert($watermark, 10, 10, 'bottom-left');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#0000ff', $img->pickColor(10, 21, 'hex'));
|
||||
$this->assertEquals('#ff0000', $img->pickColor(9, 20, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// bottom anchor
|
||||
$img->insert($watermark, 0, 0, 'bottom');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#0000ff', $img->pickColor(8, 16, 'hex'));
|
||||
$this->assertEquals('#ff0000', $img->pickColor(8, 15, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// bottom anchor coordinates
|
||||
$img->insert($watermark, 10, 10, 'bottom');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#0000ff', $img->pickColor(18, 21, 'hex'));
|
||||
$this->assertEquals('#ff0000', $img->pickColor(17, 21, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// bottom-right anchor
|
||||
$img->insert($watermark, 0, 0, 'bottom-right');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#0000ff', $img->pickColor(16, 16, 'hex'));
|
||||
$this->assertEquals('#ff0000', $img->pickColor(15, 16, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// bottom-right anchor coordinates
|
||||
$img->insert($watermark, 10, 10, 'bottom-right');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#0000ff', $img->pickColor(21, 21, 'hex'));
|
||||
$this->assertEquals('#ff0000', $img->pickColor(22, 22, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// center anchor
|
||||
$img->insert($watermark, 0, 0, 'center');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#0000ff', $img->pickColor(23, 23, 'hex'));
|
||||
$this->assertEquals('#ff0000', $img->pickColor(8, 7, 'hex'));
|
||||
$img->reset();
|
||||
|
||||
// center anchor coordinates
|
||||
$img->insert($watermark, 10, 10, 'center');
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInternalType('int', $img->width);
|
||||
$this->assertInternalType('int', $img->height);
|
||||
$this->assertEquals($img->width, 32);
|
||||
$this->assertEquals($img->height, 32);
|
||||
$this->assertEquals('#0000ff', $img->pickColor(31, 31, 'hex'));
|
||||
$this->assertEquals('#ff0000', $img->pickColor(18, 17, 'hex'));
|
||||
$img->reset();
|
||||
}
|
||||
|
||||
public function testOpacity()
|
||||
|
Reference in New Issue
Block a user