Copied to clipboard

Flag this post as spam?

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


  • Graham Johnston 4 posts 24 karma points
    Oct 05, 2009 @ 19:24
    Graham Johnston
    0

    Getting image dimensions

    I'm trying write in two functions in an xslt file that I can use later on to get the width and height of an image that's not in the media section. Here are the functions, they work in a standard .html file but when I try to save in umbraco it gives me the following error. Anyone have any ideas? Or know of an alternative way to get image dimensions from images that aren't in the media section.

    Error in XSLT at line 23, char 18
    21:      
    22:      function getImgSizeHeight(imgSrc) {
    23: >>>  var newImg = new Image(); <<<
    24:      newImg.src = imgSrc;
    25:      curHeight = newImg.height;

    <msxsl:script implements-prefix="myFuncs" language="JavaScript"> 
    <![CDATA[

    var curHeight;
    var curWidth;

    function getImgSizeHeight(imgSrc) {
    var newImg = new Image();
    newImg.src = imgSrc;
    curHeight = newImg.height;
    return curHeight;
    }

    function getImgSizeWidth(imgSrc) {
    var newImg = new Image();
    newImg.src = imgSrc;
    curWidth = newImg.width;
    return curWidth;
    }

    ]]>
    </msxsl:script>
  • bob baty-barr 1180 posts 1294 karma points MVP
    Oct 05, 2009 @ 20:08
    bob baty-barr
    1

    Graham -- totally wild... i was JUST wondering how to do this minutes ago :)

    anyway, with the help of my good friend Casey Neehouse i was able to simply add to properties to the docType with the upload on it... i am assuming you are talking about imges uploaded via the upload dataType on a content node???

    anyway, if you add umbracoWidth and umbracoHeight as Lables to the docType you can then simply access them via xslt with data[@alias='umbracoWidth'] for example...

    hope this helps.

  • Graham Johnston 4 posts 24 karma points
    Oct 05, 2009 @ 20:48
    Graham Johnston
    0

    Yeah I think I remember reading that. Do you know if you also need to change the image upload alias to be 'umbracoFilename'? Or will it still work just adding those labels?

    This solves the problem going forward, but I've still got a lot of images already uploaded, so if anyone knows of a way to get those dimensions I'd love to hear it.

  • bob baty-barr 1180 posts 1294 karma points MVP
    Oct 05, 2009 @ 22:12
    bob baty-barr
    0

    if you look at Doug's blog... http://blog.percipientstudios.com i believe his latest post advanced xslt covers getting file size from an image, so extending that should be able to get you what you want from the images...

    also, when i did the umbracoWidth, etc. this morning... my upload alias is pageImage and it worked just fine...

  • Stephan Lonntorp 195 posts 212 karma points
    Oct 05, 2009 @ 23:36
    Stephan Lonntorp
    0

    I don't think umbracoWidth, umbracoHeight, umbracoBytes or umbracoExtension get's saved for the upload datatype, only for the Image media type. If you want to write inline C# in your xslt macro, you'll need to import the namespaces.

    Look at http://our.umbraco.org/wiki/reference/xslt/extend-your-xslt-with-custom-functions for an excellent guide.

  • Graham Johnston 4 posts 24 karma points
    Oct 06, 2009 @ 04:42
    Graham Johnston
    0

    Thanks a bunch guys, lots of great ideas. Bob, I was able to adapt from Doug's blog using c#. Here's what I'm working off of now. Seems to be working for me. I've got some other XSL I'm using to proportionally resize the image based on getting the height and width. Let me know if you'd like to see that also.

    <msxml:script language="CSharp" implements-prefix="myFuncs">
    <msxml:using namespace="System.IO" />
    <msxml:assembly name="System.Web" />
    <msxml:using namespace="System.Web" />
    <msxml:assembly name="System.Drawing" />
    <msxml:using namespace="System.Drawing" />

    <![CDATA[
    public String imageHeight(String filePath) {

    if ((filePath != null) && (filePath.Length != 0)) {
    String localFile = HttpContext.Current.Server.MapPath(filePath);
    if (File.Exists(localFile)) {
    Image img = Image.FromFile(localFile);
    return String.Format("{0:#,###,###.##}",(img.Size.Height));
    }
    }
    return null;
    }

    public String imageWidth(String filePath) {

    if ((filePath != null) && (filePath.Length != 0)) {
    String localFile = HttpContext.Current.Server.MapPath(filePath);
    if (File.Exists(localFile)) {
    Image img = Image.FromFile(localFile);
    return String.Format("{0:#,###,###.##}",(img.Size.Width));
    }
    }
    return null;
    }

    ]]>
    </msxml:script>
  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Oct 06, 2009 @ 11:03
    Douglas Robar
    0

    Perhaps an easier (and more flexible) way of handling resizing is to use ImageGen (http://our.umbraco.org/projects/imagegen). Tons of options and full docs.

    cheers,
    doug.

Please Sign in or register to post replies

Write your reply to:

Draft