Chuyển đến nội dung
Có nhiều giải pháp để mã hoá hình ảnh, Encode Base64 là một trong số đó. Khi chuyển hình ảnh về dạng này ảnh được hiển thị trực tiếp từ Encode Base64 Image mà không qua URL ban đầu, điều này có nghĩa là sau khi bạn đã mã hoá thì bạn sẽ dùng code đã mã hoá này thay thế cho link ảnh ban đầu.
Cách mã hoá này có ưu điểm là giúp bạn tránh được việc link ảnh bị die, hiển thị hình ảnh nhanh hơn và không sợ bị lỗi liên quan đến https. Tuy nhiên, nó cũng có nhược điểm là khi bạn dùng để hiển thị ảnh code in ra sẽ dài và nặng hơn.
Một chút giới thiệu về cách mã hoá này thôi. Bây giờ ta tiến hành thực hiện, các bạn làm theo hướng dẫn bên dưới.
//C1: dùng trên host $path = "myfolder/myimage.png"; 'duong-dan-ảnh-trên-host $type = pathinfo($path, PATHINFO_EXTENSION); //C2: dùng từ URL get content về rồi mã hoá $path = "http(s)://mydomain.com/myimage.png"; 'duong-dan-ảnh-từ-web $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); //KQ => ảnh đã được encode sang base64Code trên hoạt động bằng cách lấy content và header (để xem type ảnh) của ảnh rồi sao đó đưa vào phần mã hoá trả về kết quả $base64.
DEMO:
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> ";Bài viết mã hoá ảnh đơn giản đến đây là hết rồi, cảm ơn các bạn đã đọc
Xem thêm: Chia sẻ API getlink Zing Mp3 2019
Xin lỗi vì đã trả lời muộn cho một câu hỏi đã được trả lời, nhưng tôi không nghĩ rằng base64_decode ($ x, true) là một giải pháp đủ tốt cho vấn đề này. Trên thực tế, có thể không có một giải pháp tốt nào phù hợp với bất kỳ đầu vào nhất định nào. Ví dụ: tôi có thể đặt nhiều giá trị xấu vào $ x và không nhận được giá trị trả về sai.
var_dump(base64_decode('wtf mate',true)); string(5) "���j�" var_dump(base64_decode('This is definitely not base64 encoded',true)); string(24) "N���^~)��r��[jǺ��ܡם"Tôi nghĩ rằng ngoài việc kiểm tra giá trị trả lại nghiêm ngặt, bạn cũng cần thực hiện xác thực sau giải mã. Cách đáng tin cậy nhất là nếu bạn có thể giải mã và sau đó kiểm tra một tập hợp các giá trị khả thi đã biết.
Một giải pháp chung hơn với độ chính xác dưới 100% (gần hơn với chuỗi dài hơn, không chính xác đối với chuỗi ngắn) là nếu bạn kiểm tra đầu ra của mình để xem liệu nhiều ký tự có nằm ngoài phạm vi thông thường của ký tự utf-8 (hoặc bất kỳ mã hóa nào bạn sử dụng) hay không.
Xem ví dụ này:
<?php $english = array(); foreach (str_split('[email protected]#$%^*()_+|}?><": Iñtërnâtiônàlizætiøn') as $char) { echo ord($char) . "\n"; $english[] = ord($char); } echo "Max value english = " . max($english) . "\n"; $nonsense = array(); echo "\n\nbase64:\n"; foreach (str_split(base64_decode('Not base64 encoded',true)) as $char) { echo ord($char) . "\n"; $nonsense[] = ord($char); } echo "Max nonsense = " . max($nonsense) . "\n"; ?>Các kết quả:
Max value english = 195 Max nonsense = 233Vì vậy, bạn có thể làm một cái gì đó như sau:
if ( $maxDecodedValue > 200 ) {} //decoded string is Garbage - original string not base64 encoded else {} //decoded string is useful - it was base64 encodedBạn có thể nên sử dụng giá trị trung bình () của các giá trị được giải mã thay vì giá trị max (), tôi chỉ sử dụng hàm max () trong ví dụ này vì đáng buồn là không có hàm ý nghĩa () tích hợp sẵn trong PHP. Thước đo bạn sử dụng (trung bình, tối đa, v.v.) so với ngưỡng nào (ví dụ: 200) phụ thuộc vào hồ sơ sử dụng ước tính của bạn.
Tóm lại, động thái chiến thắng duy nhất là không chơi. Tôi sẽ cố gắng tránh phải phân biệt base64 ngay từ đầu.
25 hữu ích 0 bình luận chia sẻ 0 bình luận chia sẻ
(PHP 4, PHP 5, PHP 7, PHP 8)
base64_decode — Decodes data encoded with MIME base64 — Decodes data encoded with MIME base64
Description
base64_decode(string $string, bool $strict = false): string|false(string $string, bool $strict = false): string|false
Parameters
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 0The encoded data.
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 1If the <?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 1 parameter is set to <?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 3 then the base64_decode() function will return false if the input contains character from outside the base64 alphabet. Otherwise invalid characters will be silently discarded. <?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 3 then the base64_decode() function will return false if the input contains character from outside the base64 alphabet. Otherwise invalid characters will be silently discarded.
Return Values
Returns the decoded data or false on failure. The returned data may be binary. false on failure. The returned data may be binary.
Examples
Example #1 base64_decode() examplebase64_decode() example
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 6
The above example will output:
This is an encoded stringwinkelnkemper at googlemail dot com ¶ ¶
11 years ago
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 7
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 8
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 9walf ¶ ¶
6 years ago
var_dump(base64_decode('wtf mate',true)); string(5) "���j�" var_dump(base64_decode('This is definitely not base64 encoded',true)); string(24) "N���^~)��r��[jǺ��ܡם" 0
var_dump(base64_decode('wtf mate',true)); string(5) "���j�" var_dump(base64_decode('This is definitely not base64 encoded',true)); string(24) "N���^~)��r��[jǺ��ܡם" 1
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 9martinstaemmler at gmx dot net ¶ ¶
13 years ago
var_dump(base64_decode('wtf mate',true)); string(5) "���j�" var_dump(base64_decode('This is definitely not base64 encoded',true)); string(24) "N���^~)��r��[jǺ��ܡם" 3
var_dump(base64_decode('wtf mate',true)); string(5) "���j�" var_dump(base64_decode('This is definitely not base64 encoded',true)); string(24) "N���^~)��r��[jǺ��ܡם" 4
var_dump(base64_decode('wtf mate',true)); string(5) "���j�" var_dump(base64_decode('This is definitely not base64 encoded',true)); string(24) "N���^~)��r��[jǺ��ܡם" 5
var_dump(base64_decode('wtf mate',true)); string(5) "���j�" var_dump(base64_decode('This is definitely not base64 encoded',true)); string(24) "N���^~)��r��[jǺ��ܡם" 6
var_dump(base64_decode('wtf mate',true)); string(5) "���j�" var_dump(base64_decode('This is definitely not base64 encoded',true)); string(24) "N���^~)��r��[jǺ��ܡם" 7
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 9Tom ¶ ¶
15 years ago
var_dump(base64_decode('wtf mate',true)); string(5) "���j�" var_dump(base64_decode('This is definitely not base64 encoded',true)); string(24) "N���^~)��r��[jǺ��ܡם" 9
<?php $english = array(); foreach (str_split('[email protected]#$%^*()_+|}?><": Iñtërnâtiônàlizætiøn') as $char) { echo ord($char) . "\n"; $english[] = ord($char); } echo "Max value english = " . max($english) . "\n"; $nonsense = array(); echo "\n\nbase64:\n"; foreach (str_split(base64_decode('Not base64 encoded',true)) as $char) { echo ord($char) . "\n"; $nonsense[] = ord($char); } echo "Max nonsense = " . max($nonsense) . "\n"; ?> 0
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 9Starson ¶ ¶
15 years ago
<?php $english = array(); foreach (str_split('[email protected]#$%^*()_+|}?><": Iñtërnâtiônàlizætiøn') as $char) { echo ord($char) . "\n"; $english[] = ord($char); } echo "Max value english = " . max($english) . "\n"; $nonsense = array(); echo "\n\nbase64:\n"; foreach (str_split(base64_decode('Not base64 encoded',true)) as $char) { echo ord($char) . "\n"; $nonsense[] = ord($char); } echo "Max nonsense = " . max($nonsense) . "\n"; ?> 2
<?php $english = array(); foreach (str_split('[email protected]#$%^*()_+|}?><": Iñtërnâtiônàlizætiøn') as $char) { echo ord($char) . "\n"; $english[] = ord($char); } echo "Max value english = " . max($english) . "\n"; $nonsense = array(); echo "\n\nbase64:\n"; foreach (str_split(base64_decode('Not base64 encoded',true)) as $char) { echo ord($char) . "\n"; $nonsense[] = ord($char); } echo "Max nonsense = " . max($nonsense) . "\n"; ?> 3
<?php $english = array(); foreach (str_split('[email protected]#$%^*()_+|}?><": Iñtërnâtiônàlizætiøn') as $char) { echo ord($char) . "\n"; $english[] = ord($char); } echo "Max value english = " . max($english) . "\n"; $nonsense = array(); echo "\n\nbase64:\n"; foreach (str_split(base64_decode('Not base64 encoded',true)) as $char) { echo ord($char) . "\n"; $nonsense[] = ord($char); } echo "Max nonsense = " . max($nonsense) . "\n"; ?> 4
<?php $english = array(); foreach (str_split('[email protected]#$%^*()_+|}?><": Iñtërnâtiônàlizætiøn') as $char) { echo ord($char) . "\n"; $english[] = ord($char); } echo "Max value english = " . max($english) . "\n"; $nonsense = array(); echo "\n\nbase64:\n"; foreach (str_split(base64_decode('Not base64 encoded',true)) as $char) { echo ord($char) . "\n"; $nonsense[] = ord($char); } echo "Max nonsense = " . max($nonsense) . "\n"; ?> 5
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 9Starson ¶ ¶
debug ¶
<?php $english = array(); foreach (str_split('[email protected]#$%^*()_+|}?><": Iñtërnâtiônàlizætiøn') as $char) { echo ord($char) . "\n"; $english[] = ord($char); } echo "Max value english = " . max($english) . "\n"; $nonsense = array(); echo "\n\nbase64:\n"; foreach (str_split(base64_decode('Not base64 encoded',true)) as $char) { echo ord($char) . "\n"; $nonsense[] = ord($char); } echo "Max nonsense = " . max($nonsense) . "\n"; ?> 7
<?php $english = array(); foreach (str_split('[email protected]#$%^*()_+|}?><": Iñtërnâtiônàlizætiøn') as $char) { echo ord($char) . "\n"; $english[] = ord($char); } echo "Max value english = " . max($english) . "\n"; $nonsense = array(); echo "\n\nbase64:\n"; foreach (str_split(base64_decode('Not base64 encoded',true)) as $char) { echo ord($char) . "\n"; $nonsense[] = ord($char); } echo "Max nonsense = " . max($nonsense) . "\n"; ?> 8
<?php $english = array(); foreach (str_split('[email protected]#$%^*()_+|}?><": Iñtërnâtiônàlizætiøn') as $char) { echo ord($char) . "\n"; $english[] = ord($char); } echo "Max value english = " . max($english) . "\n"; $nonsense = array(); echo "\n\nbase64:\n"; foreach (str_split(base64_decode('Not base64 encoded',true)) as $char) { echo ord($char) . "\n"; $nonsense[] = ord($char); } echo "Max nonsense = " . max($nonsense) . "\n"; ?> 9
Max value english = 195 Max nonsense = 233 0
Max value english = 195 Max nonsense = 233 1
Max value english = 195 Max nonsense = 233 2
Max value english = 195 Max nonsense = 233 3
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 914 years ago ¶
debug ¶
Max value english = 195 Max nonsense = 233 5
Max value english = 195 Max nonsense = 233 6
Max value english = 195 Max nonsense = 233 7
Max value english = 195 Max nonsense = 233 8
Max value english = 195 Max nonsense = 233 9
if ( $maxDecodedValue > 200 ) {} //decoded string is Garbage - original string not base64 encoded else {} //decoded string is useful - it was base64 encoded 0
if ( $maxDecodedValue > 200 ) {} //decoded string is Garbage - original string not base64 encoded else {} //decoded string is useful - it was base64 encoded 1
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 914 years ago ¶
twm at twmacinta dot com ¶
if ( $maxDecodedValue > 200 ) {} //decoded string is Garbage - original string not base64 encoded else {} //decoded string is useful - it was base64 encoded 3
if ( $maxDecodedValue > 200 ) {} //decoded string is Garbage - original string not base64 encoded else {} //decoded string is useful - it was base64 encoded 4
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 9tobias at silverxnet dot de ¶ ¶
18 years ago
if ( $maxDecodedValue > 200 ) {} //decoded string is Garbage - original string not base64 encoded else {} //decoded string is useful - it was base64 encoded 6
if ( $maxDecodedValue > 200 ) {} //decoded string is Garbage - original string not base64 encoded else {} //decoded string is useful - it was base64 encoded 7
if ( $maxDecodedValue > 200 ) {} //decoded string is Garbage - original string not base64 encoded else {} //decoded string is useful - it was base64 encoded 8
if ( $maxDecodedValue > 200 ) {} //decoded string is Garbage - original string not base64 encoded else {} //decoded string is useful - it was base64 encoded 9
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 9user at sfdsfd dot com ¶ ¶
11 years ago
This is an encoded string 1
This is an encoded string 2
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 9walf ¶ ¶
13 years ago
This is an encoded string 4
This is an encoded string 5
This is an encoded string 6
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 9var_dump(base64_decode('wtf mate',true)); string(5) "���j�" var_dump(base64_decode('This is definitely not base64 encoded',true)); string(24) "N���^~)��r��[jǺ��ܡם" 5 ¶
Tom ¶
This is an encoded string 8
This is an encoded string 9
$string0
$string115 years ago ¶
Starson ¶
$string2
$string3
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 9debug ¶ ¶
13 years ago
$string5
var_dump(base64_decode('wtf mate',true)); string(5) "���j�" var_dump(base64_decode('This is definitely not base64 encoded',true)); string(24) "N���^~)��r��[jǺ��ܡם" 5 ¶
debug ¶
$string6
$string7
$string8
$string914 years ago ¶
13 years ago
$strict0
$strict1
$strict2
$strict3var_dump(base64_decode('wtf mate',true)); string(5) "���j�" var_dump(base64_decode('This is definitely not base64 encoded',true)); string(24) "N���^~)��r��[jǺ��ܡם" 5 ¶
6 years ago
$strict4
$strict5
<?php $path = "//i.imgur.com/uTWm5ez.jpg"; $headers = get_headers($path, 1); $type = $headers["Content-Type"]; $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src='".$base64."'/> "; 9