更好和更快的方式来显示远程图像:src =“ http:// URI_IMAGE”,使用php的file_get_contents()的base64或cURL的base64

时间:2018-07-05 20:27:16

标签: php curl base64 file-get-contents

我需要清理,我想知道在html页面中显示图像的最有效,最快的方法。在通过下载file_get_contents加载远程图像,然后转换为base64,或者使用cURL下载,然后转换为base64或简单地在img标签中传递原始url的过程中运行。

php代码: -具有file_get_contents

的base64
$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/>";

-原始网址

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

感谢所有帮助

0 个答案:

没有答案