title: PHP - JPEG Image to RGB value Array
body: <p>I am wanting to get an array of RGB values from an image. E.g. (2 X 2 pix example.)</p>
  
  <pre><code>[[[R, G, B], [R, G, B]], [[R, G, B], [R, G, B]]]
  </code></pre>
  
  <p>The code I have now:</p>
  
  <pre><code>&lt;?php
  // open an image
  $image = imagecreatefromjpeg('image.jpg'); // imagecreatefromjpeg/png/
  // get image dimension, define colour array
  $width = imagesx($image);
  $height = imagesy($image);
  $colors = [];
  for ($y = 0; $y &lt; $height; $y++)
  {
      for ($x = 0; $x &lt; $width; $x++)
      {
          $rgb = imagecolorat($image, $x, $y);
          $r = ($rgb &gt;&gt; 16) &amp; 0xFF;
          $g = ($rgb &gt;&gt; 8) &amp; 0xFF;
          $b = $rgb &amp; 0xFF;
      } 
  }
  print_r($colors);
  ?&gt;
  </code></pre>
  
  <p>The above is not working.
  My image is now just a 2 X 2 pix jpeg which should output:</p>
  
  <pre><code>    [[[0, 255, 0], [255, 0, 0]], [[0, 0, 255], [255, 255, 255]]]
  </code></pre>
  
  <p>Any help greatly appreciated!</p>