Post

2 followers Follow
0
Avatar

Download Podio item image to server (PHP)

Hi!

I've set up some webhooks that sends Podio-data to a MYSQL-database on my server, works like a charm. Now I only need to fetch the attached image and store the file in a directory on the same server, here's where I'm stuck.

First I was trying to use the direct links (like https://files.podio.com/100719741) but now understand they are not public, so in some way "get_raw" is the way to go but I don't get any of the example code-snippets to work.

I also understood that my hosting provider (Loopia wiki) does not allow allow_url_fopen which leaves out file_get_contents-solution.
Loopia WIKI (Swedish): https://support.loopia.se/wiki/Öppna_och_inkludera_filer_från_fjärrplats

I read, tried and obviously failed to use the following hints:
http://podio.github.io/podio-php/fields/#image-field
https://github.com/podio/podio-php/issues/40
https://developers.podio.com/examples/files
https://developers.podio.com/doc/files/get-raw-file-1004147

A Screenshot:
https://www.dropbox.com/s/3dh1zvyj05fe96g/Screenshot%202014-05-21%2015.06.15.png (echo $file shows a lot of ??!=#€€, is that my image data?)

Questions:
* Do I have to authenticate in some special way to use get_raw?
* Is it possible to show the image response directly in the browser (for debugging purposes)?
* I don't only want the full scale image, but also the medium/badge-versions. How do I do that, now that I can't use https://files.podio.com/image_id/medium.

Can anyone point me in the right direction here? :)

Stefan

Stefan Falkboman

Please sign in to leave a comment.

8 comments

1
Avatar

The gibberish you output is the image data, but your destination path /folder/testbild.jpg doesn't look right. It'll save the image on an absolute path from the root of your file system. That's most likely not what you want. You'll want to remove the starting / to save the file relative to your PHP file.

Andreas Haugstrup Pedersen 1 vote
0
Avatar

Incredible, that was it! So much work for one / ...

I now have:
$item_id = "156896776";
$item = PodioItem::get($item_id);
$file = $item->field(24275404)->files()[0]->get_raw();
file_put_contents("folder/testbild.jpg", $file);

And how about my second question, can I fetch the other file sizes using the same "get_raw" method?

Stefan Falkboman 0 votes
0
Avatar

You can see what the get_raw() method does here: https://github.com/podio/podio-php/blob/master/models/PodioFile.php#L27-L33

If you do the same with a thumbnail URL you can get the resized URLs as well. It's probably better if you download only the original from Podio and generate your own thumbnails from the original (using GDlib or imagemagick).

You can serve an image directly from PHP if you send the right headers: https://www.google.com/search?q=serve+image+from+php&oq=serve+image+from+php&aqs=chrome..69i57j0l5.2819j0j7&sourceid=chrome&es_sm=119&ie=UTF-8

Andreas Haugstrup Pedersen 0 votes
0
Avatar

Ok thanks, I managed to create the thumbnails using Imagemagick instead. But I still need to access the "extra_large"-version from Podio instead of starting from potentially huge full size uploads (that are very slow).

But I can't figure out how to do something like GET_RAW on the extra_large version instead.
I know I want to fetch this URL for instance: https://files.podio.com/103318436/extra_large

  1. GET_RAW gets the "link" attribute.
  2. Theres is no "extra_large_link" attribute.
  3. I tried to replace the "$this->link" with "link + /extra_large".

public function get_raw() {
return Podio::get($this->link, array(), array('file_download' => true))->body;
}

Thanks for you help so far!
Stefan

Stefan Falkboman 0 votes
0
Avatar

I tried doing this without modifying core files but didn't manage (due to poor coding skills).

So I simply duplicated the get_raw function in PodioFile.php and added . "/extra_large" according to you suggestion.

public function get_raw_large() {
return Podio::get($this->link . "/extra_large", array(), array('file_download' => true))->body;
}

That worked, and I suppose it could be done with /medium too :)

Result:
https://www.dropbox.com/s/f2bgyhkez2ujwqr/Screenshot%202014-06-04%2019.05.31.png

Thanks Andreas for all the support!

Stefan

Stefan Falkboman 0 votes
0
Avatar

Ok, I'm on this again and still couldn't figure out how to use the new functionality. I'm most likely just a PHP-looser.

I'm supposed to send the size parameter somehow in the get_raw():

$file = PodioFile::get($image_id);
file_put_contents("folder/".$my_url, $file->get_raw());

I tried:
get_raw(medium));
get_raw($size=medium));
get_raw($size='medium'));
$size='medium'; get_raw());

But I ended up doing it manually :-)

$file = Podio::get($podio_image->values[0]->link . '/medium', array(), array('file_download' => true))->body;

file_put_contents("folder/".$my_url, $file);

Stefan

Stefan Falkboman 0 votes