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 base64 Code 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 = "https://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.
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:
Các kết quả:
Vì vậy, bạn có thể làm một cái gì đó như sau:
Bạ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 Descriptionbase64_decode(string Parameters<?php $path = "https://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."'/> ";0 The encoded data. <?php $path = "https://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 If the <?php $path = "https://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 = "https://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 = "https://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 ExamplesExample #1 base64_decode() examplebase64_decode() example <?php $path = "https://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 string winkelnkemper at googlemail dot com ¶ ¶ 11 years ago <?php $path = "https://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 = "https://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 = "https://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 walf ¶ ¶ 6 years ago 0 1<?php $path = "https://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 martinstaemmler at gmx dot net ¶ ¶ 13 years ago 3 4 5 6 7<?php $path = "https://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 Tom ¶ ¶ 15 years ago 9 0<?php $path = "https://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 Starson ¶ ¶ 15 years ago 2 3 4 5<?php $path = "https://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 Starson ¶ ¶ debug ¶ 7 8 9 0 1 2 3<?php $path = "https://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 14 years ago ¶ debug ¶ 5 6 7 8 9 0 1<?php $path = "https://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 14 years ago ¶ twm at twmacinta dot com ¶ 3 4<?php $path = "https://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 tobias at silverxnet dot de ¶ ¶ 18 years ago 6 7 8 9<?php $path = "https://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 user at sfdsfd dot com ¶ ¶ 11 years ago This is an encoded string1 This is an encoded string2 <?php $path = "https://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 walf ¶ ¶ 13 years ago This is an encoded string4 This is an encoded string5 This is an encoded string6 <?php $path = "https://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 Tom ¶ This is an encoded string8 This is an encoded string9
$string 115 years ago ¶ Starson ¶
<?php $path = "https://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 debug ¶ ¶ 13 years ago
debug ¶
$string 914 years ago ¶ 13 years ago
$strict 3 6 years ago
<?php $path = "https://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 |