social-webcollage/mastodon.php

41 lines
1.1 KiB
PHP
Raw Normal View History

2021-03-03 21:07:23 +01:00
<?php
use Shikiryu\WebGobbler\Assembler\Superpose;
use Shikiryu\WebGobbler\Config;
use Shikiryu\WebGobbler\Pool;
include 'vendor/autoload.php';
include 'Mastodon/Mastodon.php';
$mastodon_config = require 'mastodon-config.php';
try {
// generate picture
$config = new Config('./config.json');
$pool = new Pool($config);
$upload_image_name = uniqid('superpose_', true);
$image = new Superpose($pool, $config);
$image_path = sprintf('./render/%s.jpg', $upload_image_name);
$image->saveTo($image_path);
// send picture to mastodon
$app = new MastodonAPI($mastodon_config['access_token'], $mastodon_config['domain']);
$curl_file = curl_file_create(realpath($image_path));
$body = ['file' => $curl_file];
$response = $app->uploadMedia($body);
$file_id = $response->id;
$statusText = 'Nouveau collage';
$status_data = [
'status' => $statusText,
'visibility' => 'public',
'language' => 'fr',
'media_ids[]' => $file_id,
];
$app->postStatus($status_data);
} catch (Exception $exception) {
echo $exception->getMessage();
}