- PHP ImageMagick Tutorial
- PHP ImageMagick - Home
- PHP ImageMagick - Introduction
- Background Color
- Image Reflections
- Image Cropping
- Image Bordering
- Image Resizing
- Composite Images
- Contrast & Brightness
- Enhancing &Equalizing
- Sharpening & Blurring
- Image Tiling
- Image Threshold
- Modifying Colors
- Different effects
- Securing The Images
- Simulation Of Sketches
- Editing TheAppearance
- Rotation and Rolling
- Splicing & Spreading
- Miscellaneous Functions
- PHP ImageMagick Useful Resources
- PHP ImageMagick - Quick Guide
- PHP ImageMagick - Resources
- PHP ImageMagick - Discussion
PHP ImageMagick - Background Color
The background color of a text of an image refers to the color of its background. Using a suitable color that complements the context of the image or text helps in increasing the readability and even makes easier to scan.
For example, in the image below, we can see that "TUTORIALS POINT" is written on a plain background and is not very readable. Selecting an appropriate background color for this element could improve its visibility.
If we look at the below image, we can observe that this is more readable than the previous image, the main reason for this is its background color.
In the following sections, we will be learning how to identify the background color of an image, and how to set the background color for an image.
Identifying the Background-color
To identify the background color of an image using ImageMagick, we have a method named ‘getImageBackgroundColor()’. If nothing is specified or found in the image, then the default background color is ‘white’.
Syntax
public Imagick :: getImageBackgroundColor() : ImagickPixel
This method doesn’t take any parameters. The return value is in the form of an RGB triplet which is a three-element row vector. Each element specifies the red, green, and blue components of the selected color. So based on that triplet, the user will be able to identify the color that is present in the background.
Example
The following example shows how to use the `getImageBackgroundColor()` method in PHP to get the background color of an image. To execute the code, you'll need a local server environment such as XAMPP.
The code starts by creating an Imagick object and then retrieves the background color with this function. Finally, it prints out an RGB triplet representation of that color on your server.
<?php //creating a new imagick object // $img=new Imagick($_SERVER['DOCUMENT_ROOT'].'/test/image.jpg'); $color=new ImagickPixel($img->getImageBackgroundColor) ;//Get the Background Color $colorInfo = $color->getColorAsString (); //Get the Color from ImagickPixel echo $colorInfo; //display colorinfo as output ?>
Assume that the following is the input image (image.jpg) in the program −
Output
srgb(0, 0, 0)
The output obtained is srgb (0,0,0) which is ‘black’.
Setting a Background color
There are a few cases where we need to set the background colors for the images, especially the images having text to increase their readability. To set the background color of an image in PHP, we use the method ‘setImageBackgroundColor()’.
Syntax
The syntax for this method is shown below −
public Imagick::setImageBackgroundColor(mixed $background): bool
This method accepts 'background' as an argument, which holds the desired background color. Upon successful execution, it returns true and generates an image with the specified background color.
Example
This PHP code snippet shows how to use the `setImageBackgroundColor()` function to set the background color of an image.
First, a new Imagick object is created and the image is read in as input. Then, the background color is set with the built-in function setImageBackgroundColor().
The output can either be displayed on the server by using `echo`, or it can be saved locally with `writeImage()`.
<?php $image=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image.png"); $image->setImageBackgroundColor('black'); $image->setImageAlphaChannel(100); $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/newimage.png"); ?>
Assume that the following is the input image (image.png) in the program −