Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Archive

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

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

gd image resize?


Reply

5 Posts   2264 Views

Avatar
dashiel

14 May 2008 at 7:06pm (Last edited: 14 May 2008 7:23pm), Community Member, 13 Posts

hi

i went through [url=http://doc.silverstripe.com/doku.php?id=imageupload&s=image]this tutorial[/url] 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

Avatar
Willr

14 May 2008 at 11:29pm Forum Moderator, 5511 Posts

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

Avatar
dashiel

15 May 2008 at 12:53pm Community Member, 13 Posts

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

Avatar
Danger

17 June 2008 at 9:52am Community Member, 3 Posts

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

Avatar
KingBerT

2 September 2008 at 5:05am Community Member, 11 Posts

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