PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 10, 2022

[FIXED] How to loop through all the pixels of an image?

 October 10, 2022     gd, php     No comments   

Issue

I want to loop through all the pixels of an image, find the rgba value of that pixel, and do something with those pixels.

Example

Say I have an image that's 100x100 pixels. I want to find the value of each one of those pixels with the function I already made:

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 store those values, along with the dimensions of the image, in an array, or an array of arrays. I want to use the end result in a html page.

How do I do this?


Solution

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 each pixel in each column.

i=iteration
pixel coordinate = (x,y)

For a 5 x 5 image the iteration would 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)


Answered By - bluegman991
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing