Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » gd image resize?

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2249 Views
  • dashiel
    Avatar
    Community Member
    13 Posts

    gd image resize? Link to this post

    hi

    i went through this tutorial covering image resizing. i initially tried modifying it to work within a dataObject, but when that failed i went back and copied it word for word to no avail.

    from what i can gather it seems the BannerImage isn't getting created at all. at least when i look in the assets folder i can other resampled images, CMSThumbnail and a few SetWidth/ResizedImage versions too.

    i'm guessing it's something really small, and really dumb that i've overlooked.

    class Page extends SiteTree {
       static $db = array(
          'featuredTitle' => 'Text'
       );
       
       
       static $has_one = array(
          'BannerImage' => 'Image'
       );
       
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $fields->addFieldToTab('Root.Content.Main', new TextField('featuredTitle','Headline'));
          $fields->addFieldToTab('Root.Content.MyBannerImage', new ImageField('BannerImage','Featured Image'));
          
          return $fields;
       }
    }

    class Page_Controller extends ContentController {
       function init() {
          parent::init();      
          
       }
    }

    class Page_BannerImage extends Image {

       function generatePageBanner($gd) {
       
          $gd->setQuality(100);
          return $gd->paddedResize(619,154);
       }

    }

    in page.ss i have

    <% if BannerImage.PageBanner %>
    <img class="headerImage" src="$BannerImage.PageBanner.URL" alt="Header banner" />
    <% else %>
    <% control Page(home) %>
    <img class="headerImage" src="$BannerImage.PageBanner.URL" alt="Header banner" />
    <% end_control %>
    <% end_if %>

    thanks in advance

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: gd image resize? Link to this post

    I think it might be failing on $BannerImage.PageBanner.URL. I dont know if it supports this or not. Try

    <% control BannerImage %>
    $PageBanner.URL
    <% end_control %>

    Also make sure the image is actually saving in the DB. Check BannerImageID exists in the Page table. then take that ID and look on the File table to make sure theres a file with that ID

  • dashiel
    Avatar
    Community Member
    13 Posts

    Re: gd image resize? Link to this post

    hi willr

    thanks for your reply. it does seem that the database record isn't getting created. i tried adding a field to the $has_one array called PageBanner, but it doesn't auto associate the ID. this has been moved to a low priority as i've fallen back on the setWidth() method which is functioning fine for now.

    i would in the future like to understand more about extending the image class. so if anyone has experience with it, i'd appreciate any pointers.

    cheers

  • Danger
    Avatar
    Community Member
    3 Posts

    Re: gd image resize? Link to this post

    Something I learnt the other day is that the field you create in the $has_one array has to use the extended image class. For you, you need to change "Image" to "Page_BannerImage".

    Cheers,
    Chris

  • KingBerT
    Avatar
    Community Member
    11 Posts

    Re: gd image resize? Link to this post

    Thank you very much Danger. Now it works fine.
    so long BerT

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