info ) { return $this->info; } $info = array( 'gd_webp' => function_exists( 'imagewebp' ), 'gd_avif' => function_exists( 'imageavif' ), 'imagick' => class_exists( 'Imagick' ), 'imagick_webp' => false, 'imagick_avif' => false, 'cli_cwebp' => '', 'cli_avifenc' => '', ); if ( $info['imagick'] ) { try { $imagick = new Imagick(); $info['imagick_webp'] = in_array( 'WEBP', (array) $imagick->queryFormats( 'WEBP' ), true ); $info['imagick_avif'] = in_array( 'AVIF', (array) $imagick->queryFormats( 'AVIF' ), true ); } catch ( Exception $e ) { $info['imagick'] = false; } } $settings = ADEE_Plugin::settings(); $info['cli_cwebp'] = $this->find_binary( 'cwebp', isset( $settings['cwebp_path'] ) ? $settings['cwebp_path'] : '' ); $info['cli_avifenc'] = $this->find_binary( 'avifenc', isset( $settings['avifenc_path'] ) ? $settings['avifenc_path'] : '' ); $this->info = $info; return $info; } /** * Locate a CLI binary. * * @param string $name Binary name (cwebp|avifenc). * @param string $override Optional full path or directory override. * @return string Path or ''. */ private function find_binary( $name, $override = '' ) { if ( $override ) { if ( @is_executable( $override ) ) { return $override; } $candidate = rtrim( $override, '/' ) . '/' . $name; if ( @is_executable( $candidate ) ) { return $candidate; } } $escaped = escapeshellarg( $name ); $output = array(); $code = 1; @exec( 'command -v ' . $escaped . ' 2>/dev/null', $output, $code ); if ( 0 === $code && ! empty( $output[0] ) && @is_executable( $output[0] ) ) { return $output[0]; } return ''; } /** * Engines able to encode a given format. * * @param string $format webp|avif. * @return array */ public function available_for( $format ) { $d = $this->detect(); $available = array(); if ( 'webp' === $format ) { if ( $d['gd_webp'] ) { $available[] = 'gd'; } if ( $d['imagick_webp'] ) { $available[] = 'imagick'; } if ( $d['cli_cwebp'] ) { $available[] = 'cli'; } } else { if ( $d['gd_avif'] ) { $available[] = 'gd'; } if ( $d['imagick_avif'] ) { $available[] = 'imagick'; } if ( $d['cli_avifenc'] ) { $available[] = 'cli'; } } return $available; } /** * Pick the best engine for a format (honours manual override). * * @param string $format webp|avif. * @return string Engine key ('gd'|'imagick'|'cli') or '' when none. */ public function pick( $format ) { $available = $this->available_for( $format ); $settings = ADEE_Plugin::settings(); $pref = ( 'webp' === $format ) ? $settings['engine_webp'] : $settings['engine_avif']; if ( 'auto' !== $pref && in_array( $pref, $available, true ) ) { return $pref; } // Auto order: GD is fastest for WebP; Imagick/CLI lead for AVIF. $order = ( 'webp' === $format ) ? array( 'gd', 'imagick', 'cli' ) : array( 'imagick', 'cli', 'gd' ); foreach ( $order as $engine ) { if ( in_array( $engine, $available, true ) ) { return $engine; } } return ''; } }