Jump to:

5554 Posts in 1742 Topics by 1227 members

Customising the CMS

SilverStripe Forums » Customising the CMS » custom image resize

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

Page: 1
Go to End
Author Topic: 1509 Views
  • Anatol
    Avatar
    126 Posts

    custom image resize Link to this post

    Hi,

    I'm a bit confused. I want to create a new resize function that I can call from the template. According to a recipe I added this to mysite/Page.php :

    class Page extends SiteTree
    ...
    static $has_one = array(
    'Image' => 'Page_Image' // We define Page_Image, which is a subclass of Image
    );
    ...
    }

    Then I added a custom function, also to mysite/Page.php

    class Page_Image extends Image { // This class can go inside the same file as Page.php   
       function generatePaddedImageByWidth($gd) {
          $height = $gd->getHeight;
          return $gd->paddedResize(500, $height);
       }
    }

    The function creates a padded image with a maximum width, but keeps the original image height (it basically creates an image that is horizontally centered with white padding on the left and on the right).

    Unfortunately it doesn't work. But if I put the following into /sapphire/core/model/Image.php it works well:

    function generatePaddedImageByWidth(GD $gd, $width) {
       $height = $gd->getHeight();
       return $gd->paddedResize($width, $height);
    }

    I would just prefer not to change core code but keep changes in Page.php. Any hint is highly appreciated.

    Cheers!
    Anatol

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