Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Getting First Image from Image Gallery with croppedResize

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1985 Views
  • bunheng
    Avatar
    Community Member
    69 Posts

    Getting First Image from Image Gallery with croppedResize Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Getting First Image from Image Gallery with croppedResize Link to this post

    You can just do

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

  • bunheng
    Avatar
    Community Member
    69 Posts

    Re: Getting First Image from Image Gallery with croppedResize Link to this post

    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

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