Create a blank image with of given dimensions and fill it with $bgColor.
public static create ( integer $width, integer $height, string $bgColor = null ) : Webiny\Component\Image\ImageInterface | ||
$width | integer | Width of the new image. |
$height | integer | Height of the new image. |
$bgColor | string | Background color. Following formats are acceptable - "fff" - "ffffff" - array(255,255,255) |
return | Webiny\Component\Image\ImageInterface |
/** * This is a method that combines resize, crop and paste methods in order to generate a thumbnail from the given image. * The benefit of using this function is that the function can automatically combine crop and resize methods together * with the pad feature in order to generate the thumb. * * @param int $width Thumb width. * @param int $height Thumb height. * @param bool|string $cropOrPad If you set this to 'crop' the method will first resize the image to preserve the * aspect ratio and then it will crop the extra pixels to fit the defined width and height. * If you set this to 'pad' the method will first do the resize and than * it wil create a blank image that has the size of defined width and height and fill it * with $padColor, then it will paste the resized image in the center of the new image. * @param null|string $padColor Parameter that fills the background with the defined color. * Following formats are acceptable * - "fff" * - "ffffff" * - array(255,255,255) * * @return $this */ public function thumbnail($width, $height, $cropOrPad = false, $padColor = null) { // get the aspect ratio $currentSize = $this->getSize(); $ar = round($currentSize['width'] / $currentSize['height'], 3); $nar = round($width / $height, 3); $newWidth = $width; $newHeight = $height; if ($ar >= 1) { if ($nar > $ar) { $newHeight = $width / $ar; } else { $newWidth = $height * $ar; } } else { if ($nar > $ar) { $newHeight = $width / $ar; } else { $newWidth = $height * $ar; } } $this->resize($newWidth, $newHeight); // crop if ($cropOrPad == self::CROP) { $this->crop($width, $height); } // pad if ($cropOrPad == self::PAD) { $padColor = !empty($padColor) ? $padColor : 'ffffff'; $image = ImageLoader::create($width, $height, $padColor); // re-calculate the size based on aspect ratio if ($width < $height) { $newWidth = $width; $newHeight = round($width / $ar, 0); } else { $newWidth = round($height / $ar, 0); $newHeight = $height; } // center the padded image $offsetX = ($width - $newWidth) / 2; $offsetY = ($height - $newHeight) / 2; // resize the current image $this->resize($newWidth, $newHeight); $image->paste($this, $offsetX, $offsetY); $this->image = $image->getInstance(); unset($image); } return $this; }
public function testCreate() { $image = ImageLoader::create(1, 1, '#666666'); $this->assertInstanceOf('\\Webiny\\Component\\Image\\ImageInterface', $image); }