Jump to:

23478 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » SetWidth background colour

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

Page: 1
Go to End
Author Topic: 1395 Views
  • Mo
    Avatar
    Community Member
    508 Posts

    SetWidth background colour Link to this post

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

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

    Mo

  • banal
    Avatar
    Community Member
    901 Posts

    Re: SetWidth background colour Link to this post

    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...

  • Mo
    Avatar
    Community Member
    508 Posts

    Re: SetWidth background colour Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: SetWidth background colour Link to this post

    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.

  • Mo
    Avatar
    Community Member
    508 Posts

    Re: SetWidth background colour Link to this post

    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

    1395 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.