You should be able to get a hold of the image in Razor using the Model.XPath method. Taken from the Razor Walkthrough guide by Gareth Evans:
XPath helper
Sometimes .Where isn't enough to select the node you want - it only works on the current node, and you can't easily check Parents/Children We've added a .XPath helper which lets you use XPath to select the nodes. The XPath you use is used to select nodes from the original XML which are then upgraded to NodeFactory.Node and then DynamicNode
@foreach(var item in @Model.XPath("//ChildItem[catCount > 4 and count(.//catPictures) > 0]").Random(4)) { @item.Name }
The .XPath helper also works on a DynamicXml item (from an XML property) but won't do the upgrade to DynamicNode/DynamicNodeList like when you call it directly on @Model.
The easiest way to do this is to use the uComponents extensions...
e.g (with filler removed):
@using uComponents.Core.uQueryExtensions;
var imageNode = new Media(@imageId); var url = @imageNode.GetImageCropperUrl("crops", "Tile"); if (string.IsNullOrEmpty(url)) { url = @imageNode.getProperty("umbracoFile").Value.ToString(); }
you don't need that stuff. I'll introduce you how to simple use just razor
1. Create a document type: i called mine image with text / properties simple Text / Image with type upload and named it umbracoFile 2. Configure your image cropper and say that it should look for umbracoFile
But i prefere to use this in combination with the Digibiz Advanced Media Picker. Solutions are writte in the related forum.
Bye and have fun experimenting razor is very powerful and in combination with Entity Framework or the strong helpers it makes a lot of fun to work with.
@Toni Becker Does this rely on using the media section? Cause I don't really want to use a media picker. I'd rather just upload the image and then crop it all right there in my members admin area.
So I have these Member Types: File Uploader with the alias = facultyHeadshot Image Cropper with the alias = cropDirectoryImage and my specific crop alias is = thumbnail
I haven't been able to plug these values into your script and get it to pull out any information. Could you show me using these values?
Hello Toni, I looked up my umbraco.config file but none of the information about my members show up in there. Is there a different way I should be looking to extract the XML?
hmmm. you could read @ asp.net/webmatrix or asp.net/mvc there are some usefull tips to work with razor and data sources. I'm working with an integrated entity framework in umbraco so i can easily acces my data and store it in a list or a data object to work and minpulate it.
I think it's very powerful instead of using ultra-long lines of razor code.
You could read it and your development skills will boost up a lot :) and makes your life easier.
Ah, looks like I let a little mistake slip in. If you have a look at the code: it's asking for the crop that is on the property ImageUpload. But that's not where the crop is, it's in the ublogsyCrop property, so this should help:
var crop = GetImageCropperUrlFullMedia(d.GetProperty("ublogsyCrop").Value, "ublogsyThumb");
Also, you MIGHT have to change .Value to .Value.ToString()
Just to be clear, you got the null reference error because you were asking for something that doesn't exist (the crop) on that particular property. So you don't need the extra null and empty string checks that I suggested earlier.
Hi Sebastiaan Janssen I was so excited about the code I went back to the office on Saturday. I'm affraid that the error is gone now.. but no value comes up when I call @crop.
Okay, we're getting there. So HELLO shows up, right?
I've just tested my code and it works fine with your XML. So the problem is most likely in the getProperty part. Do you get a string of XML back when you do:
to my file and all 4 lines returned the correct information viewable when I viewed the source code of my page. But displayed nothing on the actual page itself. The result looked like this:
If I wanted to have this information actually show on my page I could write it like this.
@d.GetProperty("ublogsyCrop").Value.ToString()
which worked, but I am still unable to only call the URL for the cropped image. When i tried your @if statement I get No result. If I add <text>hello</text> I am able to see the hello part.
As far as the reason for the d. variable.. Im not exactly sure eithe,r but I am working from inside a ublogsy file and just following the code that is already in there. if you want to try to experience the full problem you could install uBlogsy and add an upload and image cropper datatype to the Blog Post DocType and see if you could call the crops. But we can see it in the XML and I can now call the string (in full anyway) just haven't been able to isolate the URL for the cropped image.
Once we get this solved I really hope it works in the members section too cause I have crops in razor over there too..
Hi, I also had a problem with image cropper, in my case I got a "root element missing" xml error message and it took me quite a while to realize that it was because I was iterating over several images, and one of them did not have the actual cropper name. Anyways, to solve it I copied parts of Sebastiaans code and added a try catch to help me. Dunno if that might help you also, but something like this:
var imageUrl = ""; try { var mediaXml = d.GetProperty("ublogsyCrop").Value.ToString(); xmlDocument.LoadXml(mediaXml); var cropNode = xmlDocument.SelectSingleNode("descendant::crops/crop[@name='ublogsyThumb']"); if (cropNode != null) { imageUrl = cropNode.Attributes.GetNamedItem("url").InnerText; } } catch (Exception ex) { <text>@ex.Message</text> } <img src="@imageUrl"/>
section at all at the top of the page that Sebastiaan Janssen provided? I keep getting errors with your script that says that certain items don't exist.
Hi Jonas.. well when I drop in your code I get no result... I can add in "hello" text inside your div and see that text appear on my page so I know where it should be popping up.. but still I get no imagecropper URL...
I have the GetImageCropperUrlFullMedia function at the top of my cshtml file which looks like this:
Yes, Sebastiaan Janssen worked with me forever on this problem. He's been a great help all throughout my frist Umbraco Project.
Well my full template script is a little crazy but here are the important parts to get the image cropper thumbs: At the top of the Razor template make sure you have this:
Ha.. well that sure would have been nice.. but on the bright side.. I think I probably learned a lot by dissecting this thing line by line so many times... probably some good experience for me on my first project.. Thanks again for all your help... I know I have a few more major hurdles before I can launch this bad boy.. but trying to make progress everyday
DynamicNode d; if(PageData.Count>0){ d =new DynamicNode(PageData[0]); } else{ d =new DynamicNode(Model.Id); }
that looks like you are able to call the script from another page (or from a template) with a reference to a node (other than the current) with the actual cropped image. That would explain why Model and d is actually different.
Yeah, I worried that it might be hendering me.. I don't know if you guys have checked out the package "uBlogsy" yet. its a Razor based Blog for Umbraco. For the most part I am really enjoying it. However I am still in my n00b stage and understanding all of the pre-existing code is a little over my head.. I am just trying to work within the guidelines that it came with..
https://gist.github.com/1170848 is an excellent link but I have two images in my news items (image1 & image2) and I just want the crop called "News Item" off the first image. I can't seem to do
var newsImage = newsItem.image1.imageCropper.Find("name", "News Item").url;
Image Cropper images called in Razor
Hello Everyone! ( I am using Umbraco 4.7)
I need to call my cropped images into my template which is using RAZOR. Usually I write it out like this in XLST:
But I'm not sure how to convert that properly to work with my RAZOR scripts. Right now I am just calling the full uploaded image by:
I've been looking around but haven't been able to find any solid solutions. Anyone have a simple solution out there?
Hiya :-)
You should be able to get a hold of the image in Razor using the Model.XPath method. Taken from the Razor Walkthrough guide by Gareth Evans:
XPath helper
Sometimes .Where isn't enough to select the node you want - it only works on the current node, and you can't easily check
Parents/Children
We've added a .XPath helper which lets you use XPath to select the nodes.
The XPath you use is used to select nodes from the original XML which are then upgraded to NodeFactory.Node and then
DynamicNode
The .XPath helper also works on a DynamicXml item (from an XML property) but won't do the upgrade to DynamicNode/DynamicNodeList like when you call it directly on @Model.
Link: http://umbraco.com/follow-us/blog-archive/2011/2/28/umbraco-razor-feature-walkthrough-%E2%80%93-part-3
All the best,
Bo
The easiest way to do this is to use the uComponents extensions...
e.g (with filler removed):
That's all there is to it.
@Robert Foster I have uComponents installed...
So if my:
cropper alias = cropDirectoryImage
The specific crop = thumbnail
how should it look to output this?
I'm still getting errors with it.
you don't need that stuff.
I'll introduce you how to simple use just razor
1. Create a document type: i called mine image with text / properties simple Text / Image with type upload and named it umbracoFile
2. Configure your image cropper and say that it should look for umbracoFile
Here's my output XML in umbraco.config
So now i can easily acces my xml via razor without using hacky xpath or other components.
Than you could put this snippet into your template to render out original image and the crop:
And here's my result:
But i prefere to use this in combination with the Digibiz Advanced Media Picker. Solutions are writte in the related forum.
Bye and have fun experimenting razor is very powerful and in combination with Entity Framework or the strong helpers it makes a lot of fun to work with.
@Toni Becker Does this rely on using the media section? Cause I don't really want to use a media picker. I'd rather just upload the image and then crop it all right there in my members admin area.
So I have these Member Types:
File Uploader with the alias = facultyHeadshot
Image Cropper with the alias = cropDirectoryImage
and my specific crop alias is = thumbnail
I haven't been able to plug these values into your script and get it to pull out any information. Could you show me using these values?
can you post an extract from your xml? it's better to analyze the structure
Hello Toni, I looked up my umbraco.config file but none of the information about my members show up in there. Is there a different way I should be looking to extract the XML?
hmmm. you could read @ asp.net/webmatrix or asp.net/mvc there are some usefull tips to work with razor and data sources.
I'm working with an integrated entity framework in umbraco so i can easily acces my data and store it in a list or a data object to work and minpulate it.
I think it's very powerful instead of using ultra-long lines of razor code.
You could read it and your development skills will boost up a lot :) and makes your life easier.
Ok.. I got distracted will some other stuff but now I need to figure this Image Cropper in Razor thing out..
I also got the XML for you to look at:
in this particular case I am calling an image in a post that I have created using uBlogsy. I can call the full scale image like this:
But what I want to do is call the "ublogsyThumb"... and I am not sure how to grab that one using Razor...
This will be much easier in 4.7.1 but for now I have a helper method that you can call like this:
In your Razor file add these usings at the top (not sure if you will need them all):
And add this helper function:
Hmmm got an error:
Error loading Razor Script uBlogsyShowPost.cshtml
Object reference not set to an instance of an object.
Here's what I have at the top of the page..
And then I call the image further down like this:
It looks like one of the crops/images might not be available.. Try:
Thanks for checking back with me... I've been working with your example... I am able to save the template.. but the actual page errors out with:
Error loading Razor Script uBlogsyShowPost.cshtml
Data at the root level is invalid. Line 1, position 1.
You have the line 1 position 1 script ? I you can copy here to check it !
There has to be something wrong with the Function part at the top of that file..(see below)
later on on the file, I am trying to call the cropped image like this
I only get the error on pages that have an image.. soo
But this is driving me nuts..
My XML looks like this for example:
If anyone could provide a solution using that info... THAT WOULD BE GREAT!
Ah, looks like I let a little mistake slip in. If you have a look at the code: it's asking for the crop that is on the property ImageUpload. But that's not where the crop is, it's in the ublogsyCrop property, so this should help:
Also, you MIGHT have to change .Value to .Value.ToString()
Just to be clear, you got the null reference error because you were asking for something that doesn't exist (the crop) on that particular property. So you don't need the extra null and empty string checks that I suggested earlier.
Hi Sebastiaan Janssen I was so excited about the code I went back to the office on Saturday. I'm affraid that the error is gone now.. but no value comes up when I call @crop.
I tried both ways with .Value and .Value.ToString() and got nothing to come back..
I put the Hello Text just to see if stuff was showing up.. which it does... Thanks again for looking at it.. I can't wait to have this one solved..
Okay, we're getting there. So HELLO shows up, right?
I've just tested my code and it works fine with your XML. So the problem is most likely in the getProperty part. Do you get a string of XML back when you do:
If that is empty, obviously the crop will be empty as well.
Instead of this, try:
(as I don't understand why you're creating that "d" variable in the first place).
If that gives you an xml string, this should work:
hmm closer but no cigar..I added:
to my file and all 4 lines returned the correct information viewable when I viewed the source code of my page. But displayed nothing on the actual page itself. The result looked like this:
If I wanted to have this information actually show on my page I could write it like this.
which worked, but I am still unable to only call the URL for the cropped image. When i tried your @if statement I get No result. If I add <text>hello</text> I am able to see the hello part.
As far as the reason for the d. variable.. Im not exactly sure eithe,r but I am working from inside a ublogsy file and just following the code that is already in there. if you want to try to experience the full problem you could install uBlogsy and add an upload and image cropper datatype to the Blog Post DocType and see if you could call the crops. But we can see it in the XML and I can now call the string (in full anyway) just haven't been able to isolate the URL for the cropped image.
Once we get this solved I really hope it works in the members section too cause I have crops in razor over there too..
Hi, I also had a problem with image cropper, in my case I got a "root element missing" xml error message and it took me quite a while to realize that it was because I was iterating over several images, and one of them did not have the actual cropper name. Anyways, to solve it I copied parts of Sebastiaans code and added a try catch to help me. Dunno if that might help you also, but something like this:
Ok I had to take a little break from this cause it was just holding me up too much.. But now I need to give it another go..
@Jonas Eriksson did you modify the
section at all at the top of the page that Sebastiaan Janssen provided? I keep getting errors with your script that says that certain items don't exist.
-----------------------------------------------------------------------------------------------------------------------------------------------------------
@Sebastiaan Janssen well we left off being able to display the XML on the page.. that looks like this..
Any other tricks that you might be able to pull out to issolate just the URL value?? I really gotta get past this hurdle..
Thanks to both of you for working with me.. sorry this has been so frustrating
Ok well I've spent the day rewriting the template line by line..But still no luck pulling the cropped thumbnail..
If I place this line of code into my template
I writes this on my page...
I just need to figure out how to only pull the URL from this string..
Hm, if your value looks like that - and you have the GetImageCropperUrlFullMedia-function in your macro script, this should be fine for you:
Hi Jonas.. well when I drop in your code I get no result... I can add in "hello" text inside your div and see that text appear on my page so I know where it should be popping up.. but still I get no imagecropper URL...
I have the GetImageCropperUrlFullMedia function at the top of my cshtml file which looks like this:
Do you see anything wrong with that?? I have no idea why I can't pull the info..
I noticed you changed the code slightly, please change back this row:
YAAAAAAAYYYYYYYYY
THANK YOU!!!!!!! OMG... Finally..
Yay, happy it works :) (credits should really go to Sebastiaan and uComponents originally I think) could you please add the whole script for us?
Yes, Sebastiaan Janssen worked with me forever on this problem. He's been a great help all throughout my frist Umbraco Project.
Well my full template script is a little crazy but here are the important parts to get the image cropper thumbs:
At the top of the Razor template make sure you have this:
And then where you need to call the crop you can do it like this..
It's just depressing that in 4.7.1 (which should be out soon-ish) you could just do the below.. would've saved many hours:
Ps. We were at the exact same solution 5 days ago, do you have any idea what changed?
Ha.. well that sure would have been nice.. but on the bright side.. I think I probably learned a lot by dissecting this thing line by line so many times... probably some good experience for me on my first project.. Thanks again for all your help... I know I have a few more major hurdles before I can launch this bad boy.. but trying to make progress everyday
No, honestly I have no idea how that got changed.. very very very sorry.. however it did..
Curious : I saw that you were using this code:
that looks like you are able to call the script from another page (or from a template) with a reference to a node (other than the current) with the actual cropped image. That would explain why Model and d is actually different.
@FarmFresh Nah, don't be sorry, was just curious what might be different now.
@Jonas Aaah, that might be it
Yeah, I worried that it might be hendering me.. I don't know if you guys have checked out the package "uBlogsy" yet. its a Razor based Blog for Umbraco. For the most part I am really enjoying it. However I am still in my n00b stage and understanding all of the pre-existing code is a little over my head.. I am just trying to work within the guidelines that it came with..
Here's a link to an example of how you do this in Umbraco 4.7.1.
You shouldn't need more than one row of code:
Worked like a charm for me!
/Thomas
https://gist.github.com/1170848 is an excellent link but I have two images in my news items (image1 & image2) and I just want the crop called "News Item" off the first image. I can't seem to do
Don't understand why it isn't easier.
Craig
here is some image crop codes in C#
is working on a reply...