pngcrush causing black background on some images

454 views Asked by At

I'm using the following to optimize some png images with pngcrush:

exec(
    "pngcrush -q -blacken -brute -reduce -rem alla -rem text -rem time {$source} {$destination}",
    $output,
    $return_var
);

The optimization works really well, but for some images, the png is creating these black blocks where the transparency goes.

The problem goes away when I remove the -reduce option, but then the PNG optimization does very little, and it's no use at all running the optimization.

I'm assuming that the problem is to do with transparency, so is there a way to tell pngcrush, to skip the -reduce command, only for images that don't have transparency?

2

There are 2 answers

2
Jongware On

Don't use -blacken, as it is the immediate cause of your problem.

From the changelog:

Added -blacken option, to enable changing the color samples of any fully-transparent pixels to zero in PNG files with color-type 4 or 6, potentially improving their compressibility. Note that this is an irreversible lossy change: the underlying colors of all fully transparent pixels are lost, if they were not already black.

Possibly some of your transparent pixels are not as transparent as they seem.

0
Stephen Niedzielski On

I had this issue on v1.7.65 with just the "-reduce" option. The same image and command worked fine on v1.7.86.