sFire PHP Framework

GD image driver

sFire\Image\Driver\GD is a driver for sFire\Image\Image and is based on the PHP GD Library.

Note: for this to work, you should have PHP GD installed.

Note: Be careful with large image file size. If an image is to large to process, it can trigger the error "allowed memory size exhausted".

In this section we will handle:

  • Setting the image driver
  • Setting the image
  • Retrieving hexadecimal colors
  • Determine if an image is black and white / grayscale
  • Retrieving the base color(s)
  • Appling image filters
  • Resizing an image
  • Cropping an image

Setting the image driver

To set-up the image driver, you need to inject the driver into a new instance of sFire\Image\Image. You can do so by calling the setDriver method.

Set driver
use sFire\Image\Image;

$image = new Image();
$image -> setDriver('GD');

Setting the image

After setting the driver, you can set an image to work with.

$image = new Image()
$image -> setDriver('GD');
$image -> setImage('/path/to/image.jpg');

Retrieving hexadecimal colors

You can retrieve all hexadecimal colors by calling the getHexColors method. This method will return an Array with the hexedecimal colors and their amount. By default it will round the hexadecimal colors.

$image -> getHexColors();

//Output similar to:
Array
(
    [402020] => 21019
    [808080] => 16489
    [f0f0f0] => 14563
    [604020] => 14559
    [a0a0a0] => 11391
    ...
)
Add a limit to the Array

You can add a limit to the returned Array by giving a number as first parameter.

$image -> getHexColors(3);
Array
(
    [402020] => 21019
    [808080] => 16489
    [f0f0f0] => 14563
)
Disable rounding the colors

To disable the default of rounding the colors for a larger dataset, you can set the second parameter to Boolean false. Now you will get alle the hexadecimal colors in an Array.

$image -> getHexColors(null, false);

Determine if an image is black and white / grayscale

Will return a percentage between 0 and 100.

$percentage = $image -> blackWhite();
echo $percentage; //Outputs similar to "82"

Retrieving the base color(s)

Get base colors

The getBaseColors method will return the most used colors in an image with extra information about the used colors. It will return the hexadecimal value, shade and base color.

$colors = $image -> getBaseColors();
print_r($colors);

//Output similar to:
Array
(
    [0] => stdClass Object
        (
            [r] => 172
            [g] => 172
            [b] => 172
            [h] => 0
            [s] => 0
            [l] => 172
            [hex] => acacac
            [title] => Silver Chalice
            [shade] => stdClass Object
                (
                    [id] => 38
                    [hex] => 949391
                )

            [base] => stdClass Object
                (
                    [id] => 8
                    [hex] => 949391
                    [title] => gray
                )

            [amount] => 19338
        )

    [1] => stdClass Object
        (
            [r] => 110
            [g] => 72
            [b] => 38
            [h] => 20
            [s] => 124
            [l] => 74
            [hex] => 6e4826
            [title] => Pickled Bean
            [shade] => stdClass Object
                (
                    [id] => 2
                    [hex] => 673e0f
                )

            [base] => stdClass Object
                (
                    [id] => 1
                    [hex] => 844204
                    [title] => brown
                )

            [amount] => 3427
        )
    ...
    ...
)
Base colors

Below is a list of all the base colors:

  • Black
  • Brown
  • Red
  • Purple
  • Pink
  • Blue
  • Yellow
  • Orange
  • Gray
  • Green
  • White

Applying image filters

sFire has many built-in filters for you to use. You can change the contract, brightness or even blurr an image by applying filters to an image.

You can apply multiple filters and then call the save method to save the image. You can overwrite the current image or set a new image path by giving a path as parameter:

$image -> negate();
$image -> contrast(80);
$image -> save();
$image -> save('/path/to/new/image');

Filters:

Below is a list of available filters.

Negate

Reverses all colors of the image.

$image -> negate();
Contrast

Changes the contrast of the image. Use the first parameter to set the level of contrast ranges between 0 and 100. Default is 50.

$image -> contrast(80);
Brightness

Changes the brightness of an image. Use the first parameter to set the level of brightness ranges between -255 to 255. Default is 50.

$image -> brightness(70);
Grayscale

Converts an image into grayscale by changing the red, green and blue components to their weighted sum using the same coefficients as the REC.601 luma (Y') calculation. The alpha components are retained.

$image -> grayscale();
Colorize

Like grayscale, except you can specify the color. Use parameter 1, parameter 2 and parameter 3 in the form of red, green, blue and the optional parameter 4 for the alpha channel. The range for each color is 0 to 255.

$image -> selectiveblur(52, 63, 25);
$image -> selectiveblur(52, 63, 25, 10);
Edgedetect

Uses edge detection to highlight the edges in the image.

$image -> edgedetect();
Emboss

Embosses an image.

$image -> emboss();
Gaussianblur

Blurs an image using the Gaussian method.

$image -> gaussianblur();
Selectiveblur

Blurs an image.

$image -> selectiveblur();
Meanremoval

Uses mean removal to achieve a "sketchy" effect.

$image -> meanremoval();
Smooth

Makes the image smoother. Use the first parameter to set the level of smoothness ranges between 0 and 100. Default is 50.

$image -> smooth(60);
Pixelate

Applies pixelation effect to an image. Use the first parameter to set the block size and the second parameter to set the pixelation effect mode. Default blocksize is 5 and default effect mode is 50.

$image -> pixelate(10, 80);

Resizing an image

To resize an image, you can call the resize method. You can set a new width and height, keep the aspect ratio, set a new image quality for improving file sizes, save the new image to a file and create interlaced images.

Syntax
$image -> resize(Integer $width, Integer $height, Boolean $ratio, Boolean $interlace);
Set new width and height

Resize the image to a new width and height in pixels.

$image -> resize(300, 200);
$image -> save();
Keep aspect ratio

Resize an image but keep aspect ratio.

$image -> resize(300, 200, true);
$image -> save();
Interlace

Interlacing (also known as interleaving) is a method of encoding an image such that a person who has partially received it sees a degraded copy of the entire image. When communicating over a slow communications link, this is often preferable to seeing a perfectly clear copy of one part of the image, as it helps the viewer decide more quickly whether to abort or continue the transmission.

Set the last parameter to Boolean true to use interlace.

$image -> resize(300, 200, null, true);
$image -> save();
Save image to new file

You can save the resized image to a new file by adding a path as the first parameter of the save method.

$image -> resize(300, 200, null, null);
$image -> save('/path/to/new/image.png');
Set the image quality

The second parameter of the save method is a value between 0 and 100 for setting the image quality. The lower the image quality, the lower the file size of the new image.

$image -> resize(300, 200);
$image -> save(null, 95);

Cropping an image

To crop an image, you can call the crop method. You can set the X and Y position from where to crop, keep the aspect ratio, set a new image quality for improving file sizes, save the new image to a file and create interlaced images.

Syntax
$image -> crop(Integer $x, Integer $y, Integer $width, Integer $height, Boolean $interlace);
$image -> save();
Set X and Y position from where to crop

Crop the image by setting the X and Y position in pixels.

$image -> crop(20, 52, 100, 100);
$image -> save();
Set the image quality

The second parameter of the save method is a value between 0 and 100 for setting the image quality. The lower the image quality, the lower the file size of the new image.

$image -> crop(20, 52, 150, 100);
$image -> save(null, 95);
Interlace

Interlacing (also known as interleaving) is a method of encoding an image such that a person who has partially received it sees a degraded copy of the entire image. When communicating over a slow communications link, this is often preferable to seeing a perfectly clear copy of one part of the image, as it helps the viewer decide more quickly whether to abort or continue the transmission.

$image -> crop(20, 52, 150, 100, true);
$image -> save();
Save image to new file

You can save the cropped image to a new file by adding a path as the first parameter of the save method.

$image -> crop(20, 52, 150, 100);
$image -> save('/path/to/new/image.png');