4

I have a HTML form with a file upload button. I want the user to be able to select an image and submit the form, then the server should upload the file to imgur using PHP and retrieve the direct link of that image (meaning the URL should end with the .jpg or .png, etc.)

I would really appreciate it if anyone can show me how to upload an image to imgur using PHP and then retrieve the direct link to that image so that I can store the link to a MySQL database. If there is no way to retrieve the direct image link, then the normal imgur link would also work. Meaning something like this will also work:

https://imgur.com/gallery/KlzhGuE

But, the first priority is something like this:

https://i.imgur.com/KlzhGuE.jpg

Edit: So, I managed to upload the image and get a json reply using the api. After I decode the json that is returned back and var_dump it, I get this:

object(stdClass)#1 (3) {
  ["data"]=>
  object(stdClass)#2 (21) {
    ["id"]=>
    string(7) "yPfV2Tq"
    ["title"]=>
    NULL
    ["description"]=>
    NULL
    ["datetime"]=>
    int(1456813618)
    ["type"]=>
    string(9) "image/png"
    ["animated"]=>
    bool(false)
    ["width"]=>
    int(123)
    ["height"]=>
    int(23)
    ["size"]=>
    int(3149)
    ["views"]=>
    int(0)
    ["bandwidth"]=>
    int(0)
    ["vote"]=>
    NULL
    ["favorite"]=>
    bool(false)
    ["nsfw"]=>
    NULL
    ["section"]=>
    NULL
    ["account_url"]=>
    NULL
    ["account_id"]=>
    int(0)
    ["comment_preview"]=>
    NULL
    ["deletehash"]=>
    string(15) "bNjtF9OSdG1QAM7"
    ["name"]=>
    string(0) ""
    ["link"]=>
    string(30) "https://i.stack.imgur.com/psNPA.png"
  }
  ["success"]=>
  bool(true)
  ["status"]=>
  int(200)
}

Now how do it extract the part saying https://i.stack.imgur.com/psNPA.png ?

Fil
  • 8,225
  • 14
  • 59
  • 85
Adib
  • 359
  • 1
  • 6
  • 16
  • 1
    Does this explain what you're looking for: http://subinsb.com/uploading-images-using-imgur-api-in-php? – stratedge Mar 01 '16 at 04:38
  • I looked into this one before, but many things changed since this was posted, so couldn't get it to work. Imgur also had changed many things since then I mean in their api. – Adib Mar 01 '16 at 04:41
  • 1
    Understandable. Can you use composer to pull in libraries? There look to be at least 2 libraries available for interacting with the Imgur API which would make your life easy if you can. https://github.com/Adyg/php-imgur-api-client looks promising and includes documentation how to upload an image using the library (https://github.com/Adyg/php-imgur-api-client/blob/master/doc/Image.md). – stratedge Mar 01 '16 at 04:48
  • I think the one you posted will work, only if I can authenticate using their new authentication system. Could you please help me with that if you have any idea? – Adib Mar 01 '16 at 04:49
  • thanks, that "might" solve all the problem – Adib Mar 01 '16 at 04:49
  • 1
    Yeah, on the authentication side, you'll need to register your application with Imgur if you haven't yet to get a client ID and secret (https://api.imgur.com/#registerapp), and then the library should handle most of the rest for you – stratedge Mar 01 '16 at 04:52
  • Could you please look into the edit part above? @xjstratedgebx – Adib Mar 01 '16 at 06:31
  • 1
    The URL would be in `$json->data->link` – drew010 Mar 01 '16 at 06:42
  • thanks a lot man, so finally its all solved – Adib Mar 01 '16 at 08:42

1 Answers1

1

Ok so I have found the way how to do it, Used help from here: using imgur api v3 to upload images anonymously using php

Used the code provided by h0tw1r3 in his answer. And used help from drew010 for extracting the link by using

$reply->data->link;

in addition to the code provided by h0tw1r3. Anyways thanks to xjstratedgebx for trying.

Community
  • 1
  • 1
Adib
  • 359
  • 1
  • 6
  • 16