View source
<?php
namespace Drupal\blazy;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Site\Settings;
use Drupal\image\Entity\ImageStyle;
class BlazyUtil {
private static $styleId;
public static function generatePlaceholder($width, $height) : string {
return 'data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D\'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg\'%20viewBox%3D\'0%200%20' . $width . '%20' . $height . '\'%2F%3E';
}
public static function sanitize(array $attributes = []) {
$clean_attributes = [];
$tags = [
'href',
'poster',
'src',
'about',
'data',
'action',
'formaction',
];
foreach ($attributes as $key => $value) {
if (is_array($value)) {
$value = implode(' ', $value);
$clean_attributes[$key] = array_map('\\Drupal\\Component\\Utility\\Html::cleanCssIdentifier', explode(' ', $value));
}
else {
$kid = mb_substr($key, 0, 2) === 'on' || in_array($key, $tags);
$key = $kid ? 'data-' . $key : $key;
$clean_attributes[$key] = $kid ? Html::cleanCssIdentifier($value) : Html::escape($value);
}
}
return $clean_attributes;
}
public static function buildUri($image_url) {
if (!UrlHelper::isExternal($image_url) && ($normal_path = UrlHelper::parse($image_url)['path'])) {
$base_path = \Drupal::request()
->getBasePath();
if ($base_path && strpos($normal_path, $base_path) === 0) {
$normal_path = str_replace($base_path, '', $normal_path);
}
$public_path = Settings::get('file_public_path', 'sites/default/files');
if ($public_path && strpos($normal_path, $public_path) !== FALSE) {
$rel_path = str_replace($public_path, '', $normal_path);
return file_build_uri($rel_path);
}
}
return FALSE;
}
public static function isValidUri($uri) {
return !empty($uri) && Blazy::streamWrapperManager() ? Blazy::streamWrapperManager()
->isValidUri($uri) : FALSE;
}
public static function imageUrl(array &$settings) {
$uri = $settings['uri'];
$valid = self::isValidUri($uri);
$styled = $valid && empty($settings['unstyled']);
if ($valid && !empty($settings['image_style']) && ($style = ImageStyle::load($settings['image_style']))) {
$settings['image_url'] = self::transformRelative($uri, $styled ? $style : NULL);
$settings['cache_tags'] = $style
->getCacheTags();
if (empty($settings['_dimensions']) && empty($settings['responsive_image_style'])) {
$settings = array_merge($settings, self::transformDimensions($style, $settings));
}
}
else {
$image_url = $valid ? self::transformRelative($uri) : $uri;
$settings['image_url'] = empty($settings['image_url']) ? $image_url : $settings['image_url'];
}
$data_uri = mb_substr($settings['image_url'], 0, 10) === 'data:image';
if (!empty($settings['_check_protocol']) && !$data_uri) {
$settings['image_url'] = UrlHelper::stripDangerousProtocols($settings['image_url']);
}
}
public static function imageDimensions(array &$settings, $item = NULL, $initial = FALSE) {
$width = $initial ? '_width' : 'width';
$height = $initial ? '_height' : 'height';
$uri = $initial ? '_uri' : 'uri';
if (empty($settings[$width])) {
$settings[$width] = $item && isset($item->width) ? $item->width : NULL;
$settings[$height] = $item && isset($item->height) ? $item->height : NULL;
}
if (empty($settings['image_style']) && empty($settings[$width]) && !empty($settings[$uri])) {
$abs = empty($settings['uri_root']) ? $settings[$uri] : $settings['uri_root'];
if ($data = @getimagesize($abs)) {
list($settings[$width], $settings[$height]) = $data;
}
}
$settings[$width] = empty($settings[$width]) ? NULL : (int) $settings[$width];
$settings[$height] = empty($settings[$height]) ? NULL : (int) $settings[$height];
}
public static function transformDimensions($style, array $data, $initial = FALSE) {
$uri = $initial ? '_uri' : 'uri';
$key = hash('md2', $style
->id() . $data[$uri]);
if (!isset(static::$styleId[$key])) {
$width = $initial ? '_width' : 'width';
$height = $initial ? '_height' : 'height';
$width = isset($data[$width]) ? $data[$width] : NULL;
$height = isset($data[$height]) ? $data[$height] : NULL;
$dim = [
'width' => $width,
'height' => $height,
];
$style
->transformDimensions($dim, $data[$uri]);
if ($dim['width'] != NULL) {
$dim['width'] = (int) $dim['width'];
}
if ($dim['height'] != NULL) {
$dim['height'] = (int) $dim['height'];
}
static::$styleId[$key] = [
'width' => $dim['width'],
'height' => $dim['height'],
];
}
return static::$styleId[$key];
}
public static function transformRelative($uri, $style = NULL) {
$url = $style ? $style
->buildUrl($uri) : file_create_url($uri);
return file_url_transform_relative($url);
}
public static function unstyled(array $settings) {
$extensions = [
'svg',
];
if (isset($settings['unstyled_extensions']) && ($unstyled = $settings['unstyled_extensions'])) {
$extensions = array_merge($extensions, array_map('trim', explode(' ', mb_strtolower($unstyled))));
$extensions = array_unique($extensions);
}
return isset($settings['extension']) && in_array($settings['extension'], $extensions);
}
}