-
-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor WebP conversion: add cwebp and imagick engines
- Extract WebP conversion logic into separate converter classes - Add CWebPConverter using cwebp binary for high-quality conversion - Add ImagickConverter for servers with ImageMagick support - Keep GDConverter as default fallback option
- Loading branch information
Showing
6 changed files
with
191 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Timber\Image\Converter; | ||
|
||
use Timber\Helper; | ||
|
||
/** | ||
* cwebp CLI implementation | ||
*/ | ||
class CWebPConverter implements IConverter | ||
{ | ||
private $binary_path; | ||
|
||
public function __construct( | ||
private $quality | ||
) { | ||
$this->binary_path = \apply_filters('webp_cwebp_path', '/usr/local/bin/cwebp'); | ||
} | ||
|
||
public function convert($load_filename, $save_filename) | ||
{ | ||
if (!\file_exists($this->binary_path)) { | ||
Helper::error_log('cwebp binary not found at: ' . $this->binary_path); | ||
return false; | ||
} | ||
|
||
$command = \sprintf( | ||
'%s -q %d %s -o %s 2>&1', | ||
\escapeshellcmd($this->binary_path), | ||
$this->quality, | ||
\escapeshellarg((string) $load_filename), | ||
\escapeshellarg((string) $save_filename) | ||
); | ||
|
||
\exec($command, $output, $return_var); | ||
|
||
if ($return_var !== 0) { | ||
Helper::error_log('cwebp conversion failed: ' . \implode(' ', $output)); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Timber\Image\Converter; | ||
|
||
use Timber\Helper; | ||
|
||
/** | ||
* Original GD implementation | ||
*/ | ||
class GDConverter implements IConverter | ||
{ | ||
public function __construct( | ||
private $quality | ||
) { | ||
} | ||
|
||
public function convert($load_filename, $save_filename) | ||
{ | ||
$ext = \wp_check_filetype($load_filename); | ||
if (isset($ext['ext'])) { | ||
$ext = $ext['ext']; | ||
} | ||
$ext = \strtolower((string) $ext); | ||
$ext = \str_replace('jpg', 'jpeg', $ext); | ||
|
||
$imagecreate_function = 'imagecreatefrom' . $ext; | ||
if (!\function_exists($imagecreate_function)) { | ||
return false; | ||
} | ||
|
||
$input = $imagecreate_function($load_filename); | ||
|
||
if ($input === false) { | ||
return false; | ||
} | ||
|
||
if (!\imageistruecolor($input)) { | ||
\imagepalettetotruecolor($input); | ||
} | ||
|
||
if (!\function_exists('imagewebp')) { | ||
Helper::error_log('The function imagewebp does not exist on this server to convert image to ' . $save_filename . '.'); | ||
return false; | ||
} | ||
|
||
return \imagewebp($input, $save_filename, $this->quality); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Timber\Image\Converter; | ||
|
||
/** | ||
* Interface for WebP converters | ||
*/ | ||
interface IConverter | ||
{ | ||
public function convert($load_filename, $save_filename); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Timber\Image\Converter; | ||
|
||
use Exception; | ||
use Imagick; | ||
use Timber\Helper; | ||
|
||
/** | ||
* ImageMagick implementation | ||
*/ | ||
class ImagickConverter implements IConverter | ||
{ | ||
public function __construct( | ||
private $quality | ||
) { | ||
} | ||
|
||
public function convert($load_filename, $save_filename) | ||
{ | ||
if (!\class_exists('Imagick')) { | ||
Helper::error_log('Imagick is not installed on this server.'); | ||
return false; | ||
} | ||
|
||
try { | ||
$image = new Imagick($load_filename); | ||
$image->setImageFormat('webp'); | ||
$image->setImageCompressionQuality($this->quality); | ||
return $image->writeImage($save_filename); | ||
} catch (Exception $e) { | ||
Helper::error_log('Imagick conversion failed: ' . $e->getMessage()); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters