cjrasmussen / BlueskyApi

Simple helper for interacting with the Bluesky API/AT protocol
MIT License
16 stars 3 forks source link

Error uploading image #2

Closed edel79 closed 1 year ago

edel79 commented 1 year ago

Hello, me again. I'm using your class to send a message to Bluesky, with a picture attached as upload.

It's working using a quite small picture, but when I want to upload a bigger one (a photo taken from an iphone), this doesn't work. Prior to uploading the picture, I first convert it from HEIC to JPG and then I strip all the exif data. Lastly I resize it because BlueSky uploads are limited to 1 Mb files (at least using the api).

Maybe the error is due to these preprocessing tasks.

Anyway, here is the error encontered (several seconds after sending the request) :

on the class side : Fatal error: Uncaught JsonException: Syntax error in BlueskyApi.php:147

json_decode('', false, 512, 4194304)

and on my script on this line : bluesky->request('POST', 'com.atproto.rep...', Array, '\xFF\xD8\xFF\xE0\x00\x10JFIF\x00\x01\x01\x01\x00...', 'image/jpg')

Can you see where could be the error ?

edel79 commented 1 year ago

Here is the entire code used to upload the image :

// Convert HEIC to JPG $image_to_convert = 'IMG_0015.HEIC'; $image_converted = 'test.jpg'; $heic_tool_path = './utils/heicToJpg'; exec($heic_tool_path.' '.$image_to_convert.' '.$image_converted, $output, $retval); echo "Returned with status $retval and output:\n"; print_r($output);

// Remove EXIF Info $img = new Imagick($image_converted); $profiles = $img->getImageProfiles("icc", true); if(!empty($profiles)) { $img->profileImage("icc", $profiles['icc']); } $img->stripImage(); $img->resizeImage($img->getImageWidth() 0.5, $img->getImageHeight() 0.5, Imagick::FILTER_LANCZOS, 1); $img->writeImage($image_converted); $img->clear(); $img->destroy();

// Send Image $imgfile = file_get_contents($image_converted); $response = $bluesky->request('POST', 'com.atproto.repo.uploadBlob', [], $imgfile, 'image/jpg'); $image = $response->blob;

cjrasmussen commented 1 year ago

I can't say for certain because my test didn't return the exact same result as yours, but I'd recommend sending the MIME type as image/jpeg rather than image/jpg. The Bluesky web app uses image/jpeg and when I used that, my test succeeded, but when I used image/jpg (as you are) it failed (though in a different way from your example).

edel79 commented 1 year ago

Yes you're right ! Indeed setting JPEG as mime type did solve the problem.

Also I suspect my photo was a little too heavy as the BlueSky API limits the size of each picture uploaded. I think that 1 MB is currently the maximum allowed.