stephenjbell

dotCMS - Responsive image from file path

Mar 6th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.04 KB | None | 0 0
  1. <h1>Responsive images from file asset image</h1>
  2.  
  3. #macro(responsiveImageFromPath $fileURL $sizes $altText $resList $focalPoint)
  4.  
  5.     #if(!$UtilMethods.isSet($fileURL))
  6.         ERROR: You need to specify a file URL
  7.     #end
  8.  
  9.     ## Defaults
  10.  
  11.     #if(!$UtilMethods.isSet($altText))
  12.         #set($sizes = "100vw") ## default to full width image
  13.     #end
  14.  
  15.     #if(!$UtilMethods.isSet($altText))
  16.         #set($altText = "")
  17.     #end
  18.  
  19.     #if(!$UtilMethods.isSet($resList))
  20.         #set($resList = [350,600,1000,1500,2000,2500])
  21.     #end
  22.  
  23.     #if(!$UtilMethods.isSet($focalPoint))
  24.         #set($focalPoint = "50% 50%")
  25.     #end
  26.  
  27.     ## Get just the path (strip off file name)
  28.     #set($myFolderUrl = $fileURL.substring(0,$fileURL.lastIndexOf("/")))
  29.     #set($myFolder= $folderAPI.findCurrentFolder("${myFolderUrl}/",$host))
  30.     #set($myFolderInode = $myFolder.inode)
  31.  
  32.     ## Get just the file name (strip off path)
  33.     #set($myFileName = $fileURL.replaceAll("\/([^/]*/)+([^/]*)$","$2"))
  34.  
  35.     ## Use the folder inode and filename to find the file
  36.     #set($images = $dotcontent.pull("+contentType:FileAsset +conFolder:${myFolder.inode} +FileAsset.title:${esc.q}$myFileName${esc.q}",5,"modDate desc"))
  37.  
  38.     #if($images.size() > 1)
  39.         Error: There seems to be more than one image in this folder/host with this filename. Getting the first one.
  40.     #end
  41.  
  42.     ## Get the object, which should be the only thing found
  43.     #set($fileImage = $images.get(0))
  44.  
  45.     #set($jpegQuality = 75)
  46.  
  47.    
  48.     #set($defaultRes = 600)
  49.     #set($defaultSrc = "/contentAsset/image/${fileImage.identifier}/fileAsset/filter/Resize,Jpeg/resize_w/$!{defaultRes}/jpeg_q/$!{jpegQuality}")
  50.  
  51.     #set($originalImgWidth = $fileImage.fileAsset.width) ## https://dotcms.com/docs/latest/content-object-in-velocity
  52.     #set($originalImgHeight = $fileImage.fileAsset.height)
  53.  
  54.  
  55.     ## Add all the image sizes we want to be in srcset
  56.     #set($srcSet = "")
  57.     #foreach($res in $resList)
  58.         #if($res < $originalImgWidth)
  59.             ##set($thisSrc = "/contentAsset/image/${fileImage.identifier}/fileAsset?filter=Resize,Jpeg&resize_w=$!{res}&jpeg_q=$!{jpegQuality}")
  60.             #set($thisSrc = "/contentAsset/image/${fileImage.identifier}/fileAsset/filter/Resize,Jpeg/jpeg_q/$!{jpegQuality}/resize_w/$!{res}")
  61.             #set($srcSet = "${srcSet},${thisSrc} ${res}w")
  62.         #end
  63.     #end
  64.  
  65.     ## Add the original size (JPG compressed) to the srcset
  66.     #set($srcSet = "${srcSet},/contentAsset/image/${fileImage.identifier}/fileAsset/filter/Jpeg/jpeg_q/$!{jpegQuality} ${originalImgWidth}w")
  67.  
  68.     ## Strip off leading comma
  69.     #set($srcSet = $srcSet.replaceAll("^,",""))
  70.  
  71.     ## Strip HTML out of caption, then make sure quotes won't break HTML
  72.     #set($altText = $bigPhoto.caption.replaceAll('<(.|\n)+?>',''))
  73.     #set($altText = $altAttr.replaceAll("$esc.q","&quot;"))
  74.  
  75.     <img src="$defaultSrc" sizes="${sizes}" srcset="$srcSet" width="$originalImgWidth" height="$originalImgHeight" alt="$!altText" title="$!altText" style="object-position:${focalPoint};">
  76. #end
  77.  
  78. #responsiveImageFromPath("/templates/2017/2017-oc-edu/img/clocktower-green.jpg", "(min-width:1024px) 33.3vw, (min-width:640px) 50vw, 100vw", "This is my alt text", [350,600,1000,1500,2000,2500], "33% 50%")
Advertisement
Add Comment
Please, Sign In to add comment