<p>I'm attempting to resize pngs with transparent backgrounds in PHP and the code samples I've found online don't work for me. Here's the code I'm using, advice will be much appreciated!</p>

<pre><code>$this-&gt;image = imagecreatefrompng($filename);

imagesavealpha($this-&gt;image, true);
$newImage = imagecreatetruecolor($width, $height);

// Make a new transparent image and turn off alpha blending to keep the alpha channel
$background = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
imagecolortransparent($newImage, $background);
imagealphablending($newImage, false);
imagesavealpha($newImage, true);

imagecopyresampled($newImage, $this-&gt;image, 0, 0, 0, 0, $width, $height,  $this-&gt;getWidth(), $this-&gt;getHeight());
$this-&gt;image = $newImage;  
imagepng($this-&gt;image,$filename);
</code></pre>

<p><br />
<strong>Update</strong>
By 'not working' I meant to say the background color changes to black when I resize pngs.</p>

From what I can tell, you need to set the blending mode to
```
false
```
, and the save alpha channel flag to
```
true
```
before
you do the imagecolorallocatealpha()
```
```
<?php
/**
 * https://stackoverflow.com/a/279310/470749
 * 
 * @param resource $image
 * @param int $newWidth
 * @param int $newHeight
 * @return resource
 */
public function getImageResized($image, int $newWidth, int $newHeight) {
    $newImg = imagecreatetruecolor($newWidth, $newHeight);
    imagealphablending($newImg, false);
    imagesavealpha($newImg, true);
    $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
    imagefilledrectangle($newImg, 0, 0, $newWidth, $newHeight, $transparent);
    $src_w = imagesx($image);
    $src_h = imagesy($image);
    imagecopyresampled($newImg, $image, 0, 0, 0, 0, $newWidth, $newHeight, $src_w, $src_h);
    return $newImg;
}
?>
```
```
UPDATE
: This code is working only on background transparent with opacity = 0. If your image have 0 < opacity < 100 it'll be black background.