title: How can you fix transparency turning black on image re-size?
body: <p>I have found many questions about this on SO but none seem to solve my issue.</p>
  
  <p>I need to resize images and save them to specific directories. My class below works exactly as I need it with the exception of transparent images. The transparent images turn black where they should be transparent.</p>
  
  <p>Modified class of <a href="http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/" rel="nofollow noreferrer">SimpleImage</a></p>
  
  <pre><code>&lt;?php
  /**
   * Class ImageResize
   */
  class ImageResize {
      /**
       * Image
       * @var resource
       */
      var $image;
  
      /**
       * Image type
       * @var int
       */
      var $image_type;
  
      /**
       * Loads the image file
       * @param $filename
       */
      function load($filename) {
          $image_info = getimagesize($filename);
          $this-&gt;image_type = $image_info[2];
          unset($image_info);
          if ($this-&gt;image_type == IMAGETYPE_JPEG) {
              $this-&gt;image = imagecreatefromjpeg($filename);
          } elseif ($this-&gt;image_type == IMAGETYPE_GIF) {
              $this-&gt;image = imagecreatefromgif($filename);
          } elseif ($this-&gt;image_type == IMAGETYPE_PNG) {
              $this-&gt;image = imagecreatefrompng($filename);
          }
      }
  
      /**
       * Saves the image file
       * @param $filename
       * @param int $image_type
       * @param int $compression
       * @param null $permissions
       */
      function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 75, $permissions = NULL) {
          if ($image_type == IMAGETYPE_JPEG) {
              imagejpeg($this-&gt;image, $filename, $compression);
          } elseif ($image_type == IMAGETYPE_GIF) {
              imagegif($this-&gt;image, $filename);
          } elseif ($image_type == IMAGETYPE_PNG) {
              imagepng($this-&gt;image, $filename);
          }
          if ($permissions != NULL) {
              chmod($filename, $permissions);
          }
      }
  
      /**
       * Outputs the image file
       * @param int $image_type
       */
      function output($image_type = IMAGETYPE_JPEG) {
          if ($image_type == IMAGETYPE_JPEG) {
              imagejpeg($this-&gt;image);
          } elseif ($image_type == IMAGETYPE_GIF) {
              imagegif($this-&gt;image);
          } elseif ($image_type == IMAGETYPE_PNG) {
              imagepng($this-&gt;image);
          }
      }
  
      /**
       * Gets the image width
       * @return int
       */
      function getWidth() {
          return imagesx($this-&gt;image);
      }
  
      /**
       * Gets the image height
       * @return int
       */
      function getHeight() {
          return imagesy($this-&gt;image);
      }
  
      /**
       * Resize the image to a specified height
       * @param $height
       */
      function resizeToHeight($height) {
          $ratio = $height / $this-&gt;getHeight();
          $width = $this-&gt;getWidth() * $ratio;
          $this-&gt;resize($width, $height);
          unset($ratio, $width);
      }
  
      /**
       * Resize the image to a specified width
       * @param $width
       */
      function resizeToWidth($width) {
          $actualWidth = $this-&gt;getWidth();
          if ($width &lt; $actualWidth) {
              $ratio  = $width / $actualWidth;
              $height = $this-&gt;getheight() * $ratio;
              $this-&gt;resize($width, $height);
          }
          unset($actualWidth, $ratio, $height);
      }
  
      /**
       * Scales the image
       * @param $scale
       */
      function scale($scale) {
          $width  = $this-&gt;getWidth() * $scale / 100;
          $height = $this-&gt;getheight() * $scale / 100;
          $this-&gt;resize($width, $height);
          unset($width, $height);
      }
  
      /**
       * Resize the image
       * @param $width
       * @param $height
       */
      function resize($width, $height) {
          $new_image = imagecreatetruecolor($width, $height);
          imagecopyresampled($new_image, $this-&gt;image, 0, 0, 0, 0, $width, $height, $this-&gt;getWidth(), $this-&gt;getHeight());
          $this-&gt;image = $new_image;
          unset($new_image);
      }
  
  }
  </code></pre>
  
  <p>I have tried these fixes as I have found throughout SO and the all mighty Google:</p>
  
  <p><em>Note:</em> These fixes are added to the <code>resize</code> method.</p>
  
  <p><strong>First Fix:</strong></p>
  
  <pre><code>function resize($width, $height) {
      $new_image = imagecreatetruecolor($width, $height);
  
      // Attempt fix transparency
      if ($this-&gt;image_type == IMAGETYPE_PNG || $this-&gt;image_type == IMAGETYPE_GIF) {
          // Even tried with 255, 255, 255 for white and still no good
          imagecolortransparent($new_image, imagecolorallocatealpha($new_image, 0, 0, 0, 127));
          imagealphablending($new_image, false);
          imagesavealpha($new_image, true);
      }
  
      imagecopyresampled($new_image, $this-&gt;image, 0, 0, 0, 0, $width, $height, $this-&gt;getWidth(), $this-&gt;getHeight());
      $this-&gt;image = $new_image;
      unset($new_image);
  }
  </code></pre>
  
  <p>The first fix does not change the black to white and it makes the image quality very low. So this doesn't seem to work properly.</p>
  
  <p><strong>Second Fix:</strong></p>
  
  <pre><code>function resize($width, $height) {
      $new_image = imagecreatetruecolor($width, $height);
  
      if ($this-&gt;image_type == IMAGETYPE_PNG || $this-&gt;image_type == IMAGETYPE_GIF) {
              $colourBlack = imagecolorallocate($new_image, 0, 0, 0);
              imagecolortransparent($new_image, $colourBlack);
      }
  
      imagecopyresampled($new_image, $this-&gt;image, 0, 0, 0, 0, $width, $height, $this-&gt;getWidth(), $this-&gt;getHeight());
      $this-&gt;image = $new_image;
      unset($new_image);
  }
  </code></pre>
  
  <p>The second fix didn't seem to fix the black background either and the quality wasn't too bad but it could be better.</p>
  
  <p><strong>Third Fix:</strong></p>
  
  <pre><code>function resize($width, $height) {
      $new_image = imagecreatetruecolor($width, $height);
  
      if ($this-&gt;image_type == IMAGETYPE_PNG || $this-&gt;image_type == IMAGETYPE_GIF) {
          imagefill($new_image, 0, 0, imagecolorallocatealpha($new_image, 255, 255, 255, 127)); // Works but horrible quality
      }
  
      imagecopyresampled($new_image, $this-&gt;image, 0, 0, 0, 0, $width, $height, $this-&gt;getWidth(), $this-&gt;getHeight());
      $this-&gt;image = $new_image;
      unset($new_image);
  }
  </code></pre>
  
  <p>The third fix actually replaces the transparency with white but the quality is so bad I can't use it.</p>
  
  <p><strong>My Question:</strong></p>
  
  <p>I'm hoping I missed something but if not is there a way to do this? I am not wanting to use <code>timThumb</code>, <code>phpThumb</code>, or the <code>class.upload.php</code> script. Mainly because of the system I am working with and the way <code>timthumb</code> and <code>phpthumb</code> works is not ideal. The <code>class.upload.php</code> would be too much work to change all the scripts that use this... (old system that I got tossed on...)</p>
  
  <p>Thanks for any help you can provide! :)</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I was finally able to get png transparency to work by doing this (answer found <a href="https://stackoverflow.com/a/6401135/650206">here</a>):</p>
  
  <pre><code>function resize($width, $height) {
      $new_image = imagecreatetruecolor($width, $height);
  
      /* Check if this image is PNG or GIF, then set if Transparent*/
      if(($this-&gt;image_type == IMAGETYPE_GIF) || ($this-&gt;image_type==IMAGETYPE_PNG)){
          imagealphablending($new_image, false);
          imagesavealpha($new_image,true);
          $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
          imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
      }
  
      imagecopyresampled($new_image, $this-&gt;image, 0, 0, 0, 0, $width, $height, $this-&gt;getWidth(), $this-&gt;getHeight());
      $this-&gt;image = $new_image;
      unset($new_image);
  }
  </code></pre>
  
  <p>I still need to figure out how to do this fix for gif</p>