title: PHP-GD: preserve half-transparent areas
body: <p>I'm in need of a generic image upload for a PHP site. Photos and Logos should be re-sized to a certain extend to make sure they're not too big and fit the design.</p>
  
  <p>I'm trying it with this code:</p>
  
  <pre><code>function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
  
      if($this-&gt;image_type == PNG or $this-&gt;image_type == GIF) {
          imagealphablending($new_image, false);
          imagesavealpha($new_image,true);
          $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
          imagefilledrectangle($new_image, 0, 0, $nWidth, $nHeight, $transparent);
      }
  
      imagecopyresized($new_image, $this-&gt;image, 0, 0, 0, 0, $width, $height, $this-&gt;getWidth(), $this-&gt;getHeight());
      $this-&gt;image = $new_image;
  }
  </code></pre>
  
  <p>However, when I upload an image that has areas with alpha-values between 0 and 255, they get replaced by a complete black, turning anti-aliased areas to a black border.</p>
  
  <p>The full transparency works fine for PNG and GIF, merely the half-transparent areas are a problem.</p>
  
  <p>I apologise if I'm not using the correct terms to explain my problem, maybe that's why I hardly found anything on it.</p>