0

I need a clearing please, I would like to know the most efficient and fastest way to display an image in a html page. runs between loading the remote image by downloading with file_get_contents and then converting to base64, or downloading with cURL and then converting to base64 or to simply passing the raw url in the img tag..

php code: - base64 with file_get_contents

$arrContextOptions=array("ssl"=>array("verify_peer"=>false,"verify_peer_name"=>false,));
$type_ext = pathinfo($image_path, PATHINFO_EXTENSION);
$imgData = file_get_contents($image_path, false, stream_context_create($arrContextOptions));
$imgBase64Data = base64_encode($imgData);
$imageData = 'data:image/'.$type_ext.';base64,' .$imgBase64Data;
$sizes = getimagesize($imageData);

-base64: cURL

$url = IMAGE_URL;
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
 $data = curl_exec($ch);
 curl_close($ch);

 $imageData = base64_encode($this->curl_get_contents($data));
 $mime_types = array(
   'gif' => 'image/gif',
   'jpg' => 'image/jpg',
   'jpeg' => 'image/jpeg',
   'png' => 'image/png',
   'bmp' => 'image/bmp'
  );
  $ext = pathinfo($image, PATHINFO_EXTENSION);

 if (array_key_exists($ext, $mime_types)) {
    $a = $mime_types[$ext];
 }
echo "<img src='data: '.$a.';base64,'.$imageData/>";

-raw url

echo "<img src='IMAGE_URL'/>";

thank for all help

kevin kemta
  • 90
  • 10
  • Depends on how many images and size of the images. base64 will bloat the image by approximately 33% due to the encoding. With smaller images this would be less of an issue than the additional http calls. If the images can be cached via cdn (or your server), you would probably be best to use that. If not, I would suggest using a server that supports http/2 maybe to take advantage of multiplexing. Really though, this is likely an opinionated question and highly depends on your setup and requirements. – Jonathan Jul 05 '18 at 20:34
  • Also, not to mention that in this example you are getting the image on your server and essentially "proxying" it to the user via curl/file_get_contents (your server is making the request on behalf of the user and serving it via encoded). In this example it would be better to just use img tags as you are preventing the user from caching the image at all (unless the webpage was also cached). – Jonathan Jul 05 '18 at 20:41

0 Answers0