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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

SetWidth background colour


Reply

5 Posts   1417 Views

Avatar
Mo

16 December 2009 at 7:52am Community Member, 509 Posts

Anyone know if there is a way to change it from white?

Something like: $image->bgcolour('hexvalue'); Maybe?

Mo

Avatar
banal

16 December 2009 at 8:28am Community Member, 901 Posts

Hm. I was under the impression that SetWidth never creates a background?
Maybe you are referring to "PaddedImage" and the like? I had to change the color once and subclassed Image for that purpose.

Something like:

class BetterImage extends Image
{
   public function generatePaddedImage(GD $gd, $width, $height) {
      return $gd->paddedResize($width, $height, 'cccccc');
   }
   
   public function SetWidth($width) {
      if($width == $this->getWidth())
         return $this;
         
      return $this->getFormattedImage('SetWidth', $width);
   }
   
   public function SetHeight($height) {
      if($height == $this->getHeight())
         return $this;
         
      return $this->getFormattedImage('SetHeight', $height);
   }
   
}

Then instead of using "Image", use "BetterImage" in your $has_one relations.
The above class also doesn't resize Images when they already have the correct dimensions which will improve quality...

Avatar
Mo

16 December 2009 at 11:44am Community Member, 509 Posts

Sorry, I should have been more specific, I am using:

$Image.setWidth(80,80);

In the template language. It pads out the image if it has to, with a background colour.

I think I Ideally might have to use a similar method to scale and crop all images square, but the setWidth method would do for this stage of development.

Mo

Avatar
banal

16 December 2009 at 11:52am Community Member, 901 Posts

I'm not aware of a way to alter the background color without subclassing or decorating. Simply create a subclass as proposed above and override the generate functions you need to customize.

Avatar
Mo

16 December 2009 at 12:24pm Community Member, 509 Posts

Hmm, I just found out I could do:

<% control croppedImage(80,80) %>
<img src="$URL" />
<% end_control %>

Which is pretty much exactly what I wanted initially. I think you might be right about the subclassing. I will have to look into it :).

Cheers,

Mo