Jump to:

3434 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Checking Image Orientation

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2263 Views
  • Boogiez
    Avatar
    Community Member
    17 Posts

    Checking Image Orientation Link to this post

    Hey all,

    I apologize if I should be able to figure this out on my own, but I've been working on it for a few days and I just don't know how to write this. A veteran SS'er should probably find this pretty trivial...

    I have a page that has a different layout based on whether a photo on the page is landscape or portrait oriented. I want to write a boolean function isHorizontal so that I can have a template that looks something like this:

    ...
    <% if isHorizontal %>
    // Horizontal code here
    <% else %>
    // Vertical code here
    <% end_if %>

    Right now, I have created a function inside the Controller class for the page that goes like this:

       function isHorizontal() {
       $result = rand(1,2);
       if($result==1) return true;
       return false;
       }

    This works exactly like I want it to, except it obviously just returns a random result for the image orientation rather than checking the image's height against its width. I've looked at the datamodel stuff and it's just a little bit over my head as I'm not great with PHP. If anyone is able to answer this, I would also be incredible appreciative if you could explain your answer. Thanks!

  • UncleCheese
    Avatar
    4085 Posts

    Re: Checking Image Orientation Link to this post

    Well, assuming you haven't subclassed your image and you're using the standard Image object, you're going to have to do some hacking.

    public function Horizontal()
    {
    return $this->obj('NameOfImageField')->getHeight()<$this->obj('NameOfImageField')->getWidth();
    }

    A better way to do it is to create a MyImage.php class

    class MyImage extends Image
    {
    public function Horizontal()
    {
    return $this->getWidth() > $this->getHeight();
    }
    }

    and of course update your model to use the MyImage class rather than Image.

  • Boogiez
    Avatar
    Community Member
    17 Posts

    Re: Checking Image Orientation Link to this post

    Thanks for your quick response!

    I decided to go with your second suggestion and call my subclass "GalleryImage." So I created a php file, GalleryImage.php and placed it in the code directory:

    class GalleryImage extends Image {
       public function Horizontal() {
          return $this->getWidth() > $this->getHeight();
       }
    }

    However, after adding this (before updating my model) I rebuilt the database and I get this:

    ...
    * GalleryImage
    class GalleryImage extends Image { public function Horizontal() { return $this->getWidth() > $this->getHeight(); } }
    Warning: Cannot modify header information - headers already sent by (output started at /home/homewerx/potteraestudios.com/site/sapphire/core/model/DatabaseAdmin.php:137) in /home/homewerx/potteraestudios.com/site/sapphire/core/Debug.php on line 151

    It also failed after updating my model, and after I changed the return statement to simply be false rather than doing the calculation. I must be missing something super obvious...

  • UncleCheese
    Avatar
    4085 Posts

    Re: Checking Image Orientation Link to this post

    is all of that wrapped in <?php tags? Looks like your code is just spilling out into the browser.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Checking Image Orientation Link to this post

    Heh, I liked the idea so I submitted a patch and Sam has already merged into 2.3, so in the new (nearly ready for release) version, you will be able to do:

    myImage->getOrientation()

    ...which will return a class constant, either:

    ORIENTATION_SQUARE or ORIENTATION_PORTRAIT or ORIENTATION_LANDSCAPE.

    So, in your page class you will be able to do:

    function landscape() {
       return $this->Image()->getOrienation() == Image::ORIENTATION_LANDSCAPE;
    }

  • Boogiez
    Avatar
    Community Member
    17 Posts

    Re: Checking Image Orientation Link to this post

    I did indeed forget to put it in the <?php ... ?> tags. D'oh!

    Works like a charm!

  • Boogiez
    Avatar
    Community Member
    17 Posts

    Re: Checking Image Orientation Link to this post

    Just FYI - I was converting an website I created several years ago to use SilverStripe rather than the hackish CMS I wrote in REALBasic. Despite the legacy code being a steaming mound of poo, it was easy to move everything over.

    Check it out (the image orientation bit occurs in the gallery):
    http://www.potteraestudios.com/

    And if you value proper XHTML/CSS coding, then for the love of God, do not check the source code. It's the most miserable table-based, inline-CSS garbage ever. But it's not worth fixing at this point ;)

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Checking Image Orientation Link to this post

    looks like your flash intro is still linking to site/index.html, throws a 404 is you watch it all the way through ;)

    have you thought about using the e-commerce module for the shop stuff? perhaps it's overkill but might be good to try (although I've heard getting Paypal to work can be a pain).

    I found the navigation a little confusing, but I liked the site

    2263 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.