How to navigate through all the pixels of an image?

I want to skip all the pixels of the image, find the rgba value of this pixel and do something with these pixels.

Example

Say I have a 100x100 pixel image. I want to find the value of each of these pixels with a function already executed:

function getPixel($image, $x, $y) { $colors = imagecolorsforindex($image, imagecolorat($image, $x, $y)); $inrgba = 'rgba(' . $colors['red'] . ',' . $colors['green'] . ',' . $colors['blue'] . ',' . $colors['alpha'] . ')'; return $inrgba; } 

And save these values ​​along with the image size in an array or array of arrays. I want to use the end result in an html page.

How to do it?

+6
source share
2 answers
 for($x=1;$x<=$width;$x++) { for($y=1;$y<=$height;$y++) { $pixel=getPixel($image, $x, $y); //do something } } 

What this will do is find every pixel in every column.

 i=iteration pixel coordinate = (x,y) 

For a 5 x 5 image, the iteration will look like this:

 i1 = (1,1) i2 = (1,2) i3 = (1,3) i4 = (1,4) i5 = (1,5) i6 = (2,1) i7 = (2,2) i8 = (2,3) i9 = (2,4) i10 = (2,5) i11 = (3,1) i12 = (3,2) i13 = (3,3) i14 = (3,4) i15 = (3,5) i16 = (4,1) i17 = (4,2) i18 = (4,3) i19 = (4,4) i20 = (4,5) i21 = (5,1) i22 = (5,2) i23 = (5,3) i24 = (5,4) i25 = (5,5) 
+9
source

Here's the full answer without rollback error

 <?php $src = '2fuse.jpg'; $im = imagecreatefromjpeg($src); $size = getimagesize($src); $width = $size[0]; $height = $size[1]; for($x=0;$x<$width;$x++) { for($y=0;$y<$height;$y++) { $rgb = imagecolorat($im, $x, $y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; var_dump($r, $g, $b); } } 
+2
source

Source: https://habr.com/ru/post/915745/


All Articles