2010-10-19 97 views
204

你能指導我如何將圖像從URL轉換爲base64編碼?如何將圖像轉換爲base64編碼?

+3

您想要做什麼? – 2010-10-19 10:46:34

+4

將它嵌入Img標籤的src屬性 – Volatil3 2010-10-20 03:31:42

+4

這是一箇舊線程,但爲了完整起見: 對於在css中使用,編碼圖像,特別是小圖像(小於1k)是很有意義的。這樣可以節省一個請求,這會花費更長的時間,甚至可能會更大。 – arminrosu 2012-10-31 12:46:23

回答

389

我認爲它應該是:

$path = 'myfolder/myimage.png'; 
$type = pathinfo($path, PATHINFO_EXTENSION); 
$data = file_get_contents($path); 
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); 
+6

這取決於你需要的圖像。如果它是一個內聯網址,這是要走的路。儘管如此,它與單純的base64編碼並不相同。 – 2013-07-31 13:21:49

97

簡單:

$imagedata = file_get_contents("/path/to/image.jpg"); 
      // alternatively specify an URL, if PHP settings allow 
$base64 = base64_encode($imagedata); 

記住,這將由33%擴大的數據,你就會有文件的大小超過您memory_limit問題。

+26

+1的數據放大 – 2013-07-31 12:59:07

19

也使用這種方式來表示使用Base64編碼格式的圖片 發現PHP函數file_get_content和未來使用功能base64_encode

,並得到結果準備str作爲data:" . file_mime_type . " base64_encoded string。在img src屬性中使用它。請參閱以下代碼,我可以爲您提供幫助。

// A few settings 
$img_file = 'raju.jpg'; 

// Read image path, convert to base64 encoding 
$imgData = base64_encode(file_get_contents($img_file)); 

// Format the image SRC: data:{mime};base64,{data}; 
$src = 'data: '.mime_content_type($img_file).';base64,'.$imgData; 

// Echo out a sample image 
echo '<img src="'.$src.'">'; 
+1

如果找不到函數mime_content_type,只需從http://vpn.php.net/manual/en/function.mime-content-type.php中加入函數 – 2013-12-10 20:00:48

+0

@Raju Rajotia Jangid cool +1 – pregmatch 2014-04-04 14:50:23

7
<img src="data:image/png;base64,<?php echo base64_encode(file_get_contents("IMAGE URL HERE")) ?>"> 

我試圖用這個資源,但一直得到一個錯誤,我發現上面的代碼完美地工作。

與圖像的URL只要置換後的圖像URL HERE - http://www.website.com/image.jpg

+0

感謝您將它壓縮爲儘可能小,並且您包含了img標記用例。最好的答案是肯定的。 – BarryMode 2016-12-22 16:16:36

0

這裏是上傳編碼,並將其保存到MySQL

if (!isset($_GET["getfile"])) { 
    if ($_FILES["file"]["error"] > 0) { 
     echo "Error: " . $_FILES["file"]["error"] . "<br>"; 
    } else { 
     move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]); 

     $bin_string = file_get_contents($_FILES["file"]["name"]); 
     $hex_string = base64_encode($bin_string); 
     $mysqli = mysqli_init(); 

     if (!$mysqli->real_connect('localhost', 'root', '', 'arihant')) { 
      die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); 
     } 

     $mysqli->query("INSERT INTO upload(image) VALUES ('" . $hex_string . "')"); 
    } 
} 

用於顯示圖像使用此

代碼
echo "<img src='data:image/jpeg;base64, $image' width=300>"; 
+4

這不是要求。 – AlexioVay 2017-02-03 05:38:25

2

這是一個使用cURL調用的示例..這比file_get_contents()函數更好。當然,使用BASE64_ENCODE()

$url = "http://example.com"; 

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$output = curl_exec($ch); 
curl_close($ch); 
?> 

<img src="data:image/png;base64,<?php echo base64_encode($output);?>"> 
+3

爲什麼它更好? – ruuter 2016-02-13 22:12:40

3

非常簡單,將常用的有:

function getDataURI($imagePath) { 
    $finfo = new finfo(FILEINFO_MIME_TYPE); 
    $type = $finfo->file($imagePath); 
    return 'data:'.$type.';base64,'.base64_encode(file_get_contents($imagePath)); 
} 

//Use the above function like below: 
echo '<img src="'.getDataURI('./images/my-file.svg').'" alt="">'; 
echo '<img src="'.getDataURI('./images/my-file.png').'" alt="">'; 

注:該文件的MIME類型將被自動添加(以幫助從這個PHP documentation )。

0

您還可以通過捲曲做到這一點,正是你需要的路徑爲圖像文件,並把它傳遞給下面給出的函數..

public static function getImageDataFromUrl($url) 
{ 
    $urlParts = pathinfo($url); 
    $extension = $urlParts['extension']; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    $response = curl_exec($ch); 
    curl_close($ch); 
    $base64 = 'data:image/' . $extension . ';base64,' . base64_encode($response); 
    return $base64; 

} 
6

萬一你(無論何種原因)無法請使用curl也不是file_get_contents,您可以解決:

$img = imagecreatefrompng('...'); 
ob_start(); 
imagepng($img); 
$bin = ob_get_clean(); 
$b64 = base64_encode($bin); 
相關問題