/** * Create the 2x image. * * @param \Craft\AssetFileModel $image * @param \Craft\AssetTransformModel $transform * @return string $markup */ protected function create2xImage(AssetFileModel $image, AssetTransformModel $transform) { // Grab sizes as int. $originalWidth = (int) $image->getWidth(false); $transformWidth = (int) $transform->width; $transformHeight = (int) $transform->height; // Set transform params, uses params set in Craft. $params = ['mode' => $transform->mode, 'width' => $transformWidth * 2, 'height' => $transformHeight * 2, 'quality' => $transform->quality, 'position' => $transform->position]; // Markup for the specified transform. $markup = $image->getUrl($transform->handle); // If original width is bigger than the 2x size for the specified transform, add the srcset. if ($originalWidth >= $transformWidth * 2) { $markup .= '" srcset="' . $image->getUrl($params) . ' 2x'; } return $markup; }