PHP imagecolorsforindex – Retrieve RGBA Components for a Color Index
The article explains PHP’s imagecolorsforindex function, detailing its purpose, parameters, return values, and provides a complete example demonstrating how to load an image, retrieve a pixel’s color index, convert it to RGBA components, and output the result.
The imagecolorsforindex function returns an associative array with keys red, green, blue, and alpha that represent the RGBA components of a specified color index in an image resource.
Parameters
image : The image resource returned by an image creation function such as imagecreatetruecolor().
index : The color index whose RGBA values are to be retrieved.
Return value
On success the function returns an array with the RGBA components; on failure it returns FALSE.
Example
<code><?php
// Open an image
$im = imagecreatefrompng('nexen.png');
// Coordinates of the pixel
$start_x = 40;
$start_y = 50;
// Get the color index at the pixel
$color_index = imagecolorat($im, $start_x, $start_y);
// Convert the index to RGBA components
$color_tran = imagecolorsforindex($im, $color_index);
// Display the color values
echo '<pre>';
print_r($color_tran);
echo ''; ?>
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
