type = "photo"; $photo->title = $title; $photo->description = $description; $photo->name = $name; $pi = pathinfo(basename($filename)); if (empty($pi["extension"])) { throw new Exception("@todo UNKNOWN_FILE_TYPE"); } while (ORM::Factory("item") ->where("parent_id", $parent_id) ->where("name", $photo->name) ->find()->id) { $photo->name = rand() . "." . $pi["extension"]; } copy($filename, $photo->file_path()); // This saves the photo $photo->add_to_parent($parent_id); /** @todo: parameterize these dimensions */ // This saves the photo a second time, which is unfortunate but difficult to avoid. return $photo->set_thumbnail($filename, 200, 140) ->set_resize($filename, 800, 600) ->save(); } }