public function compress(array $args): bool
    {
        if (!class_exists('Imagick')) {
            return false; // Not possible.
        }
        $default_args = [
            'file'   => '',
            'format' => '',

            // For SVGs (1-8).
            'precision' => 0,

            // For PNGs (1-100).
            'min_quality' => 0,
            'max_quality' => 0,

            // For JPGs (1-100).
            'quality' => 0,

            'output_file'   => '',
            'output_format' => '',
        ];
        $args += $default_args; // Defaults.
        $args = $this->parseFormatArgs($args);

        if (!is_file($args['file']) || !$args['format']) {
            return false; // Not possible.
        } elseif (!$args['output_file'] || !$args['output_format']) {
            return false; // Not possible.
        }
        if ($args['output_format'] === 'svg') {
            return $this->compressSvg($args['file'], $args);
        } elseif (mb_stripos($args['output_format'], 'png') === 0) {
            return $this->compressPng($args['file'], $args);
        }
        $args['quality'] = max(0, min(100, (int) $args['quality']));

        $output_file_existed_prior = is_file($args['output_file']);
        $compression_type          = $this->formatToCompressionType($args['output_format']);
        $compression_quality       = $args['quality'] ?: $this->formatToCompressionQuality($args['output_format']);

        try { // Catch exceptions.
            $image = new \Imagick();
            $image->setBackgroundColor('transparent');
            $image->readImage($args['format'].':'.$args['file']);

            $image->stripImage(); // Profiles/comments.
            $image->setImageCompression($compression_type);
            $image->setImageCompressionQuality($compression_quality);
            $image->writeImage($args['output_format'].':'.$args['output_file']);

            return true; // Success.
            //
        } catch (\Throwable $Exception) {
            if (!$output_file_existed_prior && is_file($args['output_file'])) {
                unlink($args['output_file']);
            }
            return false;
        }
    }
public function compress(array $args): bool
    {
        if (!class_exists('Imagick')) {
            return false; // Not possible.
        }
        $default_args = [
            'file'   => '',
            'format' => '',

            // For SVGs (1-8).
            'precision' => 0,

            // For PNGs (1-100).
            'min_quality' => 0,
            'max_quality' => 0,

            // For JPGs (1-100).
            'quality' => 0,

            'output_file'   => '',
            'output_format' => '',
        ];
        $args += $default_args; // Defaults.
        $args = $this->parseFormatArgs($args);

        if (!is_file($args['file']) || !$args['format']) {
            return false; // Not possible.
        } elseif (!$args['output_file'] || !$args['output_format']) {
            return false; // Not possible.
        }
        if ($args['output_format'] === 'svg') {
            return $this->compressSvg($args['file'], $args);
        } elseif (mb_stripos($args['output_format'], 'png') === 0) {
            return $this->compressPng($args['file'], $args);
        }
        $args['quality'] = max(0, min(100, (int) $args['quality']));

        $output_file_existed_prior = is_file($args['output_file']);
        $compression_type          = $this->formatToCompressionType($args['output_format']);
        $compression_quality       = $args['quality'] ?: $this->formatToCompressionQuality($args['output_format']);

        try { // Catch exceptions.
            $image = new \Imagick();
            $image->setBackgroundColor('transparent');
            $image->readImage($args['format'].':'.$args['file']);

            $image->stripImage(); // Profiles/comments.
            $image->setImageCompression($compression_type);
            $image->setImageCompressionQuality($compression_quality);
            $image->writeImage($args['output_format'].':'.$args['output_file']);

            return true; // Success.
            //
        } catch (\Throwable $Exception) {
            if (!$output_file_existed_prior && is_file($args['output_file'])) {
                unlink($args['output_file']);
            }
            return false;
        }
    }
public function compress ( array $ args ) : bool { if ( ! class_exists ( 'Imagick' ) ) { return false ; // Not possible. } $ default_args = [ 'file' => '' , 'format' => '' , // For SVGs (1-8). 'precision' => 0 , // For PNGs (1-100). 'min_quality' => 0 , 'max_quality' => 0 , // For JPGs (1-100). 'quality' => 0 , 'output_file' => '' , 'output_format' => '' , ] ; $ args += $ default_args ; // Defaults. $ args = $ this -> parseFormatArgs ( $ args ) ; if ( ! is_file ( $ args [ 'file' ] ) || ! $ args [ 'format' ] ) { return false ; // Not possible. } elseif ( ! $ args [ 'output_file' ] || ! $ args [ 'output_format' ] ) { return false ; // Not possible. } if ( $ args [ 'output_format' ] === 'svg' ) { return $ this -> compressSvg ( $ args [ 'file' ] , $ args ) ; } elseif ( mb_stripos ( $ args [ 'output_format' ] , 'png' ) === 0 ) { return $ this -> compressPng ( $ args [ 'file' ] , $ args ) ; } $ args [ 'quality' ] = max ( 0 , min ( 100 , ( int ) $ args [ 'quality' ] ) ) ; $ output_file_existed_prior = is_file ( $ args [ 'output_file' ] ) ; $ compression_type = $ this -> formatToCompressionType ( $ args [ 'output_format' ] ) ; $ compression_quality = $ args [ 'quality' ] ? : $ this -> formatToCompressionQuality ( $ args [ 'output_format' ] ) ; try { // Catch exceptions. $ image = new \ Imagick ( ) ; $ image -> setBackgroundColor ( 'transparent' ) ; $ image -> readImage ( $ args [ 'format' ] . ':' . $ args [ 'file' ] ) ; $ image -> stripImage ( ) ; // Profiles/comments. $ image -> setImageCompression ( $ compression_type ) ; $ image -> setImageCompressionQuality ( $ compression_quality ) ; $ image -> writeImage ( $ args [ 'output_format' ] . ':' . $ args [ 'output_file' ] ) ; return true ; // Success. // } catch ( \ Throwable $ Exception ) { if ( ! $ output_file_existed_prior && is_file ( $ args [ 'output_file' ] ) ) { unlink ( $ args [ 'output_file' ] ) ; } return false ; } }
Compress image.

@since 17xxxx Imagick utils.

@param array $args Named arguments.

@return bool True on success.
Compress image .