html - Save image from PHP URL - returns empty image -
i'm working on small project read data electronic identity cards.
it might worth mentioning i'm using lightopenid php library $attributes['']
data eid.
now i'm stuck trying save image displayed on http://my-url.com/photo.php
photo.php contains:
<?php session_start(); $photo = $_session['photo']; header('content-type: image/jpeg'); echo($photo);
the variable $photo
contains $_session['photo']
comes index.php:
function base64url_decode($base64url) { $base64 = strtr($base64url, '-_', '+/'); $plaintext = base64_decode($base64); return ($plaintext); } $encodedphoto = $attributes['eid/photo']; $photo = base64url_decode($encodedphoto); $_session['photo'] = $photo;
the images both visible on index.php (<?php echo '<img src="photo.php"/>'; ?>
) on photo.php.
i've read on few similar topics , tried following methods:
- curl
$ch = curl_init(); curl_setopt($ch, curlopt_url, 'http://my-url.com/photo.php'); $fp = fopen('./photo/' . $filename . '.jpg', 'wb'); curl_setopt($ch, curlopt_file, $fp); curl_setopt($ch, curlopt_header, 0); curl_exec($ch); curl_close($ch); fclose($fp);
- file_put_contents
$input = "my-url.com/photo.php"; $output = './photo/' . $filename . '.jpg'; file_put_contents($output, file_get_contents($input));
- copy
even tried basic copy:
copy( "http://my-url/photo.php", './photo/' . $filename . '.jpg');
all 3 methods create empty .jpg file in directory want them too.
let me know if need provide code. hope there's can point out mistakes
finally found solution.
i decode base64url once with:
function base64url_decodeonce($base64url){ $base64 = strtr($base64url, '-_', '+/'); return ($base64); }
that way can use $base64 output for:
$data = 'data:image/jpeg;base64,' . $base64data .'\''; list($type, $data) = explode(';', $data); list(, $data) = explode(',', $data); $data = base64_decode($data); file_put_contents('./photo/' . $filename . '.jpg', $data);
Comments
Post a Comment