base64 - php base64_encode image displayed on the site from external website -
i getting content first website , displaying on other website using file_get_contents()
, using preg_replace()
, preg_match()
modify output way want it.
now, there images base64_encode()
stuck on figuring out how that?
if use line of code:
preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $fourth_content, $out)
i can print out images src
cannot encode them , send them src.
does know how can achieve this?
i'd advise think in steps. have do?
- fetch html remote url
complete
- grab every image
complete
- display every image
todo
you have image urls, what? want iterate on every image.
sidenote: every image want grab means new http request (download). builds loading time. think: want? if so, let's break down:
step 1
get html url.
<?php // url $url = 'https://twitter.com/dogethedog'; // html url $data = file_get_contents($url);
step 2
grab every image.
// grab every image source preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $data, $out);
step 3a
do every image url got.
// loop on every image source foreach($out[1] $imageurl){
step 3b
download image data our url.
for base64 encoded image display, need content type of image. can grabbed php function curl_getinfo()
.
more info base64 images in html
more info curl, it's safer e.g. images
// use curl fetch image data , image type // need image type able display jpg, png, gif, ... $ch = curl_init($imageurl); curl_setopt($ch, curlopt_ssl_verifypeer, false); // https/ssl curl_setopt($ch, curlopt_returntransfer, true); // standard curl option // fetch curl content $imagedata = curl_exec($ch); $imagetype = curl_getinfo($ch, curlinfo_content_type); // close curl session curl_close($ch);
step 3c
now have data , image type, render <img>
tag.
// image data gets fetched blob, need base64 $imagedataencoded = base64_encode($imagedata); // build html <img> tag proper type , base encoded image data ?> <img src="data:<?php print $imagetype ?>;base64,<?php print $imagedataencoded ?>" alt="could not fetch image"> <?php } ?>
Comments
Post a Comment