provide function to fetch photo contents from url

This commit is contained in:
zotlabs 2018-05-15 16:51:04 -07:00
parent 16930c1c54
commit aac5fd96cc
3 changed files with 27 additions and 10 deletions

View File

@ -55,10 +55,10 @@ class Linkinfo extends \Zotlabs\Web\Controller {
$h = explode("\n",$result['header']); $h = explode("\n",$result['header']);
foreach ($h as $l) { foreach ($h as $l) {
list($k,$v) = array_map("trim", explode(":", trim($l), 2)); list($k,$v) = array_map("trim", explode(":", trim($l), 2));
$hdrs[$k] = $v; $hdrs[strtolower($k)] = $v;
} }
if (array_key_exists('Content-Type', $hdrs)) if (array_key_exists('content-type', $hdrs))
$type = $hdrs['Content-Type']; $type = $hdrs['content-type'];
if($type) { if($type) {
$zrl = is_matrix_url($url); $zrl = is_matrix_url($url);
if(stripos($type,'image/') !== false) { if(stripos($type,'image/') !== false) {

View File

@ -83,7 +83,7 @@ class Photo extends \Zotlabs\Web\Controller {
$data = file_get_contents($data); $data = file_get_contents($data);
} }
if(! $data) { if(! $data) {
$data = file_get_contents($default); $data = fetch_image_from_url($default,$mimetype);
} }
if(! $mimetype) { if(! $mimetype) {
$mimetype = 'image/png'; $mimetype = 'image/png';
@ -183,16 +183,13 @@ class Photo extends \Zotlabs\Web\Controller {
switch($resolution) { switch($resolution) {
case 4: case 4:
$data = file_get_contents(z_root() . '/' . get_default_profile_photo()); $data = fetch_image_from_url(z_root() . '/' . get_default_profile_photo(),$mimetype);
$mimetype = 'image/png';
break; break;
case 5: case 5:
$data = file_get_contents(z_root() . '/' . get_default_profile_photo(80)); $data = fetch_image_from_url(z_root() . '/' . get_default_profile_photo(80),$mimetype);
$mimetype = 'image/png';
break; break;
case 6: case 6:
$data = file_get_contents(z_root() . '/' . get_default_profile_photo(48)); $data = fetch_image_from_url(z_root() . '/' . get_default_profile_photo(48),$mimetype);
$mimetype = 'image/png';
break; break;
default: default:
killme(); killme();

View File

@ -1011,3 +1011,23 @@ function profile_photo_set_profile_perms($uid, $profileid = 0) {
} }
} }
} }
function fetch_image_from_url($url,&$mimetype) {
$redirects = 0;
$x = z_fetch_url($url,true,$redirects,[ 'novalidate' => true ]);
if($x['success']) {
$hdrs = [];
$h = explode("\n",$x['header']);
foreach ($h as $l) {
list($k,$v) = array_map("trim", explode(":", trim($l), 2));
$hdrs[strtolower($k)] = $v;
}
if (array_key_exists('content-type', $hdrs))
$mimetype = $hdrs['content-type'];
return $x['body'];
}
return EMPTY_STR;
}