Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Conditional Image Sizes

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

Page: 1
Go to End
Author Topic: 838 Views
  • merrick_sd
    Avatar
    Community Member
    95 Posts

    Conditional Image Sizes Link to this post

    Where can I find info on controlling image sizes.?

    I have a pull down in my cms "CSSBoxStyle" and based on that pull down value i want to set the size of the image displayed.

    if they choose nostyle then its 218 pixels wide
    else its 294 pixels wide

    i'm struggling with my if statements

    Is it best to do this in the SS file or the PHP image class?

    example PHP

    <?php

    class RightContent extends DataObject {
       
       static $db = array(
          'Title' => 'Text',
          'Description' => 'HTMLText',
          'ImageLink' => 'Varchar(255)',
             'CSSBoxStyle' => "Enum('styleone, styletwo, stylethree, stylefour, nostyle')"
       );
       
       static $singular_name = 'RightContent';
       static $plural_name = 'RightContents';
       
       
       static $has_one = array(
       'Page' => 'Page',
       'SideScreenImage' => 'RightContent_SideScreenImage'
       );
       
       static $defaults = array(
             'Title' => ''
          );
       
       
          public function getCMSFields_forPopup() {
       
          $fields = new FieldSet(    
          new ImageField('SideScreenImage','MainImage (Optional)'),
          new TextField('ImageLink', 'Add URL Link to Image (Optional)'),
          new TextField('Title', 'Title'),
          new SimpleHTMLEditorField('Description','HTML'),
          new DropdownField('CSSBoxStyle', 'CSSBoxStyle', singleton('RightContent')->dbObject('CSSBoxStyle')->enumValues())

       //'getCMSFields_forPopup'
          
          );
          

       return $fields;
          
          } //getCMSFields_forPopup
       

       
       
       
       
       
       
       } //dataobject
       
    class RightContent_SideScreenImage extends Image {

       
       function generateWebsiteThumnail($gd) {
       

          $gd->setQuality(80);
          
       //if nostyle is chosen i want it to be 218
       return $gd->ResizeByWidth(218);
          
    //return $gd->ResizeByWidth(194);
          
       }
       
    }      
       

    ?>

    SS file
    <% control RightContents %>

    <% control SideScreenImage %>
       $SetWidth(218)
    <% end_control %>

    <% end_control %>

  • merrick_sd
    Avatar
    Community Member
    95 Posts

    Re: Conditional Image Sizes Link to this post

    I managed to do it in the ss file.

    <% if CSSBoxStyle == "nostyle" %>   
       
       <% control SideScreenImage %>
       $SetWidth(218)
       <% end_control %>   

       <% else %>

       <% control SideScreenImage %>
       $SetWidth(194)
       <% end_control %>
          
       <% end_if %>

    I couldn't seem to do it in the php as it was suggesting CSSBoxStyle was an unknown variable
    whats the difference between doing in in the ss file or the php file?
    is it related to performance?

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