first, what I'd like to achieve is the following:
I need to pass an image from an external source into the underlying database of structr. therefore I need to convert that image to base64. But as I'm not aware of a way to get serverside javascript/rhino to receive an image via url and convert it into base64, I came up with a workaround:
so I wrote two schema methods, one (script A) in serverside javascript and the other (script B) in php.
script A kind of imports data from an external REST API, builds the external image url and needs to pass it to script B which will use cURL to get the image, convert it to base64 and hand it back to script A.
So that's where I'm stuck right now. Help Article on Custom Schema Methods doesn't really help, because neither covers it PHP syntax nor does it state if it's even possible to return something from a custom schema method.
evaluate_script() or replace() are other possibilities, but I'm still not sure how to use it within the given context.
Maybe there's another way handling that issue, I'm open for suggestions.
SCRIPT A: Serverside Javascript Method:
var url = 'image_url",
instance = Structr.get('this'),
base64String = Instance.PHPMethod(url);
//then write base64String to structr database
SCRIPT B: PHP Method:
php{
<?php
$url = retrieve.url from SCRIPT A ????;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
$b64 = base64_encode($data);
echo $b64;
?>
}
Thanks in advance.