title: Recreated png image is black
body: <p><a href="https://i.stack.imgur.com/ih2Rr.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ih2Rr.jpg" alt="example image"></a></p>
  
  <p>Note : SO has converted the above reference image to jpeg. Here is the <a href="http://pngimg.com/upload/grass_PNG4930.png" rel="nofollow noreferrer">transparent PNG</a>.</p>
  
  <hr>
  
  <p>Following is an example code which recreates a png image <strong>on a new canvas</strong> and preserves the transparency. As you can see, it also allows pixel level manipulation eg. with a custom function like <code>custom_func($r, $g, $b)</code>, which is illustrated better at the bottom of this question. </p>
  
  <p>Basically this code <strong>recreates/redraws</strong> the above image <strong>on a new canvas</strong> successfully as it is. <em>Please note, that sky is completely transparent in the above image</em>.</p>
  
  <pre><code>    $image = imagecreatefrompng('grass.png');
  
      $x_dimension = imagesx($image);
      $y_dimension = imagesy($image);
      $new_image = imagecreatetruecolor($x_dimension, $y_dimension);
  
      // create a transparent canvas
     $trans_color = imagecolorallocatealpha($new_image, 0x00, 0x00, 0x00, 127);
     imagefill($new_image, 0, 0, $trans_color);
  
       for ($x = 0; $x &lt; $x_dimension; $x++) {
            for ($y = 0; $y &lt; $y_dimension; $y++) {
            $rgb = imagecolorat($image, $x, $y);
            $r = ($rgb &gt;&gt; 16) &amp; 0xFF;
            $g = ($rgb &gt;&gt; 8) &amp; 0xFF;
            $b = $rgb &amp; 0xFF;
            $alpha = ($rgb &amp; 0x7F000000) &gt;&gt; 24;
            //$pixel = custom_function($r, $g, $b);
            imagesetpixel($new_image, $x, $y, imagecolorallocatealpha($image, $r, $g, $b, $alpha));
        }
        }
       imagesavealpha($new_image, true);
       imagepng($new_image, 'grass-result.png');
  </code></pre>
  
  <hr>
  
  <p>However when I run the same code on this particular png image below.</p>
  
  <p><a href="https://i.stack.imgur.com/OEHBm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OEHBm.png" alt="problematic png image"></a> </p>
  
  <p>It gives me an almost black image like this.</p>
  
  <p><a href="https://i.stack.imgur.com/5s4sW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5s4sW.png" alt="very dark blue - almost black recreated image"></a></p>
  
  <hr>
  
  <p>I would like to understand what is happening here and why ? Most importantly, I would like to know about possible factors that could influence the process, so I could look into it. Why is it that the result differs from one png to another ? </p>
  
  <p>Ideally, I would like my code to be able to preserve and transfer the transparency state ( transparent, semi-transparent or opaque ) of the source png image, as it is, to the recreated image. As you can see, I have been able to achieve it, except for the case above. </p>
  
  <hr>
  
  <p>Just in case, here is my environment. Windows 7 - 64 bit ** Wampserver2.5 ** Apache-2.4.9 ** Mysql-5.6.17 ** php5.5.12-64b. Also here is a var_dump of the image with <code>getimagesize()</code> :</p>
  
  <pre><code>array (size=6)
    0 =&gt; int 228
    1 =&gt; int 230
    2 =&gt; int 3
    3 =&gt; string 'width="228" height="230"' (length=24)
    'bits' =&gt; int 8
    'mime' =&gt; string 'image/png' (length=9)
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE</strong>
  Here is the proof that the example image is indeed transparent and also that it can be manipulated, while keeping the transparency. Notice the bottom of the image is more brownish now. This was achieved by a slight modification of this line <code>imagesetpixel($new_image, $x, $y, imagecolorallocatealpha($image, 100, $g, $b, $alpha));</code></p>
  
  <p><a href="https://i.stack.imgur.com/kttJT.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kttJT.jpg" alt="manipulated transparent image"></a></p>