/** * Helper method to include lazysizes. Prevent multiple inclusions. */ static function requireLazySizes() { if (self::$_alreadyIncluded) { return false; } $basePath = LazySizesImageExtension::config()->js_path; Requirements::customScript(<<<JS window.lazySizesConfig = { addClasses: true }; JS ); Requirements::javascript($basePath . '/lazysizes.min.js'); self::$_alreadyIncluded = true; }
/** * Requires the necessary JS and sends the required HTML structure to the template * for a responsive image set * * @param array $config The configuration of the responsive image set from the config * @param array $args The arguments passed to the responsive image method, e.g. $MyImage.ResponsiveSet1(800x600) * @param string $method The method, or responsive image set, to generate * @return SSViewer */ protected function createResponsiveSet($config, $args, $method) { LazySizesControllerExtension::requireLazySizes(); if (!isset($config['sizes']) || !is_array($config['sizes'])) { throw new Exception("Responsive set {$method} does not have sizes defined in its config."); } // Resolve size if (isset($args[0])) { $defaultDimensions = $args[0]; } elseif (isset($config['default_size'])) { $defaultDimensions = $config['default_size']; } else { $defaultDimensions = self::config()->default_size; } // Resolve method name if (isset($args[1])) { $methodName = $args[1]; } elseif (isset($config['method'])) { $methodName = $config['method']; } else { $methodName = self::config()->default_method; } $srcset = $this->owner->srcset($config['sizes'], false, $methodName); list($default_width, $default_height) = $this->parseDimensions($defaultDimensions); // Render template $template = 'LazySizesImage'; // If we have a srcset, use a template according to pattern if (!empty($srcset)) { $template .= ucfirst(self::config()->pattern); } // When using lqip, render a low res image $srclqip = ''; if (self::config()->pattern == 'lqip') { $lqip = self::config()->lqip_multiplier; $srclqip = $this->owner->getFormattedImage($methodName, $default_width * $lqip, $default_height * $lqip)->Link(); } return $this->owner->customise(array('ImageSrcSet' => $srcset, 'SrcLqip' => $srclqip, 'DefaultImage' => $this->owner->getFormattedImage($methodName, $default_width, $default_height)))->renderWith($template); }