Colors & Transparency
Handling of Image Colors
2.9M Downloads / Month
Open Source MIT License
2.9M Downloads / Month
Open Source MIT License
public Image::pickColor(int $x, int $y, int $frame_key = 0): ColorInterface
Reads the color of the pixel specified via the X and Y coordinates. Furthermore, the key of the frame from which the color is taken can be determined. However, this is only relevant for animated images.
Name | Type | Description |
---|---|---|
x | int | X-Coordinate of the pixel position |
y | int | Y-Coordinate of the pixel position |
frame_key | int | Optional key of the frame from which the color information is read |
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
// create new manager instance with desired driver
$manager = new ImageManager(Driver::class);
// read an image
$image = $manager->read('images/animation.gif');
// read color information for frame 10 at position 23/9
$color = $image->pickColor(23, 9, 10);
// 'f3fbe6'
$hex = $color->toHex();
// 'rgb(243, 251, 230)'
$string = $color->toString();
// (int) 243
$red = $color->red()->toInt();
// (int) 251
$green = $color->green()->toInt();
// (int) 230
$blue = $color->blue()->toInt();
// (int) 255
$alpha = $color->alpha()->toInt();
public Image::pickColors(int $x, int $y): CollectionInterface
Reads all colors of the pixel specified via the X and Y coordinates and passes them in a collection. For animated images, this collection will contain all colors of the specified position for all frames. For non-animated images, the collection will contain only one color, since the image also contains only one frame.
Name | Type | Description |
---|---|---|
x | int | X-Coordinate of the pixel position |
y | int | Y-Coordinate of the pixel position |
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Imagick\Driver;
// create new manager instance with desired driver
$manager = new ImageManager(new Driver());
// read an image
$image = $manager->read('images/animation.gif');
// read color information at position
$colors = $image->pickColors(10, 10);
// first color
$color = $colors->first();
// color for certain frame
$color = $colors->get(6);
As soon as the pixel colors of an image have been read into an object, further options are available.
public ColorInterface::toString(): ColorInterface
Each color object can also be output as a string. It is possible to use the
toString()
method or to cast the object directly. Of course, the output
depends on the respective color space.
use Intervention\Image\ImageManager;
use Intervention\Image\Colors\Hsl\Colorspace as HslColorspace;
// create new manager instance with desired driver
$manager = ImageManager::gd();
// read an image
$image = $manager->read('images/example.png');
// read pixel color
$color = $image->pickColor(20, 10);
// transform color to hsl format
$result = $color->toString(); // "rgba(255, 255, 255, 1.0)"
// same result
$result = (string) $color;
public ColorInterface::convertTo(string|ColorspaceInterface $colorspace): ColorInterface
Each color object has a method for converting into other color spaces. Here, the target space can be specified either as a string or as an object. This also works if the driver used does not support the target color space.
The following color spaces are available.
Intervention\Image\Colors\Rgb\Colorspace
Intervention\Image\Colors\Cmyk\Colorspace
Intervention\Image\Colors\Hsv\Colorspace
Intervention\Image\Colors\Hsl\Colorspace
use Intervention\Image\ImageManager;
use Intervention\Image\Colors\Hsl\Colorspace as HslColorspace;
// create new manager instance with desired driver
$manager = ImageManager::imagick();
// read an image
$image = $manager->read('images/example.png');
// read pixel color
$color = $image->pickColor(20, 10);
$result = $color->toString(); // "rgba(0, 255, 255, 1.0)"
// transform color to hsl format
$hslColor = $color->convertTo(HslColorspace::class);
$result = $hslColor->toString(); // "hsl(180, 100, 50)"
The supported colorspaces primarily depend on the driver used. While the library supports with Imagick driver RGB and CMYK colorspaces, the GD driver is limited to the RGB colorspace.
If you are reading CMYK images with Intervention Image using the GD driver the images are transformed to RGB colorspace automatically.
public Image::colorspace(): ColorspaceInterface
This function reads the colorspace from the current image instance.
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
// create new manager instance with desired driver
$manager = new ImageManager(Driver::class);
// reading an image
$image = $manager->read('images/example.jpg');
// read the colorspace object
$colorspace = $image->colorspace();
public Image::setColorspace(string|ColorspaceInterface $colorspace): ImageInterface
This function transforms the current image into the given colorspace. The target colorspace can be specified either as a colorspace object, as a class name, or as an abbreviation for the colorspace.
Name | Type | Description |
---|---|---|
colorspace | string or ColorspaceInterface | Target colorspace |
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Imagick\Driver;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Colors\Cmyk\Colorspace as CmykColorspace;
// create new manager instance with desired driver
$manager = new ImageManager(new Driver());
// reading an image
$image = $manager->read('images/example.jpg');
// transform image to CMYK
$colorspace = $image->setColorspace('cmyk');
// transform image to rgb again
$colorspace = $image->setColorspace(RgbColorspace::class);
// and transform to cmyk again
$colorspace = $image->setColorspace(new CmykColorspace());
Currently Intervention Image is only able to handle color profiles with the
ìmagick
driver.
public Image::profile(): ProfileInterface
This function reads the ICC color profile from the current image instance. If
no profile is found an ColorException
is thrown.
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Imagick\Driver;
// create new manager instance with desired driver
$manager = new ImageManager(new Driver());
// reading an image
$image = $manager->read('images/example.jpg');
// read the icc profile
$profile = $image->profile();
// save profile in file system
$profile->save('my_profile.icc')
public function setProfile(string|ProfileInterface $input): ImageInterface
This function set a given ICC color profile to the current image instance. Usually the function takes the pathname of the profile, but it can also process an object that implements the ProfileInterface.
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Imagick\Driver;
// create new manager instance with desired driver
$manager = new ImageManager(Driver::class);
// reading an image
$image = $manager->read('images/example.jpg');
// set the new color profile
$image->setProfile('profiles/profile.icc');
public function removeProfile(): ImageInterface
Removes all color profiles from the current image.
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Imagick\Driver;
// create new manager instance with desired driver
$manager = new ImageManager(Driver::class);
// read image and remove profile
$image = $manager->read('images/example.jpg')->removeProfile();
Intervention Image supports image formats with alpha channels during decoding and output. Transparent areas are preserved as long as the destination format supports transparency.
If the target format does not support transparency, no alpha channel can be preserved. In this case the transparent areas will be blended with a color. This color can be specified in advance in the initial configuration of the image manager or as an optional argument in the following method.
It is also possible to set or read the blending color at runtime using the following methods.
public function blendingColor(): ColorInterface
Return the currently set blending color as an instance of
ColorInterface::class
. This corresponds either to the color originally
configured via the ImageManager or the blending color that was last set.
public function setBlendingColor(mixed $color): ImageInterface
Set a new blending color in the configuration of the current image instance. This color will be used as the default for all subsequent operations which use blending color and will overwrite the originally defined value in the ImageManager configuration.
Name | Type | Description |
---|---|---|
color | mixed | New blending color in supported color formats. |
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
// create new manager instance with desired driver and red as blending color
$manager = new ImageManager(Driver::class, blendingColor: 'ff0000');
// read image
$image = $manager->read('images/example.png');
// read configured blending color
$blendingColor = $image->blendingColor(); // 'ff0000'
// set grey as blending color
$image->setBlendingColor('cccccc');
// read last set blending color
$blendingColor = $image->blendingColor(); // 'cccccc'
public function blendTransparency(mixed $color = null): ImageInterface
When switching to a non-transparent image format, the transparency is automatically replaced with the configured blending color, but this can also be done manually. This function call can also be applied to image formats that can actually contain transparency.
The color
parameter can optionally be used to specify a color in the common
color formats. By default, this is
the current or previously configured blending color.
Name | Type | Description |
---|---|---|
color | mixed | Color to replace transparent areas (optional). |
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
// create new manager instance with desired driver and default blending color
$manager = new ImageManager(Driver::class);
// read a transparent image
$image = $manager->read('images/example.png');
// merge the transparent areas with orange
$image->blendTransparency('f50');
Edit