SilverStripe 获取图像宽度/高度的一半
如何计算图像的高度和宽度?我正在尝试将图像居中,如第一个答案中所述:
这就是我到目前为止所尝试过的:
<% control ProfileImage %>
<% if getOrientation = 1 %>
<img src="$URL" style="margin-left: -{$Width/2)}px" class="portrait"/>
<% else_if getOrientation = 2 %>
<img src="$URL" class="landscape"/>
<% end_if %>
<% end_control %>
决定性行的输出是:
<img src="/assets/Uploads/picture.jpg" style="margin-left: -{154/2)}px" class="portrait"/>
然后我尝试编写一个自己的函数:
class Profile extends Page{
//...
function GetHalfWidth(){
return ($this->ProfileImage->Width)/2;
}
//...
}
但当我尝试在前端查看页面时出现以下错误:
[Notice] Trying to get property of non-object
how can I get calculate with image height and width? I am trying to center an image like described in the first answer:
How to make an image center (vertically & horizontally) inside a bigger div
That's what I have tried so far:
<% control ProfileImage %>
<% if getOrientation = 1 %>
<img src="$URL" style="margin-left: -{$Width/2)}px" class="portrait"/>
<% else_if getOrientation = 2 %>
<img src="$URL" class="landscape"/>
<% end_if %>
<% end_control %>
The output of the decisive line is:
<img src="/assets/Uploads/picture.jpg" style="margin-left: -{154/2)}px" class="portrait"/>
Then I tried to write an own function:
class Profile extends Page{
//...
function GetHalfWidth(){
return ($this->ProfileImage->Width)/2;
}
//...
}
But I get the following error when I try to view the page in the frontend:
[Notice] Trying to get property of non-object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要获取模型中图像的宽度,您可以将函数更改为:
但是,如果您尝试在 ProfileImage 的控制循环中引用该方法形式,则需要“跳出”并使用 $Top。 GetHalfWidth
我认为处理它的更好方法是将其定义为 ProfileImage 的方法,如果 ProfileImage 是 Image 的子类...
在
Profile.php
中To get at the width of your image in the model you can change your function to read:
However, if you are trying to reference that method form within the control loop of ProfileImage you'll need to 'jump out' and use $Top.GetHalfWidth
I would think a better way to handle it would be to define it as a method of your ProfileImage, if ProfileImage was subclassed from Image...
In
Profile.php