Jump to:

23483 Posts in 18976 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Custom Image Resize in DataObject

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 2437 Views
  • wardog
    Avatar
    Community Member
    9 Posts

    Custom Image Resize in DataObject Link to this post

    Hello I have a problem I could not do a resize a photo already investigated but failed to
    this is my code

    Galeria.php
    --------------------------------------------

    class Galeria extends DataObject {
       static $db = array ('Titulo' => 'Varchar', 'Desc' => 'HTMLText' );
       
       static $has_one = array ('GaleriaPage' => 'GaleriaPage', 'Image' => 'Image' );
       
       public function getCMSFields_forPopup() {
          $fields = new FieldSet ( new TextField ( 'Titulo' ), new HtmlEditorField ( 'Desc' ), new ImageField ( 'Image' ) );
          return $fields;
       }

    }


    --------------------------------------------

    Galeria.php
    --------------------------------------------

    class GaleriaPage extends Page {
       
       static $has_many = array(
          'Galerias' => 'Galeria',      
       );

       
       public function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab('Root.Content.Galeria',
             new DataObjectManager(
             $this,
             'Galerias',
             'Galeria',
             array('Imagen' => 'Imagen', 'Titulo' => 'Titulo','Desc' => 'Descripcion'),
             'getCMSFields_forPopup'
             ));
          return $fields;
       }
    }

    class GaleriaPage_Controller extends Page_Controller {

       function init() {
          parent::init();
           Requirements::css('jsparty/Slideshow2/css/slideshow.css');
           Requirements::javascript('themes/mtac/js/mootools.js');
           Requirements::javascript('themes/mtac/js/slideshow.js');
           Requirements::javascript('themes/mtac/js/slideshow.kenburns.js');
       }

       function Galery() {
          $reviewContent = DataObject::get("Galeria","GaleriaPageID =" . $this->ID);
    // Debug::show($reviewContent);
    return $reviewContent;    
       }

       function ImgResize($gd) {
          return $gd->resize(50,50)->URL();
       }

    }


    --------------------------------------------

    GaleriaPage.ss
    --------------------------------------------

    ....
    <script>
    var data = {
    <% control Galery %>
    '$Image.URL': {
       caption: '$Titulo',
       thumbnail: '$Image.ImgResize'
       
    <% if Last %> } <% else %> }, <% end_if %>   
    <% end_control %>
    };
    </script>   
    ....


    --------------------------------------------

    thanks in advance for helping

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Custom Image Resize in DataObject Link to this post

    Hi wardog,

    I'm not entirely sure what you are trying to do. If you just want to echo a resized image in the template, then you should be able to do this without your ImgResize function.

    If you need to resize in the PHP with a custom function (like you are trying to do now) then your function is wrong.

    Use (in you're 'Galeria' class):

    function myResize($width = 50, $height = 50) {
       return $this->Image()->getFormattedImage('resizeRatio',$width,$height);
    }

    You should then be able to use 'myResize' as a control in the SS template.

    That should sort out your problem.

  • wardog
    Avatar
    Community Member
    9 Posts

    Re: Custom Image Resize in DataObject Link to this post

       
    I think I did not understand very well .. I am making a template to create a gallery using slidershow
    but I can not do resize the image to the thumb ... and I read the documentation but I can only do when DataObject use or do not understand how to make the image resize what you need so as it is generating resize the image and get the url of the image with the display so that it can resize

  • wardog
    Avatar
    Community Member
    9 Posts

    Re: Custom Image Resize in DataObject Link to this post

    ok I've solved. anyway thank you very much for the help

    <% control Galery %>
    '$Image.Link': {
       caption: '$Titulo',
       <% control Image %>
          <% control ResizedImage(50,50) %>
       thumbnail: '$URL'
          <% end_control %>
       <% end_control %>
       
       
    <% if Last %> } <% else %> }, <% end_if %>   
    <% end_control %>
    };
    </script>

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