Hey everyone,
It is a long time that I’ve posted something about PHP. And I did not really know if I should place this on the Wordpress category or the PHP one. Anyway.
I am writing a service package (it’s like a plugin) for an opensource project that is smushing images using the re-smush API. And it seems to smush the thumbnail, medium_large, medium
sized images.
The problem I am facing is that it won’t smush (optimize) the Large
images during the
wp_generate_attachment_metadata
filter. (the other images seem to work fine).
First I wanted to use the wp_handle_upload
to hook into the original image. But after some thinking, it seems like a bad idea to change the original image.
when I trigger this function $this->smushDemention($image, $key, 'large');
The $image["sizes"][$size]["file"]
does not return anything.
<?php
namespace OffbeatWP\ReSmush;
use OffbeatWP\Services\AbstractService;
use OffbeatWP\ReSmush\Helpers\SmushImage;
use OffbeatWP\Contracts\SiteSettings;
class Service extends AbstractService
{
protected $settings;
public function register(SiteSettings $settings)
{
// --------------------- Add settings page ---------------------
$settings->addPage(AddSettings::class);
// --------------------- WP Filters ---------------------
// add_filter('wp_handle_upload', [$this, 'handleUpload'], 10, 2);
if (setting('re_smush_enabled_thumbnails') == true) {
add_filter('wp_generate_attachment_metadata', [$this, 'handleThumbnails'], 10, 2);
}
// --------------------- Set default quality ---------------------
if (setting('re_smush_image_quality') != null && setting('re_smush_image_quality') != '') {
$this->defaultQuality = setting('re_smush_image_quality');
} else {
$this->defaultQuality = 90;
}
}
public function handleUpload($image)
{
$apiCall = new SmushImage($image['type'], $image['file']);
$apiCall->setQuality($this->defaultQuality);
$apiCall->execute();
return $image;
}
public function handleThumbnails($image, $key)
{
$this->smushDemention($image, $key, 'thumbnail');
$this->smushDemention($image, $key, 'medium_large');
$this->smushDemention($image, $key, 'medium');
$this->smushDemention($image, $key, 'large');
return $image;
}
protected function smushDemention($image, $key, $size)
{
$apiCall = new SmushImage(get_post_mime_type($key), $this->getFile($image, $size));
$apiCall->setQuality($this->defaultQuality);
$apiCall->execute();
}
protected function getBasePath($image)
{
return substr($image["file"], 0, strrpos($image["file"], '/'));
}
protected function getFile($image, $size = 'thumbnail')
{
return wp_upload_dir()['basedir'] . '/' . $this->getBasePath($image) . '/' . $image["sizes"][$size]["file"];
}
}