title: How to replace black background with white when resizing/converting PNG images with transparent backgrounds to JPEG.
body: <p>I am using a script that lets users upload images. The script resizes and converts the images to JPEG.</p>
  
  <p>The problem I have is when a PNG with transparency is uploaded, the resulting JPEG image is black where there was transparency.</p>
  
  <p>How can I edit the below script to replace the black with white? It already does this for GIF's but not for PNG's.</p>
  
  <pre><code> // RESIZE IMAGE AND PUT IN USER DIRECTORY
    switch($this-&gt;file_ext)
  {
      case "gif":
        $file = imagecreatetruecolor($width, $height);
        $new = imagecreatefromgif($this-&gt;file_tempname);
        $kek=imagecolorallocate($file, 255, 255, 255);
        imagefill($file,0,0,$kek);
        imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this-&gt;file_width, $this-&gt;file_height);
        imagejpeg($file, $photo_dest, 100);
        ImageDestroy($new);
        ImageDestroy($file);
        break;
  
      case "bmp":
        $file = imagecreatetruecolor($width, $height);
        $new = $this-&gt;imagecreatefrombmp($this-&gt;file_tempname);
        for($i=0; $i&lt;256; $i++) { imagecolorallocate($file, $i, $i, $i); }
        imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this-&gt;file_width, $this-&gt;file_height); 
        imagejpeg($file, $photo_dest, 100);
        ImageDestroy($new);
        ImageDestroy($file);
        break;
  
      case "jpeg":
      case "jpg":
        $file = imagecreatetruecolor($width, $height);
        $new = imagecreatefromjpeg($this-&gt;file_tempname);
        for($i=0; $i&lt;256; $i++) { imagecolorallocate($file, $i, $i, $i); }
        imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this-&gt;file_width, $this-&gt;file_height);
        imagejpeg($file, $photo_dest, 100);
        ImageDestroy($new);
        ImageDestroy($file);
        break;
  
      case "png":
        $file = imagecreatetruecolor($width, $height);
        $new = imagecreatefrompng($this-&gt;file_tempname);
        for($i=0; $i&lt;256; $i++) { imagecolorallocate($file, $i, $i, $i); }
        imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this-&gt;file_width, $this-&gt;file_height); 
        imagejpeg($file, $photo_dest, 100);
        ImageDestroy($new);
        ImageDestroy($file);
        break;
    } 
  
    chmod($photo_dest, 0777);
  
    return true;
  }
  </code></pre>
  
  <p>I tried editing the <strong>case "png":</strong> portion to match that of the <strong>case "gif":</strong> code but the resulting JPEG is completely white.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I fixed it myself.</p>
  
  <p>Thanks, Everyone, for contributing!</p>
  
  <p>I replaced:</p>
  
  <pre><code>case "png":
        $file = imagecreatetruecolor($width, $height);
        $new = imagecreatefrompng($this-&gt;file_tempname);
        for($i=0; $i&lt;256; $i++) { imagecolorallocate($file, $i, $i, $i); }
        imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this-&gt;file_width, $this-&gt;file_height); 
        imagejpeg($file, $photo_dest, 100);
        ImageDestroy($new);
        ImageDestroy($file);
        break;
  </code></pre>
  
  <p>with:</p>
  
  <pre><code>case "png":
        $file = imagecreatetruecolor($width, $height);
        $new = imagecreatefrompng($this-&gt;file_tempname);
        $kek=imagecolorallocate($file, 255, 255, 255);
        imagefill($file,0,0,$kek);
        imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this-&gt;file_width, $this-&gt;file_height);
        imagejpeg($file, $photo_dest, 100);
        ImageDestroy($new);
        ImageDestroy($file);
        break;
  </code></pre>