【问题标题】:SilverStripe get half of image width/heightSilverStripe 获得图像宽度/高度的一半
【发布时间】:2012-01-03 11:18:18
【问题描述】:

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

How to make an image center (vertically & horizontally) inside a bigger 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

【问题讨论】:

    标签: image silverstripe


    【解决方案1】:

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

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

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

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

    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-26
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多