Image Formatter
Create image sources JIT style
Description
This expands on Allen’s Ninja XSLT method by matching the src
of an image and resizing using the JIT url structure. This template can be used with inline images from a textarea or called on its own.
All values can be overwritten.
The template can be called like so:
<img alt="{alt/text/item}">
<xsl:call-template name="img-formatter">
<xsl:with-param name="img-src" select="concat(entry/thumb/@path, '/', entry/thumb/filename)" />
<xsl:with-param name="w" select="155" />
<xsl:with-param name="h" select="75" />
</xsl:call-template>
</img>
v1.1
I added support for dynamic post images when those values are set up to be inputted to the image post.
XSLT
View Raw
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- BEGIN: optional if this is already included in another template, this block can be removed --> <xsl:template match="body//*"> <xsl:element name="{name()}"> <xsl:apply-templates select="* | @* | text()"/> </xsl:element> </xsl:template> <xsl:template match="body//@*"> <xsl:attribute name="{name(.)}"> <xsl:value-of select="."/> </xsl:attribute> </xsl:template> <!-- END: optional --> <!-- this template assumes this information: 1. <post-images> is a node within the returned xml 2. images can be given dimensions in 1 of 2 ways: 1. via drop down menu with output set to ###x### ie. 150x75 2. two inputs, one for width, one for height --> <xsl:template match="@src" name="img-formatter" priority="1"> <xsl:param name="crop-mode" select="2" /> <xsl:param name="w"> <xsl:choose> <xsl:when test="$img-val = //post-images/post/entry/image/filename"> <xsl:choose> <xsl:when test="//post-images/post/entry[image/filename = $img-val]/size/item != '0x0'"> <xsl:value-of select="substring-before(//post-images/post/entry[image/filename = $img-val]/size/item, 'x')" /> </xsl:when> <xsl:when test="//post-images/post/entry[image/filename = $img-val]/width"> <xsl:value-of select="//post-images/post/entry[image/filename = $img-val]/width" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="450" /> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise> <xsl:value-of select="450" /> </xsl:otherwise> </xsl:choose> </xsl:param> <xsl:param name="h"> <xsl:choose> <xsl:when test="$img-val = //post-images/post/entry/image/filename"> <xsl:choose> <xsl:when test="//post-images/post/entry[image/filename = $img-val]/size/item != '0x0'"> <xsl:value-of select="substring-after(//post-images/post/entry[image/filename = $img-val]/size/item, 'x')" /> </xsl:when> <xsl:when test="//post-images/post/entry[image/filename = $img-val]/height"> <xsl:value-of select="//post-images/post/entry[image/filename = $img-val]/height" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="300" /> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise> <xsl:value-of select="300" /> </xsl:otherwise> </xsl:choose> </xsl:param> <xsl:param name="crop-pos" select="1" /> <xsl:param name="img-src" select="substring-after(., $workspace)" /> <xsl:attribute name="src"> <xsl:value-of select="concat($root, '/image/', $crop-mode, '/', $w, '/', $h, '/', $crop-pos, $img-src)"/> </xsl:attribute> </xsl:template> </xsl:stylesheet>