0

I am making an Instagram post scheduler for that I need an API from which we can select images from our device then that API converts that image into a URL so that I can use it to post my request to Instagram.

If there's any way to do this please help me.

Aryan
  • 111
  • 2
  • 7
  • a URL points to a resource - you can't convert an image on your device into an URL without that image being accessible on the internet - i.e. you'd upload the image to some server, then the URL would be the location of that image on that server – Bravo Sep 24 '21 at 02:35
  • 1
    Try using firebase storage. You can upload the image there and then fetch the URL. – Shubham Garg Sep 24 '21 at 02:45
  • 1
    If you don't want to store the image to a server, perhaps the base64 encoding of your local file could help you. Some snippets are given here : https://stackoverflow.com/questions/6150289/how-can-i-convert-an-image-into-base64-string-using-javascript – Philippe Sep 24 '21 at 02:54
  • @ShubhamGarg I can use firebase but for that, I need to know to the exact location of the file. But I want a box to appear from that we can select the file we need from the device. Like they have for Unsplash or photoshop. – Aryan Sep 24 '21 at 03:17

1 Answers1

0

imgur site provides an api to automate this. Here is some demo code:

$('document').ready(function () {
  $('input[type=file]').on('change', function () {
    var $files = $(this).get(0).files;

    if ($files.length) {
      // Reject big files
      if ($files[0].size > $(this).data('max-size') * 1024) {
        console.log('Please select a smaller file');
        return false;
      }

      // Begin file upload
      console.log('Uploading file to Imgur..');

      // Replace ctrlq with your own API key
      var apiUrl = 'https://api.imgur.com/3/image';
      var apiKey = 'ctrlq';

      var settings = {
        async: false,
        crossDomain: true,
        processData: false,
        contentType: false,
        type: 'POST',
        url: apiUrl,
        headers: {
          Authorization: 'Client-ID ' + apiKey,
          Accept: 'application/json',
        },
        mimeType: 'multipart/form-data',
      };

      var formData = new FormData();
      formData.append('image', $files[0]);
      settings.data = formData;

      // Response contains stringified JSON
      // Image URL available at response.data.link
      $.ajax(settings).done(function (response) {
        console.log(response);
      });
    }
  });
});

**but to use their procedure you have to register your app and boom. Here is their Documentation.

https://www.labnol.org/code/20526-javascript-image-uploader

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
PRANESH
  • 23
  • 7