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.

General Questions /

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

Custom Image Resize in DataObject


Reply


4 Posts   3121 Views

Avatar
wardog

Community Member, 9 Posts

22 August 2009 at 4:26pm

Edited: 22/08/2009 4:27pm

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

Avatar
Pigeon

Community Member, 243 Posts

22 August 2009 at 10:55pm

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.

Avatar
wardog

Community Member, 9 Posts

23 August 2009 at 5:41pm

   
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

Avatar
wardog

Community Member, 9 Posts

23 August 2009 at 6:12pm

Edited: 23/08/2009 6:12pm

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>