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, biapar, Willr, Ingo, swaiba

Getting First Image from Image Gallery with croppedResize


Go to End
Reply


3 Posts   2503 Views

Avatar
bunheng

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.

ProductPage.php

<?php
/**
 * 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 
         array( 
            'GalleryImageTitle' => 'GalleryImageTitle' 
         ), // Headings 
         'getCMSFields_forPopup' // Detail fields (function name or FieldSet object) 
         // Filter clause 
         // Sort clause 
         // Join clause 
      ); 
      $fields->addFieldToTab('Root.Content.GalleryImage',$manager);

     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 {
}
  
?>

ProductHolder.ss

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

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

Bunheng

Avatar
UncleCheese

Forum Moderator, 4102 Posts

14 February 2011 at 5:42am

You can just do

<% control GalleryImages.First %>
$MyGalleryImage.CroppedImage(100,100)
<% end_control %>

Avatar
bunheng

Community Member, 78 Posts

14 February 2011 at 12:40pm

Hi,

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.

Regards,
Bunheng