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".
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.
use sFire\Image\Image;
$image = new Image();
$image -> setDriver('GD');
After setting the driver, you can set an image to work with.
$image = new Image()
$image -> setDriver('GD');
$image -> setImage('/path/to/image.jpg');
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
...
)
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
)
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);
Will return a percentage between 0 and 100.
$percentage = $image -> blackWhite();
echo $percentage; //Outputs similar to "82"
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
)
...
...
)
Below is a list of all the base colors:
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');
Below is a list of available filters.
Reverses all colors of the image.
$image -> negate();
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);
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);
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();
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);
Uses edge detection to highlight the edges in the image.
$image -> edgedetect();
Embosses an image.
$image -> emboss();
Blurs an image using the Gaussian method.
$image -> gaussianblur();
Blurs an image.
$image -> selectiveblur();
Uses mean removal to achieve a "sketchy" effect.
$image -> meanremoval();
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);
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);
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.
$image -> resize(Integer $width, Integer $height, Boolean $ratio, Boolean $interlace);
Resize the image to a new width and height in pixels.
$image -> resize(300, 200);
$image -> save();
Resize an image but keep aspect ratio.
$image -> resize(300, 200, true);
$image -> save();
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();
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');
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);
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.
$image -> crop(Integer $x, Integer $y, Integer $width, Integer $height, Boolean $interlace);
$image -> save();
Crop the image by setting the X and Y position in pixels.
$image -> crop(20, 52, 100, 100);
$image -> save();
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);
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();
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');