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.

Customising the CMS /

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

custom image resize


Go to End


2114 Views

Avatar
Anatol

126 Posts

7 July 2009 at 2:33pm

Edited: 07/07/2009 2:37pm

Hi,

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.

Cheers!
Anatol