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.

Template Questions /

Extending Image


Reply


3 Posts   1149 Views

Avatar
CodeGuerrilla

Community Member, 105 Posts

31 March 2011 at 1:23am

Edited: 31/03/2011 8:37pm

I am trying to extend Image so when I return it to the template it has a certain compression ratio (don't ask):

CustomImage.php

class CustomImage extends Image
{
function generateOptimised($gd) {
$gd->setQuality(70);
return $gd->resizeByWidth(941);
}
}

And in my template:

<% control CustomImage %>
$Optimised
<% end_control %>

OR:

$CustomImage.Optimised

Doesn't do anything.

$CustomImage.Optimised.URL

returns 'http://' what am I doing wrong? have used custom images before

Avatar
simon_w

Forum Moderator, 474 Posts

31 March 2011 at 8:12am

My usual approach to extending Image is to use a DataObjectDecorator, just so the added methods are available on all Images. Though that may also require something like:

function Optimised() {
return $this->owner->getFormattedImage('Optimised');
}

In your case, I would assume that the Image you're trying to use is a CustomImage, but just an ordinary Image.

Avatar
CodeGuerrilla

Community Member, 105 Posts

31 March 2011 at 8:29pm

Thanks simon_w works well and as you mentioned works on all images objects that is cool so now I can just make a bunch of different function in CustomImage and call them in my templates brilliant.

CustomImage.php

class CustomImage extends DataObjectDecorator
{
   function Optimised($width, $height)
   {
      return $this->owner->getFormattedImage('Optimised', $width, $height);
   }
   
   function generateOptimised(GD $gd, $width, $height) {
$gd->setQuality(70);
return $gd->croppedResize($width, $height);
}
}

_config.php

Object::add_extension('Image', 'CustomImage');

Template

$Image.Optimised(150,150)