du1108 2018-07-05 20:27
浏览 65

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

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法