# wp-optimize/4.5.4/webp/class-wpo-webp-convert.php

WP-Optimize – Cache, Compress images, Minify &amp; Clean database to boost page speed &amp; performance, version 4.5.4. 70 lines.

- Page: https://pluginprobe.com/plugins/wp-optimize/4.5.4/code/webp/class-wpo-webp-convert.php
- Raw: https://pluginprobe.com/plugins/wp-optimize/4.5.4/raw/webp/class-wpo-webp-convert.php
- Modified: 2026-05-25T08:53:46+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wp-optimize/4.5.4/code/webp/class-wpo-webp-convert.php#L10-L20`.

```php
<?php

if (!defined('ABSPATH')) die('No direct access allowed');

if (!class_exists('WPO_WebP_Convert')) :

/**
 * Handles WebP conversion by iterating through available converters.
 */
class WPO_WebP_Convert {

	/**
	 * @var array<string>
	 */
	private $converters = array();

	private function __construct() {
		$webp_converters = WP_Optimize()->get_options()->get_option('webp_converters');
		$this->converters = false === $webp_converters ? array() : $webp_converters;
	}

	/**
	 * Singleton instance
	 *
	 * @return WPO_WebP_Convert
	 */
	public static function get_instance(): self {
		static $instance = null;
		if (null === $instance) {
			$instance = new self();
		}
		return $instance;
	}

	/**
	 * Convert an image file to WebP format using the first available converter that succeeds.
	 *
	 * @param string $source Path of the source file.
	 * @return bool True if conversion succeeded, false otherwise.
	 */
	public function convert($source): bool {
		if (empty($this->converters)) {
			return false;
		}

		$destination = WPO_WebP_Utils::get_destination_path($source);

		return $this->attempt_conversion($source, $destination);
	}

	/**
	 * Try each configured converter in order until one produces a WebP file.
	 *
	 * @param string $source      Path of the source file.
	 * @param string $destination Path of the destination file.
	 * @return bool True if a converter produced the destination file.
	 */
	private function attempt_conversion($source, $destination): bool {
		foreach ($this->converters as $converter) {
			WPO_WebP_Utils::perform_webp_conversion($converter, $source, $destination);
			if (is_file($destination)) {
				return true;
			}
		}

		return false;
	}
}
endif;

```
