Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [SOLVED] Create Image from file_put_contents

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

Page: 1
Go to End
Author Topic: 915 Views
  • zenmonkey
    Avatar
    Community Member
    528 Posts

    [SOLVED] Create Image from file_put_contents Link to this post

    I have a class that downloads images from an external server to my SS site. What is the best way to create an image Object for a has_one relation from this file. Off the top of my head I was going to manully create a new Image from the file name and then after write get its ID and attach it to the other DataObject. But manually craete the Image object in my file table seems redundant. I assume I should just be able to use the built in file system to handle my file write and file object creation, just not sure where to start

    Thanks

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: [SOLVED] Create Image from file_put_contents Link to this post

    I was going to manully create a new Image from the file name and then after write get its ID and attach it to the other DataObject.

    You're on the correct track.

    But manually craete the Image object in my file table seems redundant.

    No you'll need to manually create the Image object as well as the actual file and ensure that the Image object you created has the correct filename to link to the image.

    file_put_contents('assets/foo.jpg', $img);

    $image = new Image();
    $image->Filename = 'assets/foo.jpg';
    $image->Title = 'Foo';
    $image->write();

    $obj->ImageID = $image->ID:
    $obj->write();

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