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
4

1 回答 1

2

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

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;
    }

}
于 2011-11-22T03:03:49.690 回答