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.

Data Model Questions

[SOLVED] Create Image from file_put_contents


Reply

2 Posts   949 Views

Avatar
zenmonkey

8 December 2012 at 1:12pm Community Member, 528 Posts

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

8 December 2012 at 4:24pm Forum Moderator, 5511 Posts

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