title: Blending colors GD - PHP
body: <p>Ok, I have a 64x64 pixels image, some pixels are white, some are grey, and some darker, so I have another 64x64 pixel image with some yellow pixels which will determine which pixels of the first image must be changed.
  So far I could change the colors on the first image with the following code, but the thing is I have no idea how to "blend" the given color with the colors that were already on the first image.</p>
  
  <p>For example, if a pixel is white (255,255,255) and the new color is red (255,0,0) the result will be (255,0,0) but if the pixel is a bit darker, the new red should also be darker. Any ideas?</p>
  
  <pre><code>$image = 'o1.png';
  $overlay = 'o2.png';
  
  $background = imagecreatefrompng($image);
  
  imagealphablending($background, true);
  
  // Create overlay image
  $overlay = imagecreatefrompng($overlay);
  
  // get size
  $size = getimagesize("o2.png");
  $L=$size[0];
  $H=$size[1];
  
  for($j=0;$j&lt;$H;$j++){
      for($i=0;$i&lt;$L;$i++){
  
          $rgb = imagecolorat($overlay, $i, $j);
  
          $red = (isset($_GET['r']) ? $_GET['r'] : 0);
          $green = (isset($_GET['g']) ? $_GET['g'] : 0);
          $blue = (isset($_GET['b']) ? $_GET['b'] : 0);
  
          $r = ($rgb &gt;&gt; 16) &amp; 0xFF;
          $g = ($rgb &gt;&gt; 8) &amp; 0xFF;
          $b = $rgb &amp; 0xFF;
  
          if(($r==255)&amp;&amp;($g==255)&amp;&amp;($b==0)) {
  
              $color = imagecolorallocate($background, $red, $green, $blue);
              imagesetpixel($background, $i, $j, $color);
  
          }
  
      }
  }
  
  
  header("Content-type: image/png");
  header("Content-Disposition: filename=" . $image);
  
  imagepng($background);
  
  // Destroy the images
  imagedestroy($background);
  imagedestroy($overlay);
  </code></pre>