protected function fit() { if ($this->width !== null || $this->height !== null) { if ($this->fit === self::FIT_CROP && $this->width && $this->height) { $this->image->fit($this->width, $this->height, function ($constraint) { /* @var $constraint \Intervention\Image\Constraint */ $constraint->aspectRatio(); }); return; } if ($this->fit === self::FIT_MIN || $this->fit === self::FIT_MAX) { if ($this->fit === self::FIT_MAX) { $this->image->resize($this->width, $this->height, function ($constraint) { /* @var $constraint \Intervention\Image\Constraint */ $constraint->aspectRatio(); $constraint->upsize(); }); $this->image->resizeCanvas($this->width, $this->height, 'top-left'); } if ($this->fit === self::FIT_MIN) { $height = $this->image->getHeight() < $this->height ? $this->image->getHeight() : $this->height; $width = $this->image->getWidth() < $this->width ? $this->image->getWidth() : $this->width; $this->image->fit($width, $height, function ($constraint) { /* @var $constraint \Intervention\Image\Constraint */ $constraint->aspectRatio(); $constraint->upsize(); }); } return; } if ($this->fit === self::FIT_CLIP) { $this->image->resize($this->width, $this->height, function ($constraint) { /* @var $constraint \Intervention\Image\Constraint */ $constraint->aspectRatio(); $constraint->upsize(); }); return; } if ($this->fit === self::FIT_SCALE) { $this->image->resize($this->width, $this->height); return; } if ($this->fit === self::FIT_CLAMP) { $this->image->resize($this->width, $this->height, function ($constraint) { /* @var $constraint \Intervention\Image\Constraint */ $constraint->aspectRatio(); }); } } }
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')); // resize to larger size with anchor and only height set $img = $this->getTestImage(); $img->resizeCanvas(null, 650, 'bottom-left', false, '#00000'); $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, 650); $this->assertEquals('#000000', $img->pickColor(0, 0, 'hex')); $this->assertEquals('#ffffff', $img->pickColor(3, 50, 'hex')); $this->assertEquals('#ffa600', $img->pickColor(799, 649, 'hex')); // resize with emerging transparent area $img = $this->getTestImage(); $img->resizeCanvas(900, 700, 'center', false, array(0, 0, 0, 0)); $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, 700); $transparency_1 = $img->pickColor(0, 0, 'array'); $transparency_2 = $img->pickColor(899, 699, 'array'); $this->assertEquals(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0.0), $transparency_1); $this->assertEquals(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0.0), $transparency_2); // preserve transparency when resizing canvas $img = new Image('public/circle.png'); $img->resizeCanvas(40, 40, 'center'); $this->assertInstanceOf('Intervention\\Image\\Image', $img); $this->assertInternalType('int', $img->width); $this->assertInternalType('int', $img->height); $this->assertEquals($img->width, 40); $this->assertEquals($img->height, 40); $this->assertEquals(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0.0), $img->pickColor(0, 0, 'array')); $this->assertEquals(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0.8), $img->pickColor(20, 20, 'array')); }
public function runExpand(Image $image, $width, $color) { return $image->resizeCanvas($width * 2, $width * 2, 'center', true, $color); }
/** * Resize image canvas * * @param int $width * @param int $height * @param string $anchor * @param boolean $relative * @param mixed $bgcolor * @return \Intervention\Image\Image * @static */ public static function resizeCanvas($width, $height, $anchor = null, $relative = false, $bgcolor = null) { return \Intervention\Image\Image::resizeCanvas($width, $height, $anchor, $relative, $bgcolor); }
static function original(\Intervention\Image\Image $img, $w, $h) { $max = max($w, $h); return $img->resizeCanvas($max * 2, $max * 2); }