Resize picture before uploading

pull/1410/head
Michael 2023-08-10 21:26:10 +00:00
parent 603c8defb1
commit 21006aec74
1 changed files with 11 additions and 3 deletions

View File

@ -46,11 +46,14 @@ use Friendica\Model\Item;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Core\Config\Util\ConfigFileManager; use Friendica\Core\Config\Util\ConfigFileManager;
use Friendica\Model\Photo; use Friendica\Model\Photo;
use Friendica\Object\Image;
use Friendica\Util\Images; use Friendica\Util\Images;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack; use GuzzleHttp\HandlerStack;
use GuzzleHttp\Subscriber\Oauth\Oauth1; use GuzzleHttp\Subscriber\Oauth\Oauth1;
const TWITTER_MAX_IMAGE_SIZE = 500000;
function twitter_install() function twitter_install()
{ {
Hook::register('load_config' , __FILE__, 'twitter_load_config'); Hook::register('load_config' , __FILE__, 'twitter_load_config');
@ -262,15 +265,20 @@ function twitter_post_status(int $uid, string $status, array $media_ids = [], st
function twitter_upload_image(int $uid, array $image) function twitter_upload_image(int $uid, array $image)
{ {
if (!empty($image['id'])) { if (!empty($image['id'])) {
$photo = Photo::selectFirst(['resource-id'], ['id' => $image['id']]); $photo = Photo::selectFirst([], ['id' => $image['id']]);
$photo = Photo::selectFirst([], ["`resource-id` = ? AND `scale` > ?", $photo['resource-id'], 0], ['order' => ['scale']]);
} else { } else {
$photo = Photo::createPhotoForExternalResource($image['url']); $photo = Photo::createPhotoForExternalResource($image['url']);
} }
$picturedata = Photo::getImageForPhoto($photo); $picturedata = Photo::getImageForPhoto($photo);
Logger::info('Uploading', ['uid' => $uid, 'size' => strlen($picturedata), 'image' => $image]); $type = Images::getMimeTypeByData($picturedata, $photo['filename'], $photo['type']);
$picture = Photo::resizeToFileSize(new Image($picturedata, $type), TWITTER_MAX_IMAGE_SIZE);
$picturedata = $picture->asString();
Logger::info('Uploading', ['uid' => $uid, 'size' => strlen($picturedata), 'type' => @getimagesizefromstring($picturedata), 'photo' => $photo]);
$media = twitter_post($uid, 'https://upload.twitter.com/1.1/media/upload.json', 'form_params', ['media' => base64_encode($picturedata)]); $media = twitter_post($uid, 'https://upload.twitter.com/1.1/media/upload.json', 'form_params', ['media' => base64_encode($picturedata)]);
if (isset($media->media_id_string)) { if (isset($media->media_id_string)) {