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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

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


Go to End


35 Posts   13440 Views

Avatar
Craftnet

Community Member, 58 Posts

26 July 2012 at 6:28pm

@Liam
Use only $Width

Avatar
colymba

Community Member, 26 Posts

27 July 2012 at 12:22am

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

Avatar
Liam

Community Member, 470 Posts

27 July 2012 at 2:37am

Edited: 27/07/2012 2:56am

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

Community Member, 26 Posts

27 July 2012 at 5:03am

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

Community Member, 470 Posts

27 July 2012 at 5:43am

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

Community Member, 26 Posts

27 July 2012 at 9:01am

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

Community Member, 470 Posts

27 July 2012 at 12:50pm

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

Community Member, 470 Posts

27 July 2012 at 1:10pm

Actually upon further inspection this always returns false.

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

Thanks.