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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Resize Image with ThumbnailWidth value from DataObjectSet


Go to End


2 Posts   3003 Views

Avatar
bmc38119

Community Member, 45 Posts

23 June 2009 at 1:17am

Edited: 23/06/2009 2:29am

I am have tried this many different ways and cannot seem to get this to work.

I have a class ProductView that extends DataObject and has a 1-to-many relationship with a ProductPage. The ProductView class contains a field for ThumbnailWidth (numeric) and a has_one relation Page_Image. In my template, I loop through the ProductView DataObjectSet and am trying to output a resized version of the Image according to the maximum ThumbnailWidth that has been entered in the CMS.

I cannot seem to figure out how to retrieve the ThumbnailWidth value of the ProductView within the Page_Image class. I obviously need a get_one statement, but i cannot seem to grab the ID of the ProductView once i am in the Page_Image class. Does anyone know how to do this?

ProductView.php

<?php
class ProductView extends DataObject {
 
   static $db = array(
      'ViewTitle' => 'Text',
	  'ViewDescription' => 'Text',
	  'ThumbnailWidth' => 'Int'
   );
   

  	static $has_one = array(
		'ProductPage' => 'ProductPage',
		'ViewImage' => 'Page_Image'
	);
	

   
	public function getCMSFields_forPopup()
	{
		return new FieldSet(
			new TextField('ViewTitle', 'Title'),
			new TextareaField('ViewDescription', 'Description'),
			new NumericField('ThumbnailWidth', 'Thumbnail Width (pixels)','150'),
			new ImageField('ViewImage', 'Image')
			
		);
	}

}
?>

ProductPage.ss

            <% control ProductViewOther %>
            <div style="padding-right:20px;float:left;">
                <a class="thickbox" rel="ViewImage" title="$ViewTitle" href="$ViewImage.URL"><img src="$ViewImage.ProductPageThumbResize.URL" /></a>
            </div>
            <% end_control %>

ProductViewOther method in ProductPage.php

	/******************************************
	* Product View Other - For Product Page
	*******************************************/
	function ProductViewOther() {
		$doSet = DataObject::get(
			$callerClass = "ProductView",
			$filter = "`ProductPageID` = '".$this->ID."' and `ViewMain` = 'No'"
		);
	 
		return $doSet ? $doSet : false;
	}

Page_Image class on Page.php

class Page_Image extends Image {
	
	function generateProductPageThumbResize($gd) {
			
                        <---need to get ProductView ID here so I can retrieve ThumbnailWidth value and assign $newWidth--->

			return $gd->resizeByWidth($newWidth);
}

}

Avatar
bmc38119

Community Member, 45 Posts

24 June 2009 at 1:22pm

After hours of trying to figure this out, finally got smart and looked to see how Image Gallery module handled this. ended up being a very easy solution (i could have sworn i tried this before posting, but maybe had typos in my code):

added the following method to ProductView.php

	public function ViewImageThumbnail()
	{
		return $this->ViewImage()->SetWidth($this->ThumbnailWidth);
	}