SO网友:Tobi G.
可以使用添加筛选器add_filter()
勾引wp_generate_attachment_metadata
要教会Wordpress正确调整gif动画图像的大小,请执行以下操作:
/**
* Filters the generated attachment meta data.
* @param array $metadata An array of attachment meta data.
* @param int $attachment_id Current attachment ID.
* @param string $context Additional context. Can be \'create\' when metadata was initially created for new attachment or \'update\' when the metadata was updated.
* @return array
* @author: gresser 2020-11-26
*/
public function resizeGifs(array $metadata, int $attachment_id, string $context)
{
if (preg_match(\'/\\.gif$/\', $metadata[\'file\'])) {
foreach ($metadata[\'sizes\'] as $image_site) {
$file_path_original = wp_upload_dir()[\'basedir\'] .\'/\'. $metadata[\'file\'];
$imagick = new Imagick($file_path_original);
$imagick = $imagick->coalesceImages();
$width = $image_site[\'width\'];
$height = $image_site[\'height\'];
foreach ($imagick as $frame) {
$frame->cropThumbnailImage($width, $height);
$imagick->setImagePage($width, $height, 0, 0);
}
$imagick = $imagick->deconstructImages();
$file_path_thump = wp_upload_dir()[\'path\'] .\'/\'. $image_site[\'file\'];
$imagick->writeImages($file_path_thump, true);
}
}
return $metadata;
}