Jump to:

3433 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [Solved]Return an image object from filename

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

Page: 1
Go to End
Author Topic: 2197 Views
  • NickJacobs
    Avatar
    Community Member
    144 Posts

    [Solved]Return an image object from filename Link to this post

    I have a site where I have a lot of product, all the information (and images) is exported from the company accounting system. All images are in the format [productcode].jpg .

    In my SS site I import all the product objects directly into the database to make it easy on myself. In the front end I need to link the images to the product data objects....It's working fine, but I need to resize the image as I bring it in. at the moment I've got:

    ######### in Product.php ############

    class Product extends DataObject
    {
       .............
       
    function FindProductImage() {

    $filename = 'assets/product-images/' . $this->ProductCode . '.jpg';
    $default_filename = 'themes/allentrading/images/no-product-image.png';
       
    if (Director::fileExists($filename)) {
    $findimagepath = $filename;
    } else {
    $findimagepath = $default_filename;
    }

    $data = array(
    'ProductImagePath' => $smartimagepath
    );
       
    return $this->customise($data);
    }

    In the template I call:

    <% control FindProductImage%>
    <img src="$ProductImagePath" >
    <% end_control %>

    The problem is the images are all different sizes so I need to resize them in the template. What I ideally need to do is return the actual image object and then resize it (in the function)...not sure how to do that...or can I somehow do it in the template??

    any help appreciated!

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: [Solved]Return an image object from filename Link to this post

    Heya,

    The static method File::find($filename) should do the trick.

    It will look for the filename in the database, so your asset files need to be synced with your database. This happens normally whenever you visit AssetAdmin and can also be manually kicked off with the call: FileSystem::sync().

    Eg, if you have automated the image export/import process, call FileSystem::sync() after a batch. This should update your database. Then you can do:

    ...
    function FindProductImage() {
       return File::find('assets/product-images/' . $this->ProductCode . '.jpg');
    }
    ...

    and in your template:

    <% if FindProductImage %>
       $FindProductImage.SetWidth(200)
    <% else %>
       <img src='themes/allentrading/images/no-product-image.png' title='Not Found' />
    <% end_if %>

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: [Solved]Return an image object from filename Link to this post

    Great! that worked like a charm...thanks!

    incidentally, is there any advantage to creating specific image sizes in the function, rather than doing a resize on the template??

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: [Solved]Return an image object from filename Link to this post

    No problem.

    I believe (but don't quote me) that the resulting template will refer to the cached resampled image anyway, so aside from the overhead of resizing the image the first time it is called, it should be exactly the same.

    edit: by resulting template, I mean the ssviewer template generated from your .ss templates. The result should be equivalent

  • schellmax
    Avatar
    Community Member
    126 Posts

    Re: [Solved]Return an image object from filename Link to this post

    thought it's worth mentioning:
    i just had a similar problem, but i was uploading the images through dataobjectmanager, but using 'File' instead of 'Image' in the $has_one relation, as i wanted to upload video files (flv) as well as images (jpg).
    but as the File class doesn't define 'SetWidth' or similar functions, i couldn't resize the images in my templates.
    i was looking for some sort of 'casting' my File object to an Image object, but didn't get far (oop primer).
    this is what i ended up with:

       function myImagefile() {
          $file = DataObject::get_one('File','ID = '.$this->MediafileID);   
          $img = new Image_Cached($file->Filename);
          $img->ID = $file->ID; //image resize function requires an id
          return $img;
       }


    maybe it's of use for someone with the same problem.
    if there is a more elegant way of doing this, i'd be happy to hear about it!

    2197 Views
Page: 1
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.