Hi,
I'll have to give your suggestion a try - eloquently simple.
My solution is a little more involved.
First I added a Page_Photo array and then a function at the bottom of my staffpage.php which defines Page_Photo after the Page_Controller. (Page_Photo is an arbitrary name, you can put what you want.)
....
static $has_one = array(
'Photo' => 'Page_Photo'
);
}
class StaffPage_Controller extends Page_Controller {
}
class Page_Photo extends Image {
function generateSmallImage($gd) {
$gd->setQuality(100);
return $gd->resizeByWidth(150);
}
}
?>
In my StaffHolder.ss template I added the following:
<% control AllChildren %>
<li>
<div class="staffname"><h3>$Title</h3></div>
<div class="staffphoto"><img src="$Photo.SmallImage.URL" title="$TitleText" alt="$AltText" /></div>
<div class="staffdescription">$Content</div>
</li>
<% end_control %>
If I wanted I could have added more functions to the php file like:
function generateLargeImage($gd) {
return $gd->resizeByWidth(300);
}
So, that I could have the option of doing this in my ss template file <div class="staffphoto"><img src="$Photo.LargeImage.URL" title="$TitleText" alt="$AltText" /></div>
I figured this out largely from the following recipe:
http://doc.silverstripe.com/doku.php?id=recipes:customising_image
This document gives you some image manipulation functions
http://doc.silverstripe.org/doku.php?id=image
I found the following comment added at the bottom of http://doc.silverstripe.com/doku.php?id=gd interesting:
Blue Skies writes "In combining some functions I had the problem that doing it the "one $gd per line" way resulted in some functions not working or some functions overriding effects of previous functions.
As it turns out: some functions do create a new instance, others don't. So you have to chain certain functions to get the desired effect. For example, to use a resize, turn to greyscale and a set quality in one function, use:"
public function generateLogoGrey($gd) {
$gd = $gd->PaddedResize(70, 30)->greyscale();
$gd->setQuality(100);
return $gd;
}
The main advantage of creating a image class function, as above, is that you can combine several functions. However, regarding my original post, where I simply wanted to resize the image and have custom alt and title tags the solution suggested in the above post looks much easier to implement.
Sam Miller
----------
My complete StaffPage.php looks like:
<?php
class StaffPage extends Page {
static $db = array(
'AltText' => 'Varchar(50)',
'TitleText' => 'Varchar(50)'
);
static $has_one = array(
'Photo' => 'Page_Photo'
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.Images", new ImageField('Photo'));
$fields->addFieldToTab("Root.Content.Images", new TextField("TitleText"));
$fields->addFieldToTab("Root.Content.Images", new TextField("AltText"));
// $fields->removeByName("Sidebar");
$fields->removeFieldFromTab("Root","Sidebar");
return $fields;
}
static $defaults = array(
'ShowInMenus' => false
);
static $allowed_children = array('none');
}
class StaffPage_Controller extends Page_Controller {
}
class Page_Photo extends Image {
function generateSmallImage($gd) {
$gd->setQuality(100);
return $gd->resizeByWidth(150);
}
}
?>