cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: PHP - Show thumbnail

PHP - Show thumbnail

knalpiap
Helpful | Level 5
Go to solution

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

1 Accepted Solution

Accepted Solutions

knalpiap
Helpful | Level 5
Go to solution

Wow!! I've managed to work it out 🙂
I've read the following post again https://www.dropboxforum.com/t5/API-support/API-v2-Simple-image-gallery-on-my-website-using-dropbox/...

After this, all I additionally changed is

 

echo $result;

to

 

$data = base64_encode( $result );
echo '<img src="data&colon;image/jpg;charset=utf8;base64,'.$data.' />';

and now it works!! Thank you so much!

Best,
Knal

View solution in original post

10 Replies 10

Jane
Dropbox Staff
Go to solution

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! 

 


Jane
Community Moderator @ Dropbox
dropbox.com/support

 

Heart Did this post help you? If so please give it a Like below. 
:white_check_mark: Did this post fix your issue/answer your question? If so please press the 'Accept as Best Answer' button to help others find it.
:arrows_counterclockwise: Still stuck? Ask me a question! (
Questions asked in the community will likely receive an answer within 4 hours!)

knalpiap
Helpful | Level 5
Go to solution

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

Greg-DB
Dropbox Staff
Go to solution

[Cross-linking for reference: https://stackoverflow.com/questions/46043402/php-dropbox-api-v2-show-thumbnail ]

 

First, I would recommend doing a `echo $result;` to make sure the call worked. (You're checking for a curl error, but even if the connection succeeds, you need to check if the Dropbox API call was valid.)

 

Note that /2/files/get_thumbnail is a Content-Download style endpoint, so you need to call it differently than /2/files/list_folder, which is an RPC style endpoint.

 

There's an example of making a Content-Download call (albeit for /2/files/download, but it will work about the same) here:

 

https://stackoverflow.com/documentation/dropbox-api/408/downloading-a-file/20965/downloading-a-file-...

 

Looking at your code, there seems to be a few things you will need to update:

- Content-Download calls require the parameters in a header, not the body.

- Your parameters don't seem to be valid JSON. (You appear to be missing a closing '}').

- You should send an empty 'Content-Type', not 'application/json'.

- Not strictly required, but we highly discourage disabling SSL verification.

 

Once you get the thumbnail data, what you do with it is up to you. E.g., save it to a file, such as in the example, stream back somewhere, etc. 

knalpiap
Helpful | Level 5
Go to solution

Hi Greg,

Thanks a lot for pointing me in the right direction!
I've changed the code based on what I understood from your input:

 

<?PHP

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://content.dropboxapi.com/2/files/get_thumbnail");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$headers = array();
$headers[] = "Authorization: Bearer XXX_SECRET_CODE_XXX";
$headers[] = "Content-Type:";
$headers[] = 'Dropbox-API-Arg: {"path":"/DropsyncFiles/2017.09.01_22.36.01/20170901223632654.jpg","format":{".tag":"jpeg"},"size":{".tag":"w1024h768"}}';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

//echo imagecreatefromstring( $result ); <-- Returns 'Resource id #2' in the browser

echo $result;

?>

The current output is complete jibberish like follows:

 

ÿØÿàJFIFÿâDICC_PROFILE4mntrRGB XYZ Þ+8acspöÖÓ- descüybkptxwtptŒcprt rXYZ¸gXYZÌbXYZàrTRCô@gTRCô@bTRCô@descsRGB IEC61966-2-1 black scaledXYZ 3¤XYZ öÖÓ-textDropbox, Inc.XYZ o¢8õXYZ b™·…ÚXYZ $ „¶ÏcurvÅÌb“kö@Q4!ñ)2;’FQv]íkpz‰²š|¬i¿~ÓÁé7ÿÿÿÛC  %# , #&')*)-0-(0%()(ÿÛC   (((((((((((((((((((((((((((((((((((((((((((((((((((ÿÀà€"ÿÄ	ÿĵ}!1AQa"q2‘¡#B±ÁRÑð$3br‚	%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ	ÿĵw!1AQaq"2B‘¡±Á	#3RðbrÑ $4á%ñ&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚâãäåæçèéêòóôõö÷øùúÿÚ?ùÁIèiØëH£¹ÇáÔúö®C±7Ô@:ҁJZP2;ÒZħãšw§/¿SíI”•„ 8§(ã¯N´¸â”Th7…>?¼=&:Զɺ@=Çó© Èßï1÷¦Ž¼TÌ “íI°z÷¤	€jYäS“Óˆ4У’EJT0;_óé@Y•ñ€~´cßó§m眿.csž¿•(O§ãONs€E:„	\g¿#Ú€½y©(¢Ã°Í¾ÿ­êj}ÂÚÆ98¤úÓÆÀàQBbh`zP^ <€zÐQpåJ úS€Çnih¸(÷Ž:S@>§­KE(мž´mÁõ§Q@$&:ã½-P0¤õÇzZ(Æ@ tâ–Š(¢Š(£PEPEPœPG¹¥¢€ŒR0zÓ¨ w§NÔm8ì u	‘mãÚ“¥›zàÓL›ãÞ’¤aüñL c#¥4ÃÔJkô4ïÆšãå4Кº)H:ÒGO¶i©Ðç­l¶1¶£ñSÛu÷#Ö¢#$sRہžÝ+9lTw-Žÿ…7ƒŸPqO8éH}ˆ÷â²¹ºÐf ¡à`SñþsMÆ:tÍ"®F}(©äži»@Zi¢6“§¯±¤§qlã¶i¸Å;¥'NûRCLAžýèçg9ýhö¦˜ÛÉéIj“ÜqøS(Ä?Nµ8äTH9Õ§|:à`r…J·ëÞº­3áÅŬ†îxî±…þb¿4g¨ïúW4ñT¡£‘¢¤Ï8Û‚ÙõýA"Œg©VÐÐ&µ¼ŠÚvY¡¸Ï“:tcéÏC팊ÈòÝT³FÁ7 ùñ[


... but then a lot longer.
I imagine this is the actual image data, but now I can't output it as an image. Also, I've read your endpoint download link again (I did before posting) but I can't figure out what this means for my code.

My apologies, I am not very good at coding as you can see 🙂

Thanks,
Knal

knalpiap
Helpful | Level 5
Go to solution

Wow!! I've managed to work it out 🙂
I've read the following post again https://www.dropboxforum.com/t5/API-support/API-v2-Simple-image-gallery-on-my-website-using-dropbox/...

After this, all I additionally changed is

 

echo $result;

to

 

$data = base64_encode( $result );
echo '<img src="data&colon;image/jpg;charset=utf8;base64,'.$data.' />';

and now it works!! Thank you so much!

Best,
Knal

lrduques
Helpful | Level 6
Go to solution

Hi!! 

I am having the same issue here. I tried to read to documentation at the link below but I received a message saying: access denied!

https://www.dropboxforum.com/t5/archive-board/API-v2-Simple-image-gallery-on-my-website-using-dropbo...

Can you help me out pleeease?

Thank you in advance.

Greg-DB
Dropbox Staff
Go to solution

@lrduques Apologies for the trouble. Please try that forum link again; it should work now.

lrduques
Helpful | Level 6
Go to solution

It worked!! Thank you!

Pavlitto
Explorer | Level 4
Go to solution

Your answer have a little problem,

$data = base64_encode( $result );
echo '<img src="data&colon;image/jpg;charset=utf8;base64,'.$data.' />';

after the $data at the end you should ad ( " ) before closing so this should look like this

$data = base64_encode( $result );
echo '<img src="data&colon;image/jpg;charset=utf8;base64,'.$data.' " />';
Need more support?