Skip to content

Instantly share code, notes, and snippets.

@skerbis
Last active May 15, 2023 13:39
Show Gist options
  • Save skerbis/9a2d6ea20e0bad38361824c9a73110f6 to your computer and use it in GitHub Desktop.
Save skerbis/9a2d6ea20e0bad38361824c9a73110f6 to your computer and use it in GitHub Desktop.
REDAXO CKE5 Uikit-Helper // rewrite images to responsive or Lightbox
<?php
if (rex::isFrontend() || rex_be_controller::getCurrentPage() == 'content/edit' && rex_request('function','string')!=='edit'){
rex_extension::register('OUTPUT_FILTER', static function ($ep) {
$html = $ep->getSubject();
$pattern = '/(<figure[^>]*class="[^"]*image[^"]*"[^>]*style="[^"]*width:)(\d+)(%;[^"]*">)(.*?)(<\/figure>)/s';
$new_html = preg_replace_callback($pattern, function ($matches) {
$width_map = [
'100' => 'uk-width-1-1',
'50' => 'uk-width-1-2@s',
'25' => 'uk-width-1-4@s',
'20' => 'uk-width-1-5@s'
];
$uk_width_class = isset($width_map[$matches[2]]) ? $width_map[$matches[2]] : '';
$figure_start = $matches[1] . $matches[2] . $matches[3];
$figure_start = preg_replace('/ style="[^"]*width:' . $matches[2] . '%;[^"]*"/', '', $figure_start);
$figure_start = preg_replace('/(class="[^"]*)(")/', '$1 ' . $uk_width_class . '$2', $figure_start);
$img = preg_replace('/<img /', '<img class="uk-width-1-1" ', $matches[4]);
return $figure_start . $img . $matches[5];
}, $html);
$html = $new_html;
// Use regular expressions to find linked images in the HTML
preg_match_all('/<figure\b[^>]*\bclass\s*=\s*["\'][^"\']*?\bimage\b[^"\']*["\'][^>]*>.*?<a[^>]+href=[\'"]([^\'"]+?\.(jpg|jpeg|png|mp4|gif))[\'"][^>]*><img[^>]+src=[\'"]([^\'"]+?)[\'"][^>]*>.*?<\/figure>/i', $html, $matches, PREG_SET_ORDER);
// Loop through all the matches
foreach ($matches as $match) {
// Get the matched values
$link = $match[0];
$href = $match[1];
$ext = $match[2];
$src = $match[3];
// Check if the href value ends with .jpg, .jpeg, .png, or .gif
if (in_array($ext, array('jpg', 'jpeg', 'png', 'mp4', 'gif'))) {
// If it's an image, replace the <figure> tag with the updated version
$updated_link = str_replace('<figure ', '<figure uk-lightbox ', $link);
$html = str_replace($link, $updated_link, $html);
}
}
// Output the modified HTML
return $html;
}, rex_extension::LATE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment