protected function addBackground() { $imageUrl = $this->data($this->option('background.data')); $imageUrl = $this->option('background.value') ? $this->option('background.value') : $imageUrl; if (empty($imageUrl)) { return; } $image = $this->imagine->open($imageUrl); $imageSize = $image->getSize(); $imageAspect = $imageSize->getWidth() / $imageSize->getHeight(); $cardSize = new Box($this->width, $this->height); $cardAspect = $this->width / $this->height; // position of background $position = new Point(0, 0); // crop if resize mode is cover if ($this->backgroundSize == 'cover') { if ($imageAspect <= $cardAspect) { // resize to max width => align the image vertically $image->resize(new Box($this->width, $this->width / $imageAspect)); } else { // resize to max height => align the image horizontally $image->resize(new Box($this->height * $imageAspect, $this->height)); } // get new image size $imageSize = $image->getSize(); // cover will add cropped image always at 0/0 and be of the card's size $position = new Point(0, 0); if ($imageAspect <= $cardAspect) { switch ($this->backgroundVertical) { case 'bottom': $image->crop(new Point(0, (int) ($imageSize->getHeight() - $this->height)), $cardSize); break; case 'lowercenter': $image->crop(new Point(0, (int) (($imageSize->getHeight() - $this->height) / 3 * 2)), $cardSize); break; case 'center': $image->crop(new Point(0, (int) (($imageSize->getHeight() - $this->height) / 2)), $cardSize); break; case 'uppercenter': $image->crop(new Point(0, (int) (($imageSize->getHeight() - $this->height) / 3)), $cardSize); break; case 'top': // default // default default: $image->crop(new Point(0, 0), $cardSize); break; } } else { switch ($this->backgroundAlign) { case 'right': $image->crop(new Point($imageSize->getWidth() - $this->width, 0), $cardSize); break; case 'center': $image->crop(new Point((int) (($imageSize->getWidth() - $this->width) / 2), 0), $cardSize); break; case 'left': // default // default default: $image->crop(new Point(0, 0), $cardSize); break; } } } if ($this->backgroundSize == 'contain') { if ($imageAspect <= $cardAspect) { // resize to max height => align the image horizontally $image->resize(new Box($this->height * $imageAspect, $this->height)); } else { // resize to max width => align the image vertically $image->resize(new Box($this->width, $this->width / $imageAspect)); } // get new image size $imageSize = $image->getSize(); // position by size and alignment options $x = 0; $y = 0; switch ($this->backgroundAlign) { case 'center': $x = (int) (($this->width - $imageSize->getWidth()) / 2); break; case 'right': $x = $this->width - $imageSize->getWidth(); break; } switch ($this->backgroundVertical) { case 'center': $y = (int) (($this->height - $imageSize->getHeight()) / 2); break; case 'bottom': $y = (int) ($this->height - $imageSize->getHeight()); break; } $position = new Point($x, $y); } // add filters before pasting $blur = $this->option('background.blur'); $darken = $this->option('background.darken'); $colorize = $this->option('background.colorize'); if ($darken || $colorize) { $image->effects()->gamma(1.2); } if ($blur) { $image->effects()->blur(); } // paste background $this->image->paste($image, $position); // == background tint overlay if ($darken) { $this->image->draw()->polygon([new Point(0, 0), new Point($this->width, 0), new Point($this->width, $this->height), new Point(0, $this->height)], $this->color('222', 50), true); } if ($colorize) { $this->image->draw()->polygon([new Point(0, 0), new Point($this->width, 0), new Point($this->width, $this->height), new Point(0, $this->height)], $this->color($this->schemeColor, 30), true); } }