Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

[SOLVED] SS3 Upload many files/images [SOLVED] Resize image if it's larger than a certain width


Reply

35 Posts   5255 Views

Avatar
Craftnet

26 July 2012 at 6:28pm Community Member, 58 Posts

@Liam
Use only $Width

Avatar
colymba

27 July 2012 at 12:22am Community Member, 25 Posts

like @Craftnet said, you don't need $Image since the loop tag places you in the image scope.

Avatar
Liam

27 July 2012 at 2:37am (Last edited: 27 July 2012 2:56am), Community Member, 470 Posts

Oh. Duh.

One last thing.

Is there no way to compare less than in the template logic?

Or is there an image function I can use in the template that only resizes the image if it's larger than a certain width?

In the template, if I have an image over 800px I want to resize it to 800px, SetWidth(800), but if it's smaller, than display normal.

Or do I have to extend the image class and do it from there?

Avatar
colymba

27 July 2012 at 5:03am Community Member, 25 Posts

never tried greater or smaller than operator in templates, but I had in mind than SS3 template logic is now better. Did you try <% if $Image.Width < 800 %> or similar?

Otherwise you might be able to create a test function in your controller that returns a boolean, with something like this:
<% if $isBigger($Image.Width, 800) %>

you can extend the Image class but it doesn't seem necessary here

Avatar
Liam

27 July 2012 at 5:43am Community Member, 470 Posts

Ya I was trying <% if $Width < 800 %> while in the Images scope but it throws an error and it's the less than operator that is causing it. $Width returns proper value.

I didn't see anything in the template docs about < > so I don't think it's supported. Oh well.

Avatar
colymba

27 July 2012 at 9:01am Community Member, 25 Posts

try using this in the template then:

<% if $greaterThan($Width, 800) %>

<% end_if %>

with this in the controller

public function greaterThan($value, $reference)
{
if ($value > $reference) return true;
else return false;
}

there might be better solutions, but this could work...

Avatar
Liam

27 July 2012 at 12:50pm Community Member, 470 Posts

Yup this looks like it does the job.

I'll be sure to note that less/greater than operators don't work in templates.

Cheers,

Avatar
Liam

27 July 2012 at 1:10pm Community Member, 470 Posts

Actually upon further inspection this always returns false.

I'll play around with it and let you know what I find.

Thanks.