I am making a program in php
which, given a certain image, it must say how many pixels there are of each color, the problem is that with the code below it gives me an immense number of colors pixel by pixel, in this way:
array(4) { ["red"]=> int(255) ["green"]=> int(127) ["blue"]=> int(38) ["alpha"]=> int(0) }
array(4) { ["red"]=> int(255) ["green"]=> int(127) ["blue"]=> int(38) ["alpha"]=> int(0) }
array(4) { ["red"]=> int(255) ["green"]=> int(127) ["blue"]=> int(38) ["alpha"]=> int(0) }
array(4) { ["red"]=> int(255) ["green"]=> int(127) ["blue"]=> int(38) ["alpha"]=> int(0) }
array(4) { ["red"]=> int(255) ["green"]=> int(127) ["blue"]=> int(38) ["alpha"]=> int(0) }
array(4) { ["red"]=> int(255) ["green"]=> int(127) ["blue"]=> int(38) ["alpha"]=> int(0) }
array(4) { ["red"]=> int(255) ["green"]=> int(127) ["blue"]=> int(38) ["alpha"]=> int(0) }
array(4) { ["red"]=> int(255) ["green"]=> int(127) ["blue"]=> int(38) ["alpha"]=> int(0) }
array(4) { ["red"]=> int(255) ["green"]=> int(127) ["blue"]=> int(38) ["alpha"]=> int(0) }
.....
.....
.....
And what I want is to print for example:
N pixeles de color 222,222,222
N pixeles de color 123,123,123
.....
.....
.....
This is my code:
<html>
<head>
<meta charset="utf-8">
<title>Colores</title>
<link href="hoja_styles.css" rel="stylesheet">
</head>
<body>
<img src="colores.jpg"/><br/><br/>
<?php
$im = imagecreatefromjpeg( "colores.jpg" );
$largo = imagesx( $im );
$ancho = imagesy( $im );
for( $x = 0; $x < $largo; $x++ ) {
for( $y = 0; $y < $ancho; $y++ ) {
$indice = imagecolorat( $im, $x, $y );
$rgb = imagecolorsforindex( $im, $indice );
var_dump( $rgb );
echo "<br />";
}
}
?>
</div>
</body>
</html>
The logic you need would be 1st group and count, 2nd print with the desired format.
This makes it easy for you to manage the content to display it as needed.
The basis is to generate a container that has a
key
unique, such as valuesrgb
.Check at each iteration if it
key
exists. If it exists, increase the counter, and if it doesn't exist, add a newpixel
.Finally, with the container created, we decide what output format to give it and we distribute the content as we are interested.
As additional functions, you can check the number of each row, and assign
pixel
if it is 1 orpixeles
if it is greater than 1.PS.: If there is any mistake, comment and I'll edit.
What you need to do is increment by one a counter that is stored in an array of 256x256x256 elements (or 256x256x256x256 if you also want to include the alpha channel).
Memory consumption will not be a problem because only the necessary memory is reserved for the existing indexes, not for all of them.
Each element of the array stores its RGB component index, so that data is known from the index.
More efficient version
If you want an efficient version, with the minimum number of calls and operations, you can consider this one:
The value returned by
imagecolorat
is an integer whose value in hexadecimal is made up ofAARRGGBB
, where the two most significant bytes are the transparency value (alpha channel), the next two are the R component, then the G, and finally the B component.Results:
Memory consumption is reduced by 22% and execution time by 60%.