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.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Solved! Download section


Reply

15 Posts   1427 Views

Avatar
UncleCheese

26 November 2010 at 4:31am 4085 Posts

Only Image objects have GD functions.

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

Avatar
borriej

26 November 2010 at 4:36am Community Member, 267 Posts

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

26 November 2010 at 5:47am 4085 Posts

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

26 November 2010 at 11:09am Community Member, 267 Posts

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

26 November 2010 at 2:03pm 4085 Posts

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

26 November 2010 at 10:53pm (Last edited: 26 November 2010 10:53pm), Community Member, 267 Posts

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

27 November 2010 at 2:39am Community Member, 30 Posts

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"