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

Extend/Decorate getFormattedImage (Image class)


Reply


4 Posts   334 Views

Avatar
Double-A-Ron

Community Member, 604 Posts

21 August 2013 at 3:35pm

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()?

Avatar
Devlin

Community Member, 217 Posts

21 August 2013 at 7:17pm

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)

Avatar
Double-A-Ron

Community Member, 604 Posts

22 August 2013 at 9:43am

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.

Avatar
Devlin

Community Member, 217 Posts

22 August 2013 at 7:39pm

Edited: 22/08/2013 8:20pm

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.