Reputation: 66
I am trying to do on the fly cropping inside of silver stripe. In the past we used
$Article_Image->SetWidth(390)
It worked. It returned an image of the correct width. However I now want to return an image that's using the fill function to return a 390 x 235 image. What I've got so far returns a 'server error', but it doesn't return any useful error.
Inside my newsArticle class I have the following:
Controller - current
class newsArticle extends Page {
private static $has_one = array(
'Article_Image' => 'Image'
);
public function ResizedImage() {
return $this->Article_Image()->Fit(390,235);
}
}
and then inside my page template I have the following:
Template - current
<% if $Article_Image %>
<div class="col-md-5 col-lg-4 nopadding">
$ResizedImage()
</div>
<% end_if %>
Already tried
I have already tried to just do $Article_Image->Fit(390,235) inside the template but it doesn't work. I have also tried the following code but it returned the error 'Cannot call method Fit() on non-member object'
Controller - already tried
class newsArticle extends Page {
private static $has_one = array(
'Article_Image' => 'Image'
);
public function ResizedImage($Image) {
return $Image->Fit(390,235);
}
}
Template - already tried
<% if $Article_Image %>
<div class="col-md-5 col-lg-4 nopadding">
$ResizedImage($Article_Image)
</div>
<% end_if %>
How can I get it to return an image that's been made to fit a particular dimension? I'm sure it can be done because it's been mentioned here in Silverstripe's documentation: https://docs.silverstripe.org/en/4/developer_guides/files/images/
I'm using silverstripe 3.1.12
Upvotes: 1
Views: 110
Reputation: 1809
The easiest way is to do the following in your template.
$Article_Image.CroppedImage(390,235)
You don't need to have a ResizedImage
function in your controller.
This is a link to the version 3 docs:
https://docs.silverstripe.org/en/3.1/developer_guides/files/image/
The Fit method was introduced in SS4.
Upvotes: 1