Hướng dẫn dùng base64encode php trong PHP

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:

Các kết quả:

Max value english = 195
Max nonsense = 233

Vì 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 encoded

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ẻ

[PHP 4, PHP 5, PHP 7, PHP 8]

base64_decodeDecodes data encoded with MIME base64

Description

base64_decode[string $string, bool $strict = false]: string|false

Parameters

string

The encoded data.

strict

If the strict parameter is set to true 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.

Examples

Example #1 base64_decode[] example

The above example will output:

This is an encoded string

winkelnkemper at googlemail dot com

11 years ago

If you want to save data that is derived from a Javascript canvas.toDataURL[] function, you have to convert blanks into plusses. If you do not do that, the decoded data is corrupted:

walf

6 years ago

Base64 for URL parameters/filenames, that adhere to RFC 4648.
Defaults to dropping the padding on encode since it's not required for decoding, and keeps the URL free of % encodings.



for big $encoded strings, it's saver to use



where 256 can be replaced by a sufficiently small modulo 4 natural.

Tom

15 years ago

This function supports "base64url" as described in Section 5 of RFC 4648, "Base 64 Encoding with URL and Filename Safe Alphabet"

   

Starson

15 years ago

To expand on Jes' post:

The change took place between 5.0.5 and 5.1.0.  Exactly where I don't know or care.

In short php

This is the PHP equivalent to perl's unpack["u",___]. That is, you need to strip the 'begin' and 'end' lines from the typical uuencoded file.

Klaus Fehrenbacher

19 years ago

this script can correct the bug

dimagolov at yahoo dot com

13 years ago

Here is function to decode Base 62 [see //en.wikipedia.org/wiki/Base_62] string to number. It is used by MTA in message id, e.g. by Exim

zmorris at zsculpt dot com

14 years ago

Here is a drop-in replacement for base64_decode[], based on a faster version of morgangalpin's code:

alvaro at demogracia dot com

13 years ago

You can do partial decoding [e.g. from buffered input streams] if you choose a chunk length that is multiple of 4:



4 encoded chars represent 3 original chars. The "=" character is used as padding.

markandrewslade at gmail dot com

6 years ago

The docs don't make this explicitly clear, but if you omit `$strict` or set it to `false` then invalid characters in the encoded input will be silently ignored.

Chủ Đề