You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

knalpiap's avatar
knalpiap
Helpful | Level 5
8 years ago

PHP - Show thumbnail

Hi peepz,

I'm running a PHP project for which I read a specific Dropbox directoty using 'list_folder'. Five files (JPG), matching certain criteria are selected from all returned files. So far, so good.
Now, I want to embed these five images in a webpage. Somehow, I don't succeed in this. It's clear to me I'm overlooking something :)

Assumption: I create a e.g. photo.php file which uses 'get_thumbnail' to display the image. I call this photo.php file for each of the five <img> on my page.

When I use the Api Explorer,  the files are found just fine. However, I have no clue on how to implement this into a PHP file. I know the result is some sort of image content already, but I just cannot get it to work. Where do I start?

Thanks,
Knal

  • Jane's avatar
    Jane
    Icon for Dropbox Staff rankDropbox Staff

    Hey knalpiap

    Let's try a couple of things together! 

    I'd be glad to work on that with you if you give me more details in your reply! For instance, could you describe what you've been trying to accomplish step-by-step and (if possible) send us some examples, so that we re-create the behavior & figure out a solution with you.

    Thanks in advance, looking forward to hearing back from you! 

    • knalpiap's avatar
      knalpiap
      Helpful | Level 5

      Hi Jane, I would appreciate that very much! :)

      Until now I've created dropbox.php:

      <?php

      $ch = curl_init();

      curl_setopt($ch, CURLOPT_URL, "https://api.dropboxapi.com/2/files/list_folder"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CAINFO, "cacert.pem"); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_POSTFIELDS, '{"path":"/images","recursive":true,"include_media_info":true,"include_deleted":false}'); curl_setopt($ch, CURLOPT_POST, 1); $headers = array(); $headers[] = "Authorization: Bearer XXX_SECRET_TOKEN_XXX"; $headers[] = "Content-Type: application/json"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close ($ch); $json = json_decode($result, true);


      I filter the $json['entries'] with some functions, which hand me all data of the 5 images I need.
      Eventually, I want to display 5 thumbnails on my page, so I figured I call them separately using another file, image.php.
      Image.php should then (in my opinion) serve the image as if it were an image file. That is where I get lost:

      Image.php looks as follows:

      <?PHP
      
      $ch = curl_init();
      
      curl_setopt($ch, CURLOPT_URL, "https://content.dropboxapi.com/2/files/get_thumbnail");
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_CAINFO, "cacert.pem");
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($ch, CURLOPT_POSTFIELDS, '{"path":"/DropsyncFiles/2017.09.01_22.36.01/20170901223632654.jpg","format":{".tag":"jpeg"},"size":{".tag":"w1024h768"}');
      curl_setopt($ch, CURLOPT_POST, 1);
      
      $headers = array();
      $headers[] = "Authorization: Bearer XXX_SECRET_TOKEN_XXX";
      $headers[] = "Content-Type: application/json";
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      
      $result = curl_exec($ch);
      if (curl_errno($ch)) {
          echo 'Error:' . curl_error($ch);
      }
      curl_close ($ch);
      
      header('Content-Type: image/jpeg');
      imagejpeg( $result );
      imagedestroy( $result );
      
      ?>


      As you can see, I've lost my way and I have no cluo on what I'm doing. I've read the documentation stating that the respons is the image data but I can't seem to output it in the right way. The file however takes some time to load, so something's happening :)

      Should you whish some additional info, please let me know!

      Thank you,
      Knal

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,888 PostsLatest Activity: 21 hours ago
326 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!