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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

All other Modules /

Discuss all other Modules here.

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

DataObjectManager - resize image at creation by input value

Go to End

11 Posts   4193 Views


Community Member, 27 Posts

25 April 2009 at 1:22am

Is there a nice way to resize the image that I upload ?

I would like to use an input field (next to Name and describtion), where I can set the width, so that after the image got uploaded, and after I filled the image input fields (in the following dialog), it automatically gets resized to that width and saved as the main image (not thumbnail or so).

Thanks in advance.


Forum Moderator, 4102 Posts

25 April 2009 at 3:26am

Of course.. let's say you put two properties on your ImageObject "ImageWidth" and "ImageHeight".. you could then write a function on the page containing that image:

function ResizedImage()
return $this->Image()->CroppedImage($this->ImageWidth, $this->ImageHeight);

and on your template:

<img src="$ResizedImage.URL" alt="" />


Community Member, 27 Posts

25 April 2009 at 6:04am

If I understand you correctly, you are suggesting to always resize the image (on demand) when it is used somewhere... ?

I thought there would be a way to make this happen once, right after the image(/images) have been uploaded, in this multi-page dialog (Editing file 1 of X), that comes after pressing the upload button, or directly in the "Add Images"-dialog (before you click on the blue upload button)...

For now, I have written a function that lets me check from within the template if an uploaded image is wider than what I want. If so, I set a css width value, so that the browser can reduce the width. But since I exactly know, how big the width must be for all images that I upload ( < 700px ), it would make more sence to simply reduce the file size on uploading, wouldn't it... ?

For those who stumble across this post, wondering how I did it, here is my code...

<% control ReferenzenBild.Bild %>
<% if isWiderThan(700) %>
<img src="$URL" style="width: 740px" />
<% else %>
<img src="$URL"/>
<% end_if %>
<% end_control %>

ReferenzenBild is what you call 'Resource', // Source class in your ResourcePage
Bild is what you call 'Attachment' => 'File', in your Resource
Instead of File I use ImageExtended, a class that extends Image containing my isWiderThan function
With "your" I mean what I read in

public function isWiderThan($width = 740){
return ($this->getWidth() > $width);

By the way, it's so good, that you programmed this module, I was looking for something like that since my first contact with silverstripe (half a year).
Thank you :D !!!


Forum Moderator, 4102 Posts

25 April 2009 at 6:49am

Thanks for the compliment! Glad it's working for you.

As a matter of best practice, Silverstripe does not resize on upload. The original image that you upload is always kept in tact. When you resize the image on the template, it duplicates the image and resamples it, so that it can retrieve it easily the next time. If that ever changes, it will cut a new version, but it will never alter the integrity of your original image.


Community Member, 27 Posts

25 April 2009 at 7:32am

Edited: 25/04/2009 7:33am

I'm not quite sure I understand, do you mean it remembers my resizing (the return $this->Image()->CroppedImage($this->ImageWidth, $this->ImageHeight); ) so that next time or each next time he loads, he won't resize again, but take the image, that was resized the very first time, only ?
because I don't want him to resize each time... I know its quite fast, I programmed a Banner editor that does a really mega mega mega huge amount of work (and it's animated too) and all work is done in 1 second, but still if it's possible you never want to do the same work twice (or have the same operation twice).


Forum Moderator, 4102 Posts

25 April 2009 at 7:43am

That's correct. It caches the resampled image in the _resampled folder and provided the dimensions never change on your template, it will never resize again. Once you make a change, however, the first time the page loads, it will cut a new image to the _resampled folder with the new dimensions.


Community Member, 27 Posts

25 April 2009 at 8:33am

Thank you very very much for this imformation :D


Community Member, 215 Posts

25 April 2009 at 9:39am

Hi guys,

I realise that it is normal practice to not destroy the integrity of the source image but I am building a site at the moment that has a lot of admins all uploading photos from events and you just know that there will be those who upload the 3mb images directly off their camera. I'm not keen on having all that space used (hundreds of photos) on the server so is there a way to do what he is originally asking? ie. upload, if larger than x resize to 800x600 and save.

Go to Top