Jump to:

3395 Posts in 977 Topics by 853 members

Template Questions

SilverStripe Forums » Template Questions » Extending Image

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

Page: 1
Go to End
Author Topic: 942 Views
  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Extending Image Link to this post

    I am trying to extend Image so when I return it to the template it has a certain compression ratio (don't ask):

    CustomImage.php

    class CustomImage extends Image
    {
    function generateOptimised($gd) {
    $gd->setQuality(70);
    return $gd->resizeByWidth(941);
    }
    }

    And in my template:

    <% control CustomImage %>
    $Optimised
    <% end_control %>

    OR:

    $CustomImage.Optimised

    Doesn't do anything.

    $CustomImage.Optimised.URL

    returns 'http://' what am I doing wrong? have used custom images before

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: Extending Image Link to this post

    My usual approach to extending Image is to use a DataObjectDecorator, just so the added methods are available on all Images. Though that may also require something like:

    function Optimised() {
    return $this->owner->getFormattedImage('Optimised');
    }

    In your case, I would assume that the Image you're trying to use is a CustomImage, but just an ordinary Image.

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: Extending Image Link to this post

    Thanks simon_w works well and as you mentioned works on all images objects that is cool so now I can just make a bunch of different function in CustomImage and call them in my templates brilliant.

    CustomImage.php

    class CustomImage extends DataObjectDecorator
    {
       function Optimised($width, $height)
       {
          return $this->owner->getFormattedImage('Optimised', $width, $height);
       }
       
       function generateOptimised(GD $gd, $width, $height) {
    $gd->setQuality(70);
    return $gd->croppedResize($width, $height);
    }
    }

    _config.php

    Object::add_extension('Image', 'CustomImage');

    Template

    $Image.Optimised(150,150)

    942 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.