forked from DavidOliver/smart_image_resize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart_image_resize.xsl
53 lines (42 loc) · 1.66 KB
/
smart_image_resize.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="smart-resize-image">
<xsl:param name="xml" /> <!-- incoming XML node -->
<xsl:param name="max-w" select="300"/> <!-- max width (default = 300) -->
<xsl:param name="max-h" select="400"/> <!-- max height (default = 400) -->
<xsl:param name="alt" select="''"/> <!-- alt value (optional, default = '') -->
<xsl:param name="class" /> <!-- class (optional) -->
<!-- determining longest edge of image -->
<xsl:variable name="long-edge">
<xsl:choose>
<xsl:when test="$xml/meta/@width >= $xml/meta/@height">w</xsl:when>
<xsl:otherwise>h</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- image result -->
<xsl:element name="img">
<!-- src value depending on biggest size and limits -->
<xsl:attribute name="src">
<xsl:choose>
<xsl:when test="($long-edge = 'w') and ($xml/meta/@width > $max-w)">
<xsl:value-of select="concat('/image/1/', $max-w,'/0/0', $xml/@path, '/', $xml/filename)"/>
</xsl:when>
<xsl:when test="($long-edge = 'h') and ($xml/meta/@height > $max-h)">
<xsl:value-of select="concat('/image/1/0/', $max-h,'/0', $xml/@path, '/', $xml/filename)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('/workspace', $xml/@path, '/', $xml/filename)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="$alt"/>
</xsl:attribute>
<xsl:if test="$class">
<xsl:attribute name="class">
<xsl:value-of select="$class"/>
</xsl:attribute>
</xsl:if>
</xsl:element>
</xsl:template>
</xsl:stylesheet>