0

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
Community
  • 1
  • 1
Xazen
  • 822
  • 2
  • 12
  • 26

1 Answers1

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

}
ryanwachtl
  • 381
  • 1
  • 5