Viewed   94 times

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!

$this->image = imagecreatefrompng($filename);

imagesavealpha($this->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->image, 0, 0, 0, 0, $width, $height,  $this->getWidth(), $this->getHeight());
$this->image = $newImage;  

Update By 'not working' I meant to say the background color changes to black when I resize pngs.



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()

 * https://.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.

Wednesday, September 14, 2022

Using imagesavealpha() and a transparent bg color should do the trick...

Based on dfilkovi's code:

// Create a new true color image
$im = new imagecreatetruecolor(100, 100);

// Fill with alpha background
$alphabg = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $alphabg);

// Convert to palette-based with no dithering and 255 colors with alpha
imagetruecolortopalette($im, false, 255);
imagesavealpha($im, true);

// Save the image
imagepng($im, './paletteimage.png');
Monday, November 21, 2022

GD does not support transparency in 32-bit PNG. You have to use either 8-bit with one transparent 'color' or 24-bit (officially 24-bit does not support transparency, but Photoshop can do it when using 'save for web' with 24bit png).

Friday, October 7, 2022

Yes, using an ImageView. Just call

ImageView imageView = new ImageView("...");

By default, it will not preserve the width:height ratio: you can make it do so with


Alternately you can resize the Image directly on loading:

Image image = new Image("my/res/flower.png", 100, 100, false, false);

Resizing the image on loading is useful for things like thumbnails of larger images as the memory required is lower than storing the larger image data representation in memory.

Friday, October 28, 2022

The answer pretty much depends what you need it for. If you are developing a theme and want to keep values constant through all files you can place them in functions.php in the theme directory, which is always loaded. Variables defined there should be available everywhere in the theme. This works if you distribute the theme.

If you want to modify an existing theme for your needs on your own installation, you can either put them in wp-config.php, as suggested, or (a cleaner method) you can create a child theme of the theme you want to change. This will keep it separate from the wordpress core and will prevent theme updates from overwriting your changes.

I just tried it using functions.php:


$variable = "value";


global $variable;
echo $variable;

works for me.

Thursday, October 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :