Author:
andrrr
Version:
1.0
Release Date:
2 Dec 2009
Category:
Output

Description

This utility does proportional image resizing (using JIT, of course), depending on image’s meta information and desired max width and height. If image’s dimentions are both lower than maximum limits, utility just returns original image without resizing.

Example of utility call:

            <xsl:template match="entry">
                <h3><xsl:value-of select="entry-title"/></h3>

                <xsl:call-template name="smart-resize-image">
                    <xsl:with-param name="xml" select="entry-image"/>
                    <xsl:with-param name="max-w" select="150"/>
                    <xsl:with-param name="max-h" select="100"/>
                    <xsl:with-param name="alt" select="entry-title"/> <!-- (Optional) -->
                    <xsl:with-param name="class" select="'entry-img'"/> <!-- (Optional) -->
                </xsl:call-template>
            </xsl:template>

XSLT

View Raw
<?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="'image'"/> <!-- Alt value (optional, default = 'image') -->
  <xsl:param name="class" /> <!-- class (optional) -->

  <!-- Calculating biggest size of an image -->
  <xsl:variable name="b-side">
    <xsl:choose>
      <xsl:when test="$xml/meta/@width &gt;= $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="($b-side = 'w') and ($xml/meta/@width &gt; $max-w)">
          <xsl:value-of select="concat('/image/1/', $max-w,'/0/0/', $xml/@path, '/', $xml/filename)"/>
        </xsl:when>
        <xsl:when test="($b-side = 'h') and ($xml/meta/@height &gt; $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>

Discuss this XSLT Utility

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details