<?php

namespace PHPImageOptim\Tools\Png;

use Exception;
use PHPImageOptim\Tools\Common;
use PHPImageOptim\Tools\ToolsInterface;

class PngCrush extends Common implements ToolsInterface
{
    public function optimise()
    {
        exec($this->binaryPath . ' -rem gAMA -rem cHRM -rem iCCP -rem sRGB -brute -q -l 9 -reduce -ow ' . escapeshellarg($this->imagePath), $aOutput, $iResult);

        if ($iResult != 0)
        {
            throw new Exception('PNGCrush was unable  to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
        }
        return $this;
    }

    public function checkVersion()
    {
        exec($this->binaryPath . ' --version', $aOutput, $iResult);
    }
}

<?php

namespace PHPImageOptim\Tools\Png;

use Exception;
use PHPImageOptim\Tools\Common;
use PHPImageOptim\Tools\ToolsInterface;

class PngCrush extends Common implements ToolsInterface
{
    public function optimise()
    {
        // Pngcrush attempts to write a temporary file to the current directory;
        // make sure we're somewhere we can write a file
        $prevDir = getcwd();
        chdir(sys_get_temp_dir());

        exec($this->binaryPath . ' -rem gAMA -rem cHRM -rem iCCP -rem sRGB -brute -q -l 9 -reduce -ow ' . escapeshellarg($this->imagePath), $aOutput, $iResult);

        // Switch back to previous directory
        chdir($prevDir);

        if ($iResult != 0)
        {
            throw new Exception('PNGCrush was unable  to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
        }

        return $this;
    }

    public function checkVersion()
    {
        exec($this->binaryPath . ' --version', $aOutput, $iResult);
    }
}

Change to a temporary directory before executing pngcrush
Change to a temporary directory before executing pngcrush

Pngcrush writes temporary files in the current working directory.
Sometimes the user running PHP does not have write permissions in the
current working directory, so change to a temporary directory first,
then change back afterwards.
