title: Fill png transparency with background color
body: <p>I'm refactoring an old image crop/resize library i wrote about 5 years ago and i'm stuck trying to restore one of it's functionalities. The funny part is that i'm not even sure it worked back then since i probably never actually used it.</p>
  
  <p>I need to be able to work on png images while keeping transparency (which works), but i also wan't to be able to fill the transparent part of the image with a color.</p>
  
  <p>Creating a blank image and filling it with a color works fine, but when i try to paste my png over it, the background is transparent again.</p>
  
  <p>Here's a simplified version of my code:</p>
  
  <pre><code> &lt;?php
  
  $src = imagecreatefrompng($pathToSomePngFile);
  
  imagealphablending($src, false);
  imagesavealpha($src, true);
  
  $output = imagecreatetruecolor($width, $height);
  
  if ($backgroundColor) {
      $fillColor = imagecolorallocate(
          $output, 
          $backgroundColor['r'], 
          $backgroundColor['g'], 
          $backgroundColor['b']
      );
  
      imagefilledrectangle(
          $output, 
          0, 
          0, 
          $width, 
          $height, 
          $fillColor
      );
  } else {
      imagealphablending($output, false);
      imagesavealpha($output, true);
  }
  
  imagecopyresampled(
      $output,
      $src,
      0,
      0,
      0,
      0,
      $width,
      $height,
      $width,
      $height
  );
  
  imagepng($output, $pathToWhereImageIsSaved);
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Updated with delboy1978uk's solution to get it to work without changing my other settings.</p>