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.

Data Model Questions /

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

[SOLVED] Create Image from file_put_contents


Go to End


2 Posts   5021 Views

Avatar
zenmonkey

Community Member, 545 Posts

8 December 2012 at 1:12pm

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

Avatar
Willr

Forum Moderator, 5523 Posts

8 December 2012 at 4:24pm

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