Jump to:

23581 Posts in 19403 Topics by 2893 members

General Questions

SilverStripe Forums » General Questions » Extend/Decorate getFormattedImage (Image class)

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: 290 Views
  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Extend/Decorate getFormattedImage (Image class) Link to this post

    Hi all. I've got a site built from SS 2.4.5 here.

    I'm looking into separating the CMS and Frontend to different servers and syncing assets uploaded via the CMS "Files and Images" section with the Frontend. But since the CMS server will never have resampled images, I must exclude _resampled directories from the sync process. Else all _resampled directories in the Frontend will be purged every time the sync runs (because the CMS folders are empty).

    So what happens when a new image is uploaded is that the original image is synced to the frontend assets/ directory. And nothing is touched in the relevant _resampled directory. This is fine for new images. But causes problems for images that are removed and replaced with an image of the same name. Obviously the previous resampled image persists.

    What I want to do is pretty simple and can be thought of in the context of any other Silverstripe site - When I call SetWidth(), which in turn calls getFormattedImage(), I would like the latter to check the maketime of both the original and cached images. If the original is newer than the cached copy, resample from scratch.

    What I don't want to do is hack the core. Can anyone think of a good way to decorate Image to alter the behavior of getFormattedImage()?

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Extend/Decorate getFormattedImage (Image class) Link to this post

    I do something similar, but with Image->cachedFilename(). Maybe you can implement your functionality there.

    https://github.com/JayDevlin/silverstripe-module-rsimage (branch 2.4)

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: Extend/Decorate getFormattedImage (Image class) Link to this post

    Thanks mate. I've had a play around with your code.

    Just so we are on the same page here, I am looking to change the default behavior of images uploaded via the "Files and Images" section of the CMS. I.E. I don't have the ability to add a new object type to a page or anything.

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Extend/Decorate getFormattedImage (Image class) Link to this post

    Sure. If a user uploads an image, the class name of the image will automatically be changed to RSImage -- so you can overload a method of the image class. I think that's what you want to do. You probably don't need the cacheFilename()/deleteFormatedRSImages() stuff.
    In the master branch, there is also a check if the filename has changed. You'll need that too.

    I've have to note, that this doesn't work with Uncle Cheese's KickAssets module and if you use Uploadify, you'll need the latest version from github.

    // edit
    This is meant as a starting point on how to overload or extend/decorate the image class. You still have a lot of work to do.

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