Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Dec 12, 2011 @ 14:27
    Bjarne Fyrstenborg
    0

    Calculate width and height of image

    Hi..

    I would like to set the width and height of an image, where I have a fixed width of the image:

    <xsl:for-each select="./productImage/DAMP/mediaItem/Image">
       <xsl:variable name="imgWidth" select="'250'"/>
       <xsl:variable name="scale" select="number(umbracoWidth) div $imgWidth"/>
       <xsl:variable name="imgHeight" select="floor(number(umbracoHeight) div $scale)"/>
       
       Width: <xsl:value-of select="$imgWidth" disable-output-escaping="yes"/>
       Height: <xsl:value-of select="$imgHeight" disable-output-escaping="yes"/>
                
       <href="#">
          <img src="{concat('/ImageGen.ashx?Width=250&amp;Image=',./umbracoFile)}" id="productImage" alt='{$productName}' title="{$productName}" />
       </a>

    </xsl:for-each>

    How can I check if the decimals lower than xx.5 and then use floor, or equal or greater than xx.5 and then use ceiling?

    so if the height is calculated to 274.1779497098646 it should be set to 274 and if the height is calculated to 366.75126903553297 it should be 367.

    Bjarne

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Dec 12, 2011 @ 15:26
    Bjarne Fyrstenborg
    0

    I have tried to use something like this instead of the imgHeight variable:

    <xsl:variable name="tempImgHeight" select="number(umbracoHeight) div $scale"/>
      <xsl:variable name="imgHeight">
        <xsl:choose>
          <xsl:when test="$tempImgHeight &lt; number('#.5')">
            <xsl:value-of select="floor(number(umbracoHeight) div $scale)" disable-output-escaping="yes"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="ceiling(number(umbracoHeight) div $scale)" disable-output-escaping="yes"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    But it doesn't seem to be quite correct, as I either get the height of images only with floor or ceiling method.
    In the example above I get either: 274px and 366px or 275px and 367px, but the sizes of the images I have tested on should be 274px and 267px in height when the width is set to 250px.

    Bjarne

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Dec 12, 2011 @ 16:16
    Bjarne Fyrstenborg
    0

    Okay.. I decided to use the round method instead and is probably better as it automatically rounds the number to the nearest integer..

    <xsl:for-each select="./productImage/DAMP/mediaItem/Image">
       <xsl:variable name="imgWidth" select="'250'"/>
       <xsl:variable name="scale" select="number(umbracoWidth) div $imgWidth"/>
       <xsl:variable name="imgHeight" select="round(number(umbracoHeight) div $scale)"/>
       
       Width: <xsl:value-of select="$imgWidth" disable-output-escaping="yes"/>
       Height: <xsl:value-of select="$imgHeight" disable-output-escaping="yes"/>
                
       <a href="#">
          <img src="{concat('/ImageGen.ashx?Width=250&amp;Image=',./umbracoFile)}" id="productImage" alt='{$productName}' title="{$productName}" />
       </a>

    </xsl:for-each>

    Bjarne

Please Sign in or register to post replies

Write your reply to:

Draft