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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Creating resized image on upload instead of when you load the template


Go to End


6 Posts   1380 Views

Avatar
dendeffe

Community Member, 135 Posts

11 February 2010 at 1:38pm

Hi,

I'm trying to get my custom (resized) image to create the file on upload instead of when you open the template.
I need this to happen because I'm adding it to the page with JavaScript. Thanks to a lot of help from IRC I tried this:

http://pastie.org/819190

I didn't get it working though. Does anyone else have suggestions? Or an alterive idea with the JavaScript?

Avatar
Mo

Community Member, 541 Posts

13 February 2010 at 5:24am

Hi dendeffe,

I am not sure why javascript would cause an issue? Setting an image size in the template using something like $Img.SetWidth(106) for example, would generate a new image that wil then be referenced in the html dynamically.

As javascript is generally loaded after a page has loaded, the javascript should then just work with the resized images.

Maybe it would help if you explain what you are trying to achieve?

Mo

Avatar
dendeffe

Community Member, 135 Posts

13 February 2010 at 5:30am

I was trying to append an Image from an external JavaScript, which didn't work.

I've 'solved' it by generating a JavaScript Array with the image names in an included Template. It seems that simply mentioning the file without displaying also does the trick, it just needs to be in a template file.

Avatar
Mo

Community Member, 541 Posts

13 February 2010 at 5:49am

Edited: 13/02/2010 5:49am

That is pretty much what I would have suggested. You can use the the template methods within your classes as well, they may give you better results.

Have you looked at: http://doc.silverstripe.org/doku.php?id=image

Mo

Avatar
dendeffe

Community Member, 135 Posts

13 February 2010 at 6:02am

Ok thanks Mo. What's the advantage of using the template functions over a class function like this?

	function generateSmallThumb($gd) {
		return $gd->resizeRatio(108, 168, true);
	}

Avatar
Mo

Community Member, 541 Posts

13 February 2010 at 6:45am

My instinct would be to say the template methods are written as part of Silverstripe by the Dev's, so are integrated better, in terms of performance.

Of course, I would like to think that is true, but I can't guarantee it :). Also, I believe using the template functions automatically references the resized image back to the source image that you uploaded, where as if you were using gd, you would have to do that yourself. That might not be an issue for you though.

Not sure that makes sense... Hopefully it does :).

Mo