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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, Ed, biapar, Willr, Ingo, swaiba

Getting First Image from Image Gallery with croppedResize

Go to End

3 Posts   2797 Views


Community Member, 78 Posts

14 February 2011 at 5:01am

Hi there,

I am trying to get first image from image gallery with croppedResize. I can only retrieve the first image but the croppedResize doesn't work.


 * Defines the ProductPage page type
class MyImage extends Image {
	function generateSmallimage($gd) {
    return $gd->croppedResize(65,65);
class ProductPage extends Page {
	static $defaults = array(
    'ProvideComments' => true
    static $db = array(
	 	'Price' => 'Text',
	 	'Brief' =>'Text',
		'Date' => 'Date'
    static $has_many = array ('GalleryImages' => 'GalleryImage');

	function getCMSFields() { 
      $fields = parent::getCMSFields(); 
	  $fields->addFieldToTab('Root.Content.Main', new TextField('Price', 'Product Price'), 'Content');
      $fields->addFieldToTab('Root.Content.Main', new TextField('ProductIntro','Introduction'), 'Content');
      $fields->addFieldToTab('Root.Content.Main', $dateField = new DateField('Date','Date (for example: 20/02/2011)'), 'Content');
	  $dateField->setConfig('showcalendar', true);
	  $dateField->setConfig('dateformat', 'dd/MM/YYYY');
	$manager = new ImageDataObjectManager( 
         $this, // Controller 
         'GalleryImages', // Source name 
         'GalleryImage', // Source class 
         'MyGalleryImage', // File name on DataObject 
            'GalleryImageTitle' => 'GalleryImageTitle' 
         ), // Headings 
         'getCMSFields_forPopup' // Detail fields (function name or FieldSet object) 
         // Filter clause 
         // Sort clause 
         // Join clause 

     return $fields; 

class GalleryImage extends DataObject 
   static $db = array ( 
      'GalleryImageTitle' => 'Text' 
   static $has_one = array ( 
      'MyGalleryImage' => 'Image', 
      'BelongToProductPage' => 'ProductPage' 
   public function getCMSFields_forPopup() 
      return new FieldSet( 
         new TextField('GalleryImageTitle'), 
         new FileIFrameField('MyGalleryImage') 
class ProductPage_Controller extends Page_Controller {

<div id="content" class="typography"> 
	<div id="itemlist">
        <% control Children %>
			<a href="$Link" title="Read more on &quot;{$Title}&quot;">
			<% if GalleryImages %>
			<% control GalleryImages.First.Smallimage %>
			<img src="$MyGalleryImage.URL"/>
			<% end_control %>
			<% end_if %>
			<p>$Content.FirstParagraph <a href="$Link" title="Read more on &quot;{$Title}&quot;">Read more &gt;&gt;</a></p>
        <% end_control %>

I really need help otherwise can't go to bed :(



Forum Moderator, 4102 Posts

14 February 2011 at 5:42am

You can just do

<% control GalleryImages.First %>
<% end_control %>


Community Member, 78 Posts

14 February 2011 at 12:40pm


Thank you very much for your reply. Now It works fine, is it possible if I would like to pally CSS class to that image tag.