你能指導我如何將圖像從URL轉換爲base64編碼?如何將圖像轉換爲base64編碼?
回答
我認爲它應該是:
$path = 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
這取決於你需要的圖像。如果它是一個內聯網址,這是要走的路。儘管如此,它與單純的base64編碼並不相同。 – 2013-07-31 13:21:49
簡單:
$imagedata = file_get_contents("/path/to/image.jpg");
// alternatively specify an URL, if PHP settings allow
$base64 = base64_encode($imagedata);
記住,這將由33%擴大的數據,你就會有文件的大小超過您memory_limit
問題。
+1的數據放大 – 2013-07-31 12:59:07
也使用這種方式來表示使用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.'">';
如果找不到函數mime_content_type,只需從http://vpn.php.net/manual/en/function.mime-content-type.php中加入函數 – 2013-12-10 20:00:48
@Raju Rajotia Jangid cool +1 – pregmatch 2014-04-04 14:50:23
<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
感謝您將它壓縮爲儘可能小,並且您包含了img標記用例。最好的答案是肯定的。 – BarryMode 2016-12-22 16:16:36
這裏是上傳編碼,並將其保存到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>";
這不是要求。 – AlexioVay 2017-02-03 05:38:25
這是一個使用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);?>">
爲什麼它更好? – ruuter 2016-02-13 22:12:40
非常簡單,將常用的有:
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 )。
您還可以通過捲曲做到這一點,正是你需要的路徑爲圖像文件,並把它傳遞給下面給出的函數..
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;
}
萬一你(無論何種原因)無法請使用curl
也不是file_get_contents
,您可以解決:
$img = imagecreatefrompng('...');
ob_start();
imagepng($img);
$bin = ob_get_clean();
$b64 = base64_encode($bin);
- 1. 如何將base64編碼數據轉換爲xcode圖像
- 2. 如何將圖像(BYTE *)轉換爲Base64
- 3. 如何將Uri圖像轉換爲Base64?
- 4. 如何將base64 svg圖像轉換爲base64圖像png
- 5. 如何通過使用Base64編碼將ImageView圖像轉換爲字節碼?
- 6. 將Php轉換爲Python,base64編碼
- 7. 如何將jpeg圖像轉換爲base64編碼的二進制值?
- 8. 在Python中將base64轉換爲圖像
- 9. 在GDK中將圖像轉換爲base64
- 10. 將圖像數組轉換爲Base64
- 11. 將本地圖像轉換爲base64 javascript
- 12. Java - 將圖像轉換爲Base64
- 13. 將Base64編碼圖像加密爲另一個有效的Base64編碼圖像
- 14. 將圖像編碼爲Amazon S3的Base64
- 15. iPhone/iPad的Base64圖像編碼 - 轉換爲UIImage
- 16. Base64編碼圖像
- 17. Base64編碼圖像
- 18. 如何將java.sql.blob轉換爲java中的base64編碼字符串
- 19. 如何使用ColdFusion將BASE64編碼的HTML轉換爲GIF?
- 20. 如何將在response.outputStream中生成的PDF轉換爲Base64編碼
- 21. 如何將使用AES256編碼的NSData轉換爲base64?
- 22. 使用base64代碼將jquery廣告圖像轉換爲div
- 23. 如何將圖像URL轉換的base64圖像源usign的JavaScript
- 24. 如何將圖像轉換爲Base64字符串?
- 25. 如何將Base64圖像轉換爲文件類型
- 26. 如何將Base64字符串轉換爲Png圖像Mvc5 C#?
- 27. 如何將base64字符串圖像轉換爲android中的blob?
- 28. 如何將圖像轉換爲android中的base64二進制值?
- 29. 如何將圖像轉換爲xamarin.android中的base64?
- 30. 如何將base64字符串轉換爲PIL圖像對象
您想要做什麼? – 2010-10-19 10:46:34
將它嵌入Img標籤的src屬性 – Volatil3 2010-10-20 03:31:42
這是一箇舊線程,但爲了完整起見: 對於在css中使用,編碼圖像,特別是小圖像(小於1k)是很有意義的。這樣可以節省一個請求,這會花費更長的時間,甚至可能會更大。 – arminrosu 2012-10-31 12:46:23