Jump to:

23471 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Crop to fit - Image resizing (2.4)

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: 1084 Views
  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Crop to fit - Image resizing (2.4) Link to this post

    Hi,

    I'm looking for a way to scale an image proportionally, filling a given width and height.
    Scaling can not get smaller than the dimensions given.
    After downscaling the image, i wont it cropped from center, so the dimension that goes beyond the given size is cropped away.
    See explanation and PHP code here: http://911-need-code-help.blogspot.dk/2009/04/crop-to-fit-image-using-aspphp.html

    My question is, can someone show me how to implement the above in SilverStripe, i think it should be done using a class that extends DataObjectDecorator, so it can be easily moved between projects.

    Example found on the forums using DataObjectDecorator:
    http://www.silverstripe.org/template-questions/show/16398#post301340

    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);
       }
    }

    Thank you!

  • simples
    Avatar
    Community Member
    76 Posts

    Re: Crop to fit - Image resizing (2.4) Link to this post

    Hi,

    I am not sure if this helps but I use the following to crop from the center when returning a cropped image in one of my functions.

    // ----
    // Gets the content of the image at that ID and crops it
    // ----
    $image=DataObject::get_by_id("Image",$id);
    return $image->CroppedImage(213,200);

  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Re: Crop to fit - Image resizing (2.4) Link to this post

    Thank you for your answer ssimples.

    It's actually possible to do what i want, with the function:
    $image->croppedResize(width,height) // Crops the image from the centre, to given values.

    However i don't know how to call that function directly from template, so i just used the code found here:
    http://www.silverstripe.org/template-questions/show/16398#post301340

    Works perfect and now i can easily add extensions if i should need that.

    --

    Thomas B. Nielsen
    Nobrainer Web

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Crop to fit - Image resizing (2.4) Link to this post

    You don't need a extension. Just call in the template;

    $Image.CroppedImage(150,200)

    ...or...

    <% control Image %>
    <img src="$CroppedImage(150,200).Filename" width="150" height="200" />
    <% end_control %>

  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Re: Crop to fit - Image resizing (2.4) Link to this post

    You are so right Devlin - ofcourse it's that simple
    Wondering why i did not see that, maybe i just misunderstood the documentation.

    But nice that the function is ther, thank you.

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