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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

ImageGallery Thumbnails Custom sizes and proportions

Go to End



Community Member, 65 Posts

7 July 2011 at 6:48pm


I was looking for some further flexibility with the thumbnails for the ImageGallery as I wanted to be able to crop them to a consistant height and width but not as squares.

By making a few minor additions/amends to the ImageGalleryPage.php file in the image_gallery > code folder I was able to allow the width of the thumbnails to be set to any width.

class ImageGalleryPage extends Page
	static $db = array (
		'SortBy' => "Enum( array( 'Title', 'UploadDateASC', 'UploadDateDESC','SortASC' ), 'SortASC' )",
		'GalleryUI' => "Varchar(50)",
		'CoverImageWidth' => 'Int',
		'CoverImageHeight' => 'Int',
		'ThumbnailSize' => 'Int',
		'ThumbnailWidth' => 'Int',  // New field for entering a ThumbnailWidth
		'MediumSize' => 'Int',
		'Square' => 'Boolean',
		'NormalSize' => 'Int',
		'NormalHeight' => 'Int',
		'MediaPerPage' => 'Int',
		'UploadLimit' => 'Int'
	static $has_one = array (
		'RootFolder' => 'Folder'
	static $defaults = array (
		'CoverImageWidth' => '128',
		'CoverImageHeight' => '128',
		'ThumbnailSize' => '128',
		'ThumbnailWidth' => '172',  // A default size
		'Square' => '1',
		'MediumSize' => '400',
		'NormalSize' => '600',
		'MediaPerPage' => '30',
		'MediaPerLine' => '6',
		'UploadLimit' => '20',
		'GalleryUI' => 'Lightbox'



This then needs adding to the configuration tab in admin which is don by adding in this line of code under the ThumbnailSize field


		$f->addFieldToTab("Root.Content.$configuration", new NumericField('ThumbnailWidth',_t('ImageGalleryPage.THUMBNAILWIDTH','Thumbnail width (pixels)')));


You may also want to change the label of the the check box in the line below as, after we have made the changes below, this will allow you to select whether or not you want your images cropped.

Below I have made the width of the cropped image ThumbnailWidth instead of ThumbnailSize:

public function GalleryItems($limit = null, $items = null) {
		if($items === null) 
			$items = $this->Items($limit);
		if( $items ) {
			foreach( $items as $item ) {
					$thumbImg = $item->Image()->CroppedImage($this->ThumbnailWidth,$this->ThumbnailSize); 
					$thumbImg = $item->Image()->SetHeight($this->ThumbnailSize);					
				if($thumbImg) {
					$item->ThumbnailURL = $thumbImg->URL;
					$item->ThumbnailWidth = $this->ThumbnailWidth;
					$item->ThumbnailHeight = $this->ThumbnailSize;
						$normalImg = $item->Image()->SetWidth($this->NormalSize);
						$normalImg = $item->Image()->SetHeight($this->NormalSize);
					$item->ViewLink = $normalImg->URL;
	  	return $this->UI->updateItems($items);
		return false;


Then back in admin you now have a new field in the configuration tab called Thumbnail width which will allow you to set the width you'd like your thumbnails. The check box below, previously Crop thumbnails to square, needs to be checked for this to work other wise it will work as before and only adjust the height.

I am not sure if this is the best way to achieve this but it seems to work.

I hope this helps if you are trying to achieve the same thing.