PPMage

phpimage processinglibrary

PPMage — lightweight PHP library for applying effects to images.

Demo

An example of the library can be found on this page.

Updates

Usage

include_once('ppmage/PPMage.php');
$image = PPMage::fromFile('source.jpg')->
        // Applying effects
        eInvert(1, 1, 1, 0)->
        eBoxBlur(5, 5)->
        eExtractChannel(2, 0)->
        eInvert(0, 1, 0, 0)->
        // Getting result image
        getImage();
// Saving to file
imagejpeg($image->getData(), 'result.jpg', 85);
// Or show as base64
echo '<img src="data:image/png;base64,', $image->exportAsBase64(), '" alt="base64_image" />';
// Retrieving an image information
echo '<br/>Size: ', $image->getWidth(), 'x', $image->getHeight();
echo '<br/>Type: ', $image->getType();
Image can be loaded from:

Effects

Source image
Source image

Decolour

Converts image to grayscale.
eDecolour()
Decolour

Inverting channels

Inverts one or more selected channel.
eInvert(red [0..1], green [0..1], blue [0..1], opacity [0..1])
// Inverts blue channel
eInvert(0, 0, 1, 0)
Inverts blue channel
// Inverts all channels (negative)
eInvert(1, 1, 1) // parameters are optional
Inverts all channels

Solarize

Solarize effect.
eSolarize()
Solarize

Posterization

ПApplies the effect of posterization (coarsening).

ePosterization(value [0..255])
The higher the value, the rougher the colors.
ePosterization(64)
Posterization
ePosterization(200)
Posterization

Extract channel

Extracts one channel from image (opacity, red, green, blue). It can also convert this channel to grayscale.
NOTE: for opaque images, extracting a transparent channel returns a black image. eExtractChannel(channel [0..3], grayscale [0..1])
// extracts red channel
eExtractChannel(1, 0)
Extract red channel
// extracts green channel
eExtractChannel(2) // you can omit the default parameters
Extract green channel
// extracts blue channel, converting it to grayscale
eExtractChannel(3, 1)
Extract blue channel in grayscale mode

RGB-correction

Changes red, green, blue channel and opacity.

eRGBCorrection(red [-255..255], green [-255..255], blue [-255..255], opacity [-255..255]) NOTE: when changing the transparency, you should save the image in png or gif format, otherwise this channel is ignored.
eRGBCorrection(-70, -18, -221)
RGB-correction
// RGB correction with changeing transparency
eRGBCorrection(-255, -140, 217, -180)
ARGB-correction

HSB-correction

Changes the hue, saturation, and brightness of the image.

eHSBCorrection(hue [0..360], saturation [-100..100], brightness [-100..100], tinting [0..1])

If Tinting> mode is enabled, the image will be colored in the specified hue, otherwise the value of the Hue parameter will be added to each color.

// Change the hue by 259 degrees,
// adding saturation by 10
// and decrease the brightness by 5
eHSBCorrection(259, 10, -5, 0)
Changing the hue
// Image tinting
eHSBCorrection(259, 10, -5, 1)
Tinting

Saturation

Changes the saturation of the image.

eSaturation(value [-255..255])
// Decreasing saturation
eSaturation(-164)
Decreasing saturation
// Increasing saturation
eSaturation(100)
Increasing saturation

Contrast

Changes the contrast of the image.

eContrast(value [-100..100])
// Decreasing contrast
eContrast(-80)
Decreasing contrast
// Increasing contrast
eContrast(60)
Increasing contrast

Gamma

Changes the gamma level of the image.

eGamma(value [-50..50])
// Gamma decreasing
eGamma(-40)
Gamma decreasing
// Gamma increasing
eGamma(25)
Gamma increasing

Blur

Applies a blur effect.

eSmooth(value [1..25]) NOTE: the larger the value, the longer the effect is applied.
eSmooth(6)
Blur

Box blur

Uses a fast box blur in two directions.

eBoxBlur(horizontal [1..100], vertical [1..100])
// Horizonal blur
eBoxBlur(100, 1)
Horizonal blur
// Vertical blur
eBoxBlur(1, 100)
Vertical blur
// Two directional blur
eBoxBlur(20, 20)
Two directional blur

Edge detection

Applies the edge detection effect for four well-known operators (Roberts, Prewitt, Sobel, and Scharr). You can also get the result as colored edges, grayscale edges, or as an overlay of edges on the original image.

eEdgeDetection(operator [0..3], mode [0..2])
// Roberts operator and grayscale edges
eEdgeDetection(0, 1)
Roberts operator and grayscale edges
// Prewitt operator and colored edges
eEdgeDetection(1, 0)
Prewitt operator and colored edges
// Sobel operator and egdes subtraction
eEdgeDetection(2, 2)
Sobel operator and egdes subtraction
// Scharr operator and grayscale edges
eEdgeDetection(3, 1)
Scharr operator and grayscale edges

Scatter

Applies the effect of the scatter of pixels in two directions.

eScatter(horizontal [1..100], vertical [1..100])
// Horizontal scatter
eScatter(40, 1)
Horizontal scatter
// Vertical scatter
eScatter(1, 80)
Vertical scatter
// Two-directional scatter
eScatter(20, 20)
Two-directional scatter

Noise

Overlays the image with noise-colored or black-and-white dots.

eNoise(overlaying [1..255], grayscale [0..1])
// Overlay of colored noise
eNoise(100, 0)
Colored noise
// Overlay of grayscale noise
eNoise(150, 1)
Grayscale noise

Convolution

Applies a convolution matrix to the image. The effect is similar to imageconvolution function, but it is not limited in the size of the matrix. Due to this, you can get a variety of effects.

eConvolution(matrix width, matrix height, matrix (array), divisor, offset, border tracking mode [0..1])
// Sharpness
eConvolution(3, 3, [
     0, -1, 0,
    -1,  5, -1,
     0, -1, 0], 0, 0, 1)
Sharpness
// Soft sharpness (float numbers are used).
eConvolution(3, 3, [
    -1.2, -1, -1.2,
    -1.0, 20, -1.0,
    -1.2, -1, -1.2], 0, 0, 1)
Soft sharpness
// Blur 5x5
eConvolution(5, 5, [
    1,  4,  7,  4, 1,
    4, 16, 26, 16, 4,
    7, 26, 41, 26, 7,
    4, 16, 26, 16, 4,
    1,  4,  7,  4, 1])
Blur 5x5
// Emboss
eConvolution(3, 3, [
    0, -1, 0,
    1,  4, -1,
    0, -1, 0], 0, 127, 1)
Emboss

The overlay of two images

Overlay one image on another with different modes (as in Photoshop).

eImageBlend(image, x, y, overlay mode [0..19], opacity [0..255])
// Overlay a flower.png image over the entire area 
// of the $source image with different overlay modes
$flower = PPMage::fromFile('flower.png')->getImage();
$source = PPMage::fromFile('lena.jpg');
$srcW = $source->getImage()->getWidth();
$srcH = $source->getImage()->getHeight();
$stepY = $flower->getHeight() * 1.1;
$stepX = $flower->getWidth() * 0.9;
$count = 0;
for ($y = -20; $y < $srcH; $y += $stepY) {
    for ($x = -40; $x < $srcW; $x += $stepX) {
        $source = $source->eImageBlend($flower, $x, $y, $count % 20, 128);
        $count++;
    }
}
$image = $source->getImage();
Image overlay
// Overlay flower.png with opacity 128
$flower = // ...
// similarly...
$source = $source->eImageBlend($flower, $x, $y, $count % 20, 128);
$image = $source->getImage();
Semi-transparent image overlay