title: Blend transparent gradient with an image using php GD library
body: <p>I try to render gradient on top of an image it need to be from full color to transparent, here is my code. I get black image and if I put start more then 0 I got white gradient but no image. the output image is 338x100 px but the input image need to be aligned to right if the image is narrower.</p>
  
  <pre><code>function hex2rgb($hex) {
      $rgb[0] = hexdec(substr($hex, 0, 2));
      $rgb[1] = hexdec(substr($hex, 2, 2));
      $rgb[2] = hexdec(substr($hex, 4, 2));
      return $rgb;
  }
  
  function int2rgb($color) {
      $result[] = ($color &gt;&gt; 16) &amp; 0xFF;
      $result[] = ($color &gt;&gt; 8) &amp; 0xFF;
      $result[] = $color &amp; 0xFF;
      return $result;
  }
  
  if (isset($_GET['start']) &amp;&amp; isset($_GET['stop']) &amp;&amp; isset($_GET['color'])) {
      $input = imagecreatefrompng('file.png');
      $width = imagesx($input);
      $output = imagecreatetruecolor(338, 100);
      $color = hex2rgb($_GET['color']);
      $fill = imagecolorallocate($output, $color[0], $color[1], $color[2]);
  
  
      for ($x=0; $x&lt;$_GET['start']; ++$x) {
          for ($y=0; $y&lt;100; ++$y) {
              imagesetpixel($output, $x, $y, $fill);
          }
      }
      $range = $_GET['stop']-$_GET['start'];
      for ($x=$_GET['start']; $x&lt;$_GET['stop']; ++$x) {
          $alpha = round(255-($x*255/$range));
          $correct_x = $width &lt; 338 ? $x+$width-338 : $x;
          for ($y=0; $y&lt;100; ++$y) {
              $input_color = int2rgb(imagecolorat($input, $correct_x, $y));
  
              $new_color = imagecolorallocate($output,
                                              (($color[0]-$alpha)*$input_color[0])/255,
                                              (($color[1]-$alpha)*$input_color[1])/255,
                                              (($color[2]-$alpha)*$input_color[2])/255);
              imagesetpixel($output, $x, $y, $new_color);
          }
      }
      if ($_GET['stop']&lt;338) {
          $stop = $width &lt; 338 ? $_GET['stop']+$width-338 : $_GET['stop'];
          imagecopy($input, $output, $stop, 0, $_GET['stop'], 0, 338-$stop, 100);
          header('Content-Type: image/png');
          imagepng($output);
      }
  }
  </code></pre>
  
  <p>I run the script with <code>gradient.php?start=20&amp;stop=200&amp;color=ff0000</code> and got this instead of red gradient.</p>
  
  <p><img src="https://i.stack.imgur.com/rgsWG.jpg" alt="enter image description here">
  <img src="https://i.stack.imgur.com/pKMWn.png" alt="enter image description here"></p>
  
  <p>How to make that gradient red from full color to full transparent? So it look like this:</p>
  
  <p><img src="https://i.stack.imgur.com/p2r9F.png" alt="enter image description here"></p>