settings->flag( 'use_binaries' ) ) { return null; } $path = $this->capabilities->all()['binaries'][ $name ] ?? null; return is_string( $path ) && '' !== $path ? $path : null; } /** * Whether PNG output is lossy, which needs pngquant. * * @return bool */ public function lossy_png_active(): bool { return $this->settings->flag( 'png_lossy' ) && null !== $this->path( 'pngquant' ); } /** * Returns the program that encodes a derivative format, if one is in use. * * @param string $format `webp` or `avif`. * @return string|null */ public function tool_for_format( string $format ): ?string { $tool = array( 'webp' => 'cwebp', 'avif' => 'avifenc', )[ $format ] ?? null; return null !== $tool && null !== $this->path( $tool ) ? $tool : null; } /** * Recompresses a temp file in place with whichever programs apply. * * @param string $file File to improve. * @param string $mime Its MIME type. * @return string[] Programs that changed it. */ public function optimize_file( string $file, string $mime ): array { $used = array(); if ( 'image/png' === $mime ) { if ( $this->lossy_png_active() && $this->pngquant( $file ) ) { $used[] = 'pngquant'; } if ( $this->in_place( 'oxipng', array( '--quiet', '--opt', '2', $file ), $file ) ) { $used[] = 'oxipng'; } } elseif ( 'image/jpeg' === $mime ) { if ( $this->in_place( 'jpegoptim', array( '--quiet', '--strip-none', '--all-progressive', $file ), $file ) ) { $used[] = 'jpegoptim'; } } return $used; } /** * Encodes a PNG into a derivative. * * @param string $tool `cwebp` or `avifenc`. * @param string $input Lossless PNG. * @param string $output Output path. * @param EncodeOptions $options Encoding parameters. * @return bool */ public function encode( string $tool, string $input, string $output, EncodeOptions $options ): bool { $path = $this->path( $tool ); if ( null === $path ) { return false; } if ( 'cwebp' === $tool ) { $args = array_merge( array( $path, '-quiet', '-metadata', 'none', '-alpha_q', '100' ), $options->lossless ? array( '-lossless' ) : array( '-q', (string) $options->quality ), array( $input, '-o', $output ) ); } else { $args = array( $path, '--speed', '8', '--qcolor', (string) $options->quality, '--qalpha', '100', '--ignore-exif', '--ignore-xmp', $input, $output ); } return 0 === self::execute( $args, self::TIMEOUT )['code'] && is_file( $output ); } /** * Runs a program without a shell, with a time limit. * * @param string[] $command Program and arguments. * @param int $timeout Seconds before it is stopped. * @return array{code: int, output: string} */ public static function execute( array $command, int $timeout ): array { $failed = array( 'code' => -1, 'output' => '', ); if ( ! function_exists( 'proc_open' ) ) { return $failed; } $pipes = array(); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- A disabled or failing program is an expected outcome. $process = @proc_open( $command, array( 0 => array( 'file', '/dev/null', 'r' ), 1 => array( 'pipe', 'w' ), 2 => array( 'pipe', 'w' ), ), $pipes ); if ( ! is_resource( $process ) ) { return $failed; } stream_set_blocking( $pipes[1], false ); stream_set_blocking( $pipes[2], false ); $deadline = microtime( true ) + $timeout; $output = ''; $code = -1; do { $output .= (string) fread( $pipes[1], 8192 ) . (string) fread( $pipes[2], 8192 ); $status = proc_get_status( $process ); if ( ! $status['running'] ) { $code = (int) $status['exitcode']; break; } if ( microtime( true ) > $deadline ) { proc_terminate( $process, 9 ); break; } usleep( 10000 ); } while ( true ); $output .= (string) stream_get_contents( $pipes[1] ) . (string) stream_get_contents( $pipes[2] ); fclose( $pipes[1] ); fclose( $pipes[2] ); proc_close( $process ); return array( 'code' => $code, 'output' => substr( $output, 0, 4096 ), ); } /** * Applies pngquant. Its quality floor means it declines rather than damage an image. * * @param string $file File to improve. * @return bool Whether it changed the file. */ private function pngquant( string $file ): bool { $path = (string) $this->path( 'pngquant' ); $quality = $this->settings->int( 'png_quality' ); $output = $file . '.quant.png'; $result = self::execute( array( $path, '--force', '--skip-if-larger', '--speed', '3', '--quality', max( 0, $quality - 20 ) . '-' . $quality, '--output', $output, '--', $file ), self::TIMEOUT ); if ( 0 !== $result['code'] || ! is_file( $output ) ) { wp_delete_file( $output ); return false; } return rename( $output, $file ); } /** * Runs a program that rewrites its file, and reports whether the file shrank. * * @param string $tool Program name. * @param string[] $args Arguments. * @param string $file File being rewritten. * @return bool */ private function in_place( string $tool, array $args, string $file ): bool { $path = $this->path( $tool ); if ( null === $path ) { return false; } clearstatcache( true, $file ); $before = (int) filesize( $file ); self::execute( array_merge( array( $path ), $args ), self::TIMEOUT ); clearstatcache( true, $file ); return (int) filesize( $file ) < $before; } }