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:

Customising the CMS /

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

custom image resize

Go to End



126 Posts

7 July 2009 at 2:33pm

Edited: 07/07/2009 2:37pm


I'm a bit confused. I want to create a new resize function that I can call from the template. According to a recipe I added this to mysite/Page.php :

class Page extends SiteTree
   static $has_one = array(
      'Image' => 'Page_Image' // We define Page_Image, which is a subclass of Image

Then I added a custom function, also to mysite/Page.php

class Page_Image extends Image { // This class can go inside the same file as Page.php	
	function generatePaddedImageByWidth($gd) {
		$height = $gd->getHeight;
		return $gd->paddedResize(500, $height);

The function creates a padded image with a maximum width, but keeps the original image height (it basically creates an image that is horizontally centered with white padding on the left and on the right).

Unfortunately it doesn't work. But if I put the following into /sapphire/core/model/Image.php it works well:

function generatePaddedImageByWidth(GD $gd, $width) {
	$height = $gd->getHeight();
	return $gd->paddedResize($width, $height);

I would just prefer not to change core code but keep changes in Page.php. Any hint is highly appreciated.