Jump to:

3447 Posts in 1032 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Resize Image with ThumbnailWidth value from DataObjectSet

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

Page: 1
Go to End
Author Topic: 2223 Views
  • bmc38119
    Community Member
    45 Posts

    Resize Image with ThumbnailWidth value from DataObjectSet Link to this post

    I am have tried this many different ways and cannot seem to get this to work.

    I have a class ProductView that extends DataObject and has a 1-to-many relationship with a ProductPage. The ProductView class contains a field for ThumbnailWidth (numeric) and a has_one relation Page_Image. In my template, I loop through the ProductView DataObjectSet and am trying to output a resized version of the Image according to the maximum ThumbnailWidth that has been entered in the CMS.

    I cannot seem to figure out how to retrieve the ThumbnailWidth value of the ProductView within the Page_Image class. I obviously need a get_one statement, but i cannot seem to grab the ID of the ProductView once i am in the Page_Image class. Does anyone know how to do this?


    class ProductView extends DataObject {

    static $db = array(
    'ViewTitle' => 'Text',
        'ViewDescription' => 'Text',
        'ThumbnailWidth' => 'Int'

       static $has_one = array(
          'ProductPage' => 'ProductPage',
          'ViewImage' => 'Page_Image'

       public function getCMSFields_forPopup()
          return new FieldSet(
             new TextField('ViewTitle', 'Title'),
             new TextareaField('ViewDescription', 'Description'),
             new NumericField('ThumbnailWidth', 'Thumbnail Width (pixels)','150'),
             new ImageField('ViewImage', 'Image')



    <% control ProductViewOther %>
    <div style="padding-right:20px;float:left;">
    <a class="thickbox" rel="ViewImage" title="$ViewTitle" href="$ViewImage.URL"><img src="$ViewImage.ProductPageThumbResize.URL" /></a>
    <% end_control %>

    ProductViewOther method in ProductPage.php

       * Product View Other - For Product Page
       function ProductViewOther() {
          $doSet = DataObject::get(
             $callerClass = "ProductView",
             $filter = "`ProductPageID` = '".$this->ID."' and `ViewMain` = 'No'"
          return $doSet ? $doSet : false;

    Page_Image class on Page.php

    class Page_Image extends Image {
       function generateProductPageThumbResize($gd) {
    <---need to get ProductView ID here so I can retrieve ThumbnailWidth value and assign $newWidth--->

             return $gd->resizeByWidth($newWidth);


  • bmc38119
    Community Member
    45 Posts

    Re: Resize Image with ThumbnailWidth value from DataObjectSet Link to this post

    After hours of trying to figure this out, finally got smart and looked to see how Image Gallery module handled this. ended up being a very easy solution (i could have sworn i tried this before posting, but maybe had typos in my code):

    added the following method to ProductView.php

       public function ViewImageThumbnail()
          return $this->ViewImage()->SetWidth($this->ThumbnailWidth);

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.