View source
<?php
namespace Drupal\blazy;
use Drupal\blazy\Utility\NestedArray;
class BlazyBreakpoint implements BlazyBreakpointInterface {
public static function attributes(array &$attributes, array &$settings, $item = NULL) {
if (empty($settings['breakpoints'])) {
return;
}
$srcset = $json = [];
$settings['breakpoints'] = array_reverse($settings['breakpoints']);
foreach ($settings['breakpoints'] as $key => $breakpoint) {
$url = image_style_url($breakpoint['image_style'], $settings['uri']);
if (!empty($settings['_breakpoint_ratio']) && empty($settings['blazy_data']['dimensions'])) {
$dimensions = Blazy::transformDimensions($breakpoint['image_style'], $item);
if ($width = self::widthFromDescriptors($breakpoint['width'])) {
$json[$width] = round($dimensions['height'] / $dimensions['width'] * 100, 2);
}
}
$settings['breakpoints'][$key]['url'] = $url;
if ($settings['background']) {
$attributes['data-src-' . $key] = $url;
}
else {
$width = trim($breakpoint['width']);
$width = is_numeric($width) ? $width . 'w' : $width;
$srcset[] = $url . ' ' . $width;
}
}
if ($srcset) {
$settings['srcset'] = implode(', ', $srcset);
$attributes['srcset'] = '';
$attributes['data-srcset'] = $settings['srcset'];
$attributes['sizes'] = '100w';
if (!empty($settings['sizes'])) {
$attributes['sizes'] = trim($settings['sizes']);
unset($attributes['height'], $attributes['width']);
}
}
if ($json) {
$settings['blazy_data']['dimensions'] = $json;
}
}
public static function widthFromDescriptors($descriptor = '') {
if (empty($descriptor)) {
return FALSE;
}
$descriptor = trim($descriptor);
if (is_numeric($descriptor)) {
return (int) $descriptor;
}
$width = strpos($descriptor, "w") !== FALSE ? str_replace('w', '', $descriptor) : $descriptor;
if (strpos($descriptor, " ") !== FALSE) {
list($width, $px) = array_pad(array_map('trim', explode(" ", $width, 2)), 2, NULL);
if (is_numeric($px) && strpos($width, "x") !== FALSE) {
$width = $px;
}
}
return is_numeric($width) ? (int) $width : FALSE;
}
public static function cleanUpBreakpoints(array &$settings = []) {
if (!empty($settings['breakpoints'])) {
$breakpoints = array_filter(array_map('array_filter', $settings['breakpoints']));
$settings['breakpoints'] = NestedArray::filter($breakpoints, function ($breakpoint) {
return !(is_array($breakpoint) && (empty($breakpoint['width']) || empty($breakpoint['image_style'])));
});
}
}
public static function buildDataBlazy(array &$settings, $item = NULL) {
$settings['blazy'] = TRUE;
if (!empty($settings['blazy_data'])) {
return;
}
Blazy::imageDimensions($settings, $item, TRUE);
$json = $sources = $styles = [];
$end = end($settings['breakpoints']);
foreach ($settings['breakpoints'] as $key => $breakpoint) {
if (blazy()
->isCrop($breakpoint['image_style'])) {
$styles[$key] = TRUE;
}
}
if (count($styles) != count($settings['breakpoints'])) {
return;
}
foreach ($settings['breakpoints'] as $key => $breakpoint) {
if ($width = self::widthFromDescriptors($breakpoint['width'])) {
if (!empty($settings['ratio'])) {
$dimensions = Blazy::transformDimensions($breakpoint['image_style'], $item);
$padding = round($dimensions['height'] / $dimensions['width'] * 100, 2);
$json['dimensions'][$width] = $padding;
if ($end['width'] == $breakpoint['width']) {
$settings['padding_bottom'] = $padding;
}
}
if (!empty($settings['background'])) {
$sources[] = [
'width' => (int) $width,
'src' => 'data-src-' . $key,
];
}
}
}
if ($sources) {
$json['breakpoints'] = $sources;
}
if ($json) {
$settings['blazy_data'] = $json;
}
}
}