Skip to content

darrynten/pslayers

Repository files navigation

pslayers

Travis Build Status StyleCI Status codecov Packagist Version MIT License

Powerful and fully featured PHP library that features photoshop-style image layering, compositing, filtering, blending and masking.

Uses Imagick and PHP 7+

Introduction

We needed to give buzzbot.ai a way to express herself visually, so we gave her photoshop-level image management and manipulation capabilities.

This is the core package that our bots use during their Generation phases. As such it has been designed from the ground up to be simple to use, but to also allow for maximum flexibility.

The core function of this package is to provide an easy way to layer images and then composite them together.

We're pre-releasing this as it does everything we need it to, but we're busy with other stuff, and as you know incomplete and released is better (and quicker) than complete and released. And we need it on packagist :)

Basic Usage

$init = [
    'id' => 12,
    'width' => 100,
    'height' => 100,
];

$pslayers = new Pslayers($init);

Single Layer

// Bare minimum for a blank layer
$layerConfig = [
    'id' => 14,
    'width' => 100,
    'height' => 100,
];

$layer = new BlankLayer($layerConfig);

// getting
$width = $layer->width(); // $width = 100

// setting
$layer->width(200); // $width = 200
Standard base getters and setters

It was very silly to do var/get/set all with the same name. This is on the todo list to refactor out.

$layer->width();
$layer->height();
$layer->positionX();
$layer->positionY();
$layer->opacity(); // number between 0 and 1
$layer->composite(); // see notes below
Getting details of the layer

All layer classes implement an interface which forces the method getLayerDetailsArray() which returns an array representation of the layer, which differs from layer to layer.

There is a helper method that fetches the JSON representation of this which is called getLayerDetailsJson() and all types of layers have this method available on them.

Setting the Composite

Composite is the Composite Operator Constants, which is Imagick integer constants.

You pass then in with Imagick::COMPOSITE_DEFAULT or whichever composite operator you want to apply on your layer.

Currently only 1 composite, it is not possible to combine composites yet.

$layer->composite(Imagick::COMPOSITE_LIGHTEN);

This composite will be applied when the stack is rendered.

Collections of Layers

// Make a layer
$layer = new BlankLayer($config);

// Add to your Pslayers object
$pslayer->layers->addLayerToCollection($layer);

// Add with foced z-index (destructive, see notes below)
$pslayer->layers->addLayerToCollection($layer, 2);
Programmatic Collections

You can make and manipulate your own collections if you like

// Make a collection
$collection = new LayerCollection();

// Add a layer to a collection
$collection->addLayerToCollection($layer);

// Add a layer with a forced z index (destructive, see notes below)
$collection->addLayerToCollection($layer, 1);

Rendering

You can call the render method on your collection which will render up from index 0

$pslayers->addLayerToCollection($layer1);
$pslayers->addLayerToCollection($layer2);
$pslayers->addLayerToCollection($layer3);

$pslayers->render();

Layer Types

More info on the standard layer types

Blank Layer

Detailed above, this is the most basic but most powerful of layers. It has unlimited potential.

You can interact with its canvas attribute, which is a fully functional Imagick class.

Go crazy!

Text Layer

Allows addition and manipulation of text

Adds some additional text-specific properties

$layer = new TextLayer($config);

$layer->text('text');
$layer->font('/path/to/font');
$layer->fontFamily('Times');
$layer->fontSize(16);
$layer->fontWeight(400);
$layer->fontStretch(\Imagick::STRETCH_ANY);
$layer->fontSize(\Imagick::STYLE_NORMAL);
$layer->underColour('#FFF');
$layer->fillColour('rgba(255, 128, 0, 0.5)');
$layer->fillOpacity(0.1);
$layer->strokeColour('hsl(200, 20, 50)');
$layer->strokeWidth(2);
$layer->strokeOpacity(1.0);
Image Layer

This only works with the imageUrl config option at this time.

You can still set the image directly or use a BlankLayer and set your image on that canvas.

This will be fixed.

Gradient Layer

Currently only a start-to-finish top-to-bottom solid-to-solid gradient layer. This will be expanded upon.

$layer = new GradientLayer($config);

$layer->startColour('#FFFFFF');
$layer->endColour('#000000');

There is no start or end or direction yet

Radial Gradient Layer

$layer = new RadialGradientLayer($config);

$layer->startColour('#FFFFFF');
$layer->endColour('#000000');

Like its cousin it's also not 100% implemented, but it provides the most basic of radial gradient that Imagick has to offer.

Solid Layer

This is just a solid colour layer

$layer = new SolidLayer($config);

$layer->colour('#FFFFFF');
Pattern Layer

A layer that tiles a standard Imagick pattern. You can add optional scaling and scale filtering values.

This is not a tiling layer, this uses standard, low res, built in patterns that come with Imagick.

$layer = new PatternLayer($config);

$layer->pattern('bricks');
$layer->scale(2);
$layer->scaleFilter(Imagick::FILTER_BICUBIC);
Plasma Layer

Generates the standard Imagick plasma: style Psuedoimage.

$layer = new PlasmaLayer($config);
Group Layer

This is basically a layer that contains another collection of layers that can each have their own compositing trees, so you can have greater control over your layer composition and manipulation.

$layer = new GroupLayer($config);

$newTextLayer = new TextLayer($config);
$newGradientLayer = new GradientLayer($config);

$layer->group->addLayerToCollection($newGradientLayer, 1);
$layer->group->addLayerToCollection($newTextLayer, 2);

$layer->render();

This needs to get its render triggered when appropriate

Compositing

A big part of the power of this comes from the compositing that is available.

You can add a composite mode to any layer and during render it will be applied.

Note that while you can apply multiple filters to layers, you can only apply a single composite. If you want to achieve multiple composite mixes at once you can achieve this by making a group layer with copies of the same layer in there and composite each copy the way you want.

Supported composite modes

  • Add - The image + the image below
  • Atop - The result is the same shape as image, with composite image obscuring image where the image shapes overlap
  • Blend - Blends the image
  • Bump Map - The same as multiply, except the source is converted to grayscale first.
  • Colour Burn - Darkens the destination image to reflect the source image
  • Colour Dodge - Brightens the destination image to reflect the source image
  • Colourise - Colorizes the target image using the composite image
  • Copy - Copies the source image on the target image
  • Copy Black - Copies black from the source to target
  • Copy Blue - Copies blue from the source to target
  • Copy Cyan - Copies cyan from the source to target
  • Copy Green - Copies green from the source to target
  • Copy Magenta - Copies magenta from the source to target
  • Copy Opacity - Copies opacity from the source to target
  • Copy Red - Copies red from the source to target
  • Copy Yellow - Copies yellow from the source to target
  • Darken - Darkens the target image
  • Destination Atop - The part of the destination lying inside of the source is composited over the source and replaces the destination
  • Destination In - The parts inside the source replace the target
  • Destination Out - The parts outside the source replace the target
  • Destination Over - Target replaces the source
  • Difference - Subtracts the darker of the two constituent colors from the lighter
  • Displace - Shifts target image pixels as defined by the source
  • Dissolve - Dissolves the source in to the target
  • Exclusion - Produces an effect similar to that of Difference, but appears as lower contrast
  • Hard Light - Multiplies or screens the colors, dependent on the source color value
  • Hue - Modifies the hue of the target as defined by source
  • Composite In - Composites source into the target
  • Lighten - Lightens the target as defined by source
  • Luminise - Luminizes the target as defined by source
  • Minus - Subtracts the source from the target
  • Modulate - Modulates the target brightness, saturation and hue as defined by source
  • Multiply - Multiplies the target to the source
  • Composite Out - Composites outer parts of the source on the target
  • Composite Over - Composites source over the target
  • Overlay - Overlays the source on the target
  • Plus - Adds the source to the target
  • Replace - Replaces the target with the source
  • Saturate - Saturates the target as defined by the source
  • Screen - The source and destination are complemented and then multiplied and then replace the destination
  • Soft Light - Darkens or lightens the colors, dependent on the source
  • Source Atop - The part of the source lying inside of the destination is composited onto the destination
  • Source In - The part of the source lying inside of the destination replaces the destination
  • Source Out - The part of the source lying outside of the destination replaces the destination
  • Source Over - The source replaces the destination
  • Subtract - Subtract the colors in the source image from the destination image
  • Threshold - The source is composited on the target as defined by source threshold
  • XOR - The part of the source that lies outside of the destination is combined with the part of the destination that lies outside of the source

TODO before v1 release

  • refactor out var/get/set all using same name
  • z-index management
  • all paramaters below
  • all blend/mix modes below
  • all documentation below

Exaxt PS Layer Blend Modes for Reference

  • Brightness
  • Contrast
  • Saturation
  • Tint
  • Hue

Darken

  • Darken
  • Multiply
  • Colour Burn
  • Linear Burn
  • Darker Colour

Lighten

  • Lighten
  • Screen
  • Colour Dodge
  • Linear Dodge (Add)
  • Lighter Colour

Contrast