SilverStripe 获取图像宽度/高度的一半

发布于 2024-12-17 06:13:01 字数 963 浏览 4 评论 0原文

如何计算图像的高度和宽度?我正在尝试将图像居中,如第一个答案中所述:

如何在更大的 div 内使图像居中(垂直和水平)

这就是我到目前为止所尝试过的:

<% 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生百味 2024-12-24 06:13:01

要获取模型中图像的宽度,您可以将函数更改为:

function GetHalfWidth() {
    $width = $this->obj('ProfileImage')->Width;
    return $width / 2;
}

但是,如果您尝试在 ProfileImage 的控制循环中引用该方法形式,则需要“跳出”并使用 $Top。 GetHalfWidth

我认为处理它的更好方法是将其定义为 ProfileImage 的方法,如果 ProfileImage 是 Image 的子类...

Profile.php

<?php
class Profile extends Page {

    ...

    public static $has_one = array(
        'ProfileImage' => 'Profile_Image'
    );

    ...
}
class Profile_Controller extends Page_Controller {
    ...
}
class Profile_Image extends Image {

    function GetHalfWidth() {
        $width = $this->Width;
        return $width / 2;
    }

}

To get at the width of your image in the model you can change your function to read:

function GetHalfWidth() {
    $width = $this->obj('ProfileImage')->Width;
    return $width / 2;
}

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

<?php
class Profile extends Page {

    ...

    public static $has_one = array(
        'ProfileImage' => 'Profile_Image'
    );

    ...
}
class Profile_Controller extends Page_Controller {
    ...
}
class Profile_Image extends Image {

    function GetHalfWidth() {
        $width = $this->Width;
        return $width / 2;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文