]> git.mxchange.org Git - friendica-addons.git/commitdiff
Resize picture before uploading
authorMichael <heluecht@pirati.ca>
Thu, 10 Aug 2023 21:26:10 +0000 (21:26 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 10 Aug 2023 21:26:10 +0000 (21:26 +0000)
twitter/twitter.php

index 41105ced08bc0d01d64b9756cd3c511c6eccbec9..a644aec4c8d8a2fbe7c61944d64584bc74f33b17 100644 (file)
@@ -46,11 +46,14 @@ use Friendica\Model\Item;
 use Friendica\Model\Post;
 use Friendica\Core\Config\Util\ConfigFileManager;
 use Friendica\Model\Photo;
+use Friendica\Object\Image;
 use Friendica\Util\Images;
 use GuzzleHttp\Client;
 use GuzzleHttp\HandlerStack;
 use GuzzleHttp\Subscriber\Oauth\Oauth1;
 
+const TWITTER_MAX_IMAGE_SIZE = 500000;
+
 function twitter_install()
 {
        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)
 {
        if (!empty($image['id'])) {
-               $photo = Photo::selectFirst(['resource-id'], ['id' => $image['id']]);
-               $photo = Photo::selectFirst([], ["`resource-id` = ? AND `scale` > ?", $photo['resource-id'], 0], ['order' => ['scale']]);
+               $photo = Photo::selectFirst([], ['id' => $image['id']]);
        } else {
                $photo = Photo::createPhotoForExternalResource($image['url']);
        }
 
        $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)]);
 
        if (isset($media->media_id_string)) {