Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Solved! Download section

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2
Go to End
Author Topic: 1214 Views
  • UncleCheese
    Avatar
    4085 Posts

    Re: Solved! Download section Link to this post

    Only Image objects have GD functions.

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

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: Solved! Download section Link to this post

    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.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Solved! Download section Link to this post

    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..

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: Solved! Download section Link to this post

    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();
             }
          }
       }

    }

  • UncleCheese
    Avatar
    4085 Posts

    Re: Solved! Download section Link to this post

    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

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: Solved! Download section Link to this post

    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 %>

  • Zauberfisch
    Avatar
    Community Member
    30 Posts

    Re: Solved! Download section Link to this post

    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"

    1214 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.