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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Solved! Download section


Go to End


15 Posts   2286 Views

Avatar
UncleCheese

Forum Moderator, 4102 Posts

26 November 2010 at 4:31am

Only Image objects have GD functions.

---------------
Silverstripe tips, tutorials, screencasts, and more. http://www.leftandmain.com

Avatar
borriej

Community Member, 267 Posts

26 November 2010 at 4:36am

Ok was afraid so.

I dont really want to change my code, because i want to be able to upload all kind of files with 1 page type.

I'll use some script to generate the thumbs.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

26 November 2010 at 5:47am

Here's a possible fix. In Download.php:

public function onAfterWrite() {
parent::onAfterWrite();
if($this->FileID) {
if(in_array($this->File()->Extension, array('jpg','jpeg','gif','png'))) {
$f = $this->File();
$f->ClassName = "Image";
$f->write();
}
}

}

That might work. Might be a few syntax errors in there, but hopefully you get the gist..

Avatar
borriej

Community Member, 267 Posts

26 November 2010 at 11:09am

Dude it worked :) your great!

For those in need of this code:

template:

<% control File %>
<a href="$URL" target="_blank">
$SetHeight(110)			
</a>
<% end_control %>

Code that extends DataObject:


class yourNameHereextends DataObject {

....

	public function onAfterWrite() {
		parent::onAfterWrite();
		if($this->FileID) {
			if(in_array($this->File()->Extension, array('jpg','jpeg','gif','png'))) {
			$f = $this->File();
			$f->ClassName = "Image";
			$f->write();
			}
		}
	} 

}

Avatar
UncleCheese

Forum Moderator, 4102 Posts

26 November 2010 at 2:03pm

Glad to hear it. The only thing I'm wondering is, if your template is calling $SetWidth on every File object, then why not just cast the relation as Image, and save yourself all that trouble?

Why does it have to be casted as File?

---------------
Silverstripe tips, tutorials, screencasts, and more. http://www.leftandmain.com

Avatar
borriej

Community Member, 267 Posts

26 November 2010 at 10:53pm

Edited: 26/11/2010 10:53pm

Well.. I wanted to have a minimum amount of pagetypes.
So the goal was to have 1 downloads page type.

Im using this template to upload images, but also pdf's, doc's, zips etc.

The template checks the extensions and decides to put a nice image (with a big icon of the filetype) or just render a thumbnail.
The template checks alot of extensions, but this is an example for those in need of code.

<% control File %>
<a href="$URL" target="_blank">
<% if Extension==pdf %>
<img src="../../../themes/name/images/icons/pdf_logo.jpg" />
<% else_if Extension==jpg %>
<% if RatioChecker %>
	$SetWidth(150)
<% else %>
	$SetHeight(110)
<% end_if %>
</a>
<% end_control %>

Finally, i also want to check the image ratio. Because when the width is smaller then the height the images doesn't fill my overflow box (which is 130px x 80px)

im trying something like this, but it doesn't work.

		function RatioChecker() {
				if ($this()->getWidth() > $this()->getHeight()){ 		
							return false;
						}else{
							return true;
						}
					}

template

<% if RatioChecker %>
$SetWidth(150)
<% else %>
$SetHeight(110)
<% end_if %>

Avatar
Zauberfisch

Community Member, 30 Posts

27 November 2010 at 2:39am

there is already a function for what you need

instead of

<% if RatioChecker %>
$SetWidth(150)
<% else %>
$SetHeight(110)
<% end_if %>

you use:

<% SetRatioSize(150,110,1) %>

docs say:
SetRatioSize(80,80) // **New in 2.4** returns an image scaled proportional, with its greatest diameter scaled to 80px

and i belive, if you add a 1 as 3rd param, it will turn the function around, so it is "with its smallest diameter scaled to 80px"

Go to Top