苏州市民卡卡APP上买券为什么交易失败

PHP如何将图片转换成base64编码的实现代码分享 - 维维软件园
PHP如何将图片转换成base64编码的实现代码分享
来源:维维整理作者:维维时间: 7:49:07(0)
今天要给各位PHP爱好者带来的是PHP如何将图片转换成base64编码的实现代码,相信各位朋友都知道Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一了,假如对此不清楚的朋友可查看RFC2045~RFC2049,上面有MIME的详细规范。本文主要给大家介绍下PHP将图片转换为base64编码格式的实现方法,有兴趣的朋友来详细了解下吧。
为何要对图片base64编码:
base64是当前网络上最为常见的传输8Bit字节代码的编码方式之一。base64主要不是加密,它主要的作用是将某些二进制数转成普通字符用于网络传输。因为这些二进制字符在传输协议中属于控制字符,无法直接传送,因此需要转换一下。尽管图片可以直接传输,不过我们也能够把它变成字符串直接放在源码里,而不需要浏览器在读取到源码以后再从服务器上下载。
怎么使用PHP对图片进行base64解码输出:
$img = 'test.jpg';
$base64_img = base64EncodeImage($img);
echo '&img src=&' . $base64_img . '& /&';
function base64EncodeImage ($image_file) {
& $base64_image = '';
& $image_info = getimagesize($image_file);
& $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
& $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
& return $base64_
通过上述方法转换之后得到的base64编码字符串,可存放到数据库中,需要的时候可直接从数据库中读取,减少访问图片时的请求数量。该方法已包含进MiniFramework的全局函数库中了。
大家还看了:
[访问统计:]
上一篇:下一篇:& 对使用 MIME base64 编码的数据进行解码
PHP base64_decode 对使用 MIME base64 编码的数据进行解码
base64_decode
base64_decode & 对使用 MIME base64 编码的数据进行解码
string base64_decode
( string $encoded_data
Example #1 base64_decode() 示例
&?php$str&=&'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';echo&base64_decode($str);?&
此示例将显示:
This is an encoded string
的 6.8 章节。
PHP base64_decode note #1
base64_decode seems to fail when decoding big files/strings. I had an issue decoding a 7MB image file. Here is a solution that worked for me:
$decodedstring=base64_decode(chunk_split($encodedstring));
PHP base64_decode note #2
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:
& $encodedData = str_replace(' ','+',$encodedData);
& $decocedData = base64_decode($encodedData);
merci..........
PHP base64_decode note #3
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:
& $encodedData = str_replace(' ','+',$encodedData);
& $decocedData = base64_decode($encodedData);
PHP base64_decode note #4
I had some trouble trying to let base64_decode decode base64-strings longer than ~5k chars.
The base64-decoding function is a homomorphism between modulo 4 and modulo 3-length segmented strings. That motivates a divide and conquer approach: Split the encoded string into substrings counting modulo 4 chars, then decode each substring and concatenate all of them.
Then instead of
&?php $decoded = base64_decode($encoded); ?&
for big $encoded strings, it's saver to use
$decoded = "";
for ($i=0; $i & ceil(strlen($encoded)/256); $i++)
&& $decoded = $decoded . base64_decode(substr($encoded,$i*256,256));
where 256 can be replaced by a sufficiently small modulo 4 natural.
PHP base64_decode note #5
Here is function to decode Base 62 (see ) string to number. It is used by MTA in message id, e.g. by Exim
function base62_decode($str) {
&&& $ret= 0;
&&& for ($i= 0, $l= strlen($str); $i & $l; $i++) {
&& & && $val= ord($str[$i]);
&& & && if (ctype_digit($str[$i]))
&& & & & && $val-= ord('0');
&& & && else if (ctype_upper($str[$i]))
&& & & & && $val-= ord('A') - 10;
&& & && else if (ctype_lower($str[$i]))
&& & & & && $val-= ord('a') - 36;
&& & && else
&& & & & && $val= 0;
&& & && $ret= $ret * 62 + $val;
&&& return $ret;
PHP base64_decode note #6
I had a problem testing whether an imap message body was base64 encoded on a pre 5.2.* server.& I had been using this function on a post 5.2 server.
I found that the function imap_base64() returns FALSE on failing to decode a string, and that I could use that to check instead.
if(imap_base64($body)) $body = imap_base64($body);
PHP base64_decode note #7
You can do partial decoding (e.g. from buffered input streams) if you choose a chunk length that is multiple of 4:
$encoded = base64_encode('The quick brown fox jumps over the lazy dog');
for($i=0, $len=strlen($encoded); $i&$len; $i+=4){
&&& echo base64_decode( substr($encoded, $i, 4) );
4 encoded chars represent 3 original chars. The "=" character is used as padding.
PHP base64_decode note #8
To follow up on Starson's post, PHP was changed to no longer treat a space as if it were a plus sign in CVS revision 1.43.2.1, which corresponds to PHP 5.1.0.& You can see what happened with a diff to branch point 1.43 at:
The CVS log indicates that this change was made to fix bug #34214 (base64_decode() does not properly ignore whitespace).
It would seem from the comment preceding the code which was removed that the treatment of the space as if it were the plus sign was actually intentional at one time:
&&& When Base64 gets POSTed, all pluses are interpreted as spaces.
&&& This line changes them back.& It's not exactly the Base64 spec,
&&& but it is completely compatible with it (the spec says that spaces
&&& are invalid).& This will also save many people considerable
&&& headache.
&&& if (ch == ' ') ch = '+';
However, RFC 3548 states that characters not in the Base64 alphabet should either be ignored or cause the implementation to reject the encoding and RFC 2045 says they should be ignored.& So the original code was unfortunately not fully compatible with the spec or other implementations.& It may have also masked problems with code not properly escaping POST variables.
PHP base64_decode note #9
@morgangalpin att gmail dotty com
A better implementation would be the following regular expression:
^[a-zA-Z0-9/+]*={0,2}$
Which will also detect the usage of = or == at the end of the string (and only end).
If this regex isn't following proper RFC guidelines, please comment on it.
A function geared specifically toward this:
function is_base64_encoded()
&& & && if (preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $data)) {
&& & & & && return TRUE;
&& & && } else {
&& & & & && return FALSE;
is_base64_encoded("iash21iawhdj98UH3"); is_base64_encoded("#iu3498r"); is_base64_encoded("asiudfh9w=8uihf"); is_base64_encoded("a398UIhnj43f/1!+sadfh3w84hduihhjw=="); ?&
PHP base64_decode note #10
Here is a drop-in replacement for base64_decode(), based on a faster version of morgangalpin's code:
function& & base64_decode_fix( $data, $strict = false )
&&& if( $strict )
&& & && if( preg_match( '![^a-zA-Z0-9/+=]!', $data ) )
&& & & & && return( false );
&&& return( base64_decode( $data ) );
PHP base64_decode note #11
I was having trouble with base64_decode returning false if the data to be decoded wasn't actually encoded. It turns out that it is a bug that exists in PHP version 5.1.2, which I'm using, but it has been fixed in CVS. The relevant bug is:
"base64_decode violates RFC 3548". The fix may become available in 5.2.4 or 6 or whatever is coming next.
Since I'm not able to upgrade PHP to the latest version, I needed a way to check if some data had actually been encoded before trying to decode it. Here is the function I' I hope it helps someone.
& function checkBase64Encoded($encodedString) {
&&& $length = strlen($encodedString);
&&& for ($i = 0; $i & $length; ++$i) {
&& && $c = $encodedString[$i];
&& && if (
&& & && ($c & '0' || $c & '9')
&& & && && ($c & 'a' || $c & 'z')
&& & && && ($c & 'A' || $c & 'Z')
&& & && && ($c != '+')
&& & && && ($c != '/')
&& & && && ($c != '=')
&& & && return false;
&&& return true;
PHP base64_decode note #12
This function supports "base64url" as described in Section 5 of RFC 4648, "Base 64 Encoding with URL and Filename Safe Alphabet"
&&& function base64url_decode($base64url)
&& & && $base64 = strtr($base64url, '-_', '+/');
&& & && $plainText = base64_decode($base64);
&& & && return ($plainText);
PHP base64_decode note #13
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 &= 5.0.5's base64_decode( $string ) will assume that a space is meant to be a + sign where php &= 5.1.0's base64_decode( $string ) will no longer make that assumption.& I did not see this noted in the change log.
Please note that, as of this writing, mb_convert_encoding( $string, "UTF-8", "BASE64" ) still behaves as base64_decode( $string ) did in php &= 5.0.5 regardless of the version of php you are running.
PHP base64_decode note #14
I've come across an interesting issue with an external program that submits a gzcompressed base64_encoded string to PHP via POST. The external program encodes the string using the occasional " " (space) character, however if I encode the same original string within PHP (using base64_encode), it uses a "+" (plus) character wherever the external program would use a space. On my deployed machine, running PHP 4.3.9, base64_decode is fine with the " " (space) characters, but my test server running 5.1.4 is not. It took me a while to figure out that was the issue, but I ended up fixing it with a simple:
$post_data = str_replace(" ","+",$_POST['string'])
This fix works on both the 4.3.9 and 5.1.4 machines. I am sure that the external program is probably not conforming to the standard, and it isn't a PHP problem per- but incase anybody else ever runs into that.
PHP base64_decode note #15
The user notes posted here helped me a lot in writing the PHP code to upload uuencoded files to a server using $_POST. Hardest thing to figure out was why the files came out scrambled and corrupted. After comparing the original file with the file reconstructed by the uudecode script, I found out that a simple "stripcslashes" on the posted data will do the trick.
So, to upload any kind of uuencoded file using a POST:
1. send the raw file data to the PHP script
2. $uuencoded_data = stripcslashes($_POST['filedata']);
3. strip the marker lines from $uuencoded_data (first line, last line and second last line of the data. Each line is seperated by a LF (chr(10)) character.)
4. $decoded_data = uudecode($stripped_uuencoded_data); (this function can be found in the user notes here).
5. Use the script provided in one of the user notes on this page to write the decoded data to a binary file.
That should do the trick!
PHP base64_decode note #16
I was wondering how to decode attached images within mails. Basically they are mostly JPEG files, so it was obviously to write a function that decodes JPEG images.
I guess the plainest way to do so was the following:
function base64_to_jpeg( $inputfile, $outputfile ) {
& $ifp = fopen( $inputfile, "rb" );
& $imageData = fread( $ifp, filesize( $inputfile ) );
& fclose( $ifp );
& $ifp = fopen( $outputfile, "wb" );
& fwrite( $ifp, base64_decode( $imageData ) );
& fclose( $ifp );
& return( $outputfile );
This function decodes the given inputfile (a filename!) and saves it to the given outputfile (a filename as well) and then returns the output filename for further usage (e.g. redirect, imagejpeg() and so on).
I thought that might be helpful.
PHP base64_decode note #17
this script can correct the bug
$enc = chunk_split(preg_replace('! 15 12| 15| 12!','',$enc));
$enc = base64_decode($enc);
PHP base64_decode note #18
I used to do uudecode as a C module, but I've discovered a really fast way to do it in PHP. Here it is:
function uudecode($encode) {
& $b64chars="ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz+/";
& $encode = preg_replace("/^./m","",$encode);
& $encode = preg_replace("/
/m","",$encode);
& for($i=0; $i&strlen($encode); $i++) {
&&& if ($encode[$i] == '`')
&& && $encode[$i] = ' ';
&&& $encode[$i] = $b64chars[ord($encode[$i])-32];
& while(strlen($encode) % 4)
&&& $encode .= "=";
& return base64_decode($encode);
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.
PHPURLs - 函数PHP与JavaScript之间中文base64码转换代码_最火下载站
您的位置: >
> PHP与JavaScript之间中文base64码转换代码
PHP与JavaScript之间中文base64码转换代码
//PHP base64_encode &?php function str_encode($str){ return base64_encode(iconv('GB18030','UTF-8',$str)); } echo str_encode('你好!美女!'); ?& ////////////////////////////////////////////////////////////////////////////////////// 另附PHP的base64的加解密函数! {echo base64_encode($str).&&br /&&; //加密 $ss='Z25pZD0yNSZncGljaT0x'; echo base64_decode($ss); //解密 ///////////////////////////////////////////////////////////////////////////////////// //JavaScript base64_decode &script language='javascript'& /* utf.js - UTF-8 &=& UTF-16 convertion * * Copyright (C) 1999 Masanao Izumo &iz@onicos.co.jp& * Version: 1.0 * LastModified: Dec 25 1999 * This library is free. You can redistribute it and/or modify it. */ /* * Interfaces: * utf8 = utf16to8(utf16); * utf16 = utf16to8(utf8); */ function utf16to8(str) { var out, i, len, out = &&; len = str. for(i = 0; i & i++) { c = str.charCodeAt(i); if ((c &= 0x0001) && (c &= 0x007F)) { out += str.charAt(i); } else if (c & 0x07FF) { out += String.fromCharCode(0xE0 | ((c && 12) & 0x0F)); out += String.fromCharCode(0x80 | ((c && 6) & 0x3F)); out += String.fromCharCode(0x80 | ((c && 0) & 0x3F)); } else { out += String.fromCharCode(0xC0 | ((c && 6) & 0x1F)); out += String.fromCharCode(0x80 | ((c && 0) & 0x3F)); } }
} function utf8to16(str) { var out, i, len, var char2, char3; out = &&; len = str. i = 0; while(i & len) { c = str.charCodeAt(i++); switch(c && 4) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: // 0xxxxxxx out += str.charAt(i-1);
case 12: case 13: // 110x xxxx 10xx xxxx char2 = str.charCodeAt(i++); out += String.fromCharCode(((c & 0x1F) && 6) | (char2 & 0x3F));
case 14: // 1110 xxxx 10xx xxxx 10xx xxxx char2 = str.charCodeAt(i++); char3 = str.charCodeAt(i++); out += String.fromCharCode(((c & 0x0F) && 12) | ((char2 & 0x3F) && 6) | ((char3 & 0x3F) && 0));
} /* Copyright (C) 1999 Masanao Izumo &iz@onicos.co.jp& * Version: 1.0 * LastModified: Dec 25 1999 * This library is free. You can redistribute it and/or modify it. */ /* * Interfaces: * b64 = base64encode(data); * data = base64decode(b64); */ var base64EncodeChars = &ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/&; var base64DecodeChars = new Array( -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1); function base64encode(str) { var out, i, var c1, c2, c3; len = str. i = 0; out = &&; while(i & len) { c1 = str.charCodeAt(i++) & 0 if(i == len) { out += base64EncodeChars.charAt(c1 && 2); out += base64EncodeChars.charAt((c1 & 0x3) && 4); out += &==&;
} c2 = str.charCodeAt(i++); if(i == len) { out += base64EncodeChars.charAt(c1 && 2); out += base64EncodeChars.charAt(((c1 & 0x3)&& 4) | ((c2 & 0xF0) && 4)); out += base64EncodeChars.charAt((c2 & 0xF) && 2); out += &=&;
} c3 = str.charCodeAt(i++); out += base64EncodeChars.charAt(c1 && 2); out += base64EncodeChars.charAt(((c1 & 0x3)&& 4) | ((c2 & 0xF0) && 4)); out += base64EncodeChars.charAt(((c2 & 0xF) && 2) | ((c3 & 0xC0) &&6)); out += base64EncodeChars.charAt(c3 & 0x3F); }
} function base64decode(str) { var c1, c2, c3, c4; var i, len, len = str. i = 0; out = &&; while(i & len) { /* c1 */ do { c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff]; } while(i & len && c1 == -1); if(c1 == -1)
/* c2 */ do { c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff]; } while(i & len && c2 == -1); if(c2 == -1)
out += String.fromCharCode((c1 && 2) | ((c2 & 0x30) && 4)); /* c3 */ do { c3 = str.charCodeAt(i++) & 0 if(c3 == 61)
c3 = base64DecodeChars[c3]; } while(i & len && c3 == -1); if(c3 == -1)
out += String.fromCharCode(((c2 & 0XF) && 4) | ((c3 & 0x3C) && 2)); /* c4 */ do { c4 = str.charCodeAt(i++) & 0 if(c4 == 61)
c4 = base64DecodeChars[c4]; } while(i & len && c4 == -1); if(c4 == -1)
out += String.fromCharCode(((c3 & 0x03) && 6) | c4); }
} //input base64 encode function strdecode(str){ return utf8to16(base64decode(str)); } document.write(strdecode('5L2g5aW9Iee+juWlsyE=')); &/script&
上一篇: 下一篇:在线任意图片转Base64编码工具 - aTool在线工具
支持文件大小不限,支持 PNG、GIF、JPG、BMP、ICO 格式。建议在转码前使用本站图片压缩工具进行压缩。
【图片处理推荐:】
选择要转换成Base64的图片
还原生成的Base64编码为图片:
base64编码介绍 | Base64 Encode
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,Base64编码可用于在HTTP环境下传递较长的标识信息。采用Base64编码具有不可读性,即所编码的数据不会被人用肉眼所直接看到。
在MIME格式的电子邮件中,base64可以用来将binary的字节序列数据编码成ASCII字符序列构成的文本。使用时,在传输编码方式中指定base64。使用的字符包括大小写字母各26个,加上10个数字,和加号“+”,斜杠“/”,一共64个字符,等号“=”用来作为后缀用途。
Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之后在6位的前面补两个0,形成8位一个字节的形式。 如果剩下的字符不足3个字节,则用0填充,输出字符使用'=',因此编码后输出的文本末尾可能会出现1或2个'='。
为了保证所输出的编码位可读字符,Base64制定了一个编码表,以便进行统一转换。编码表的大小为2^6=64,这也是Base64名称的由来。
Base64编码表可以网络上搜索学习一下!
base64图片工具介绍 | Base64 Encode/Decode Tool
支持 PNG、GIF、JPG、BMP、ICO 格式。
将图片转换为Base64编码,可以让你很方便地在没有上传文件的条件下将图片插入其它的网页、编辑器中。 这对于一些小的图片是极为方便的,因为你不需要再去寻找一个保存图片的地方。
假定生成的代码为“data:image/base64, .....”,那么你只需要全部复制,然后在插入图片的时候,地址填写这段代码即可。
CSS中使用:background-image: url("data:image/base64,iVBORw0KGgo=...");
HTML中使用:&img src="data:image/base64,iVBORw0KGgo=..." /&
图片转换Base64,无线开发、HTML5、CSS3必备的工具,CSS DataURI Base64 工具。
将图片转换成base64编码的,在web网上一般用于小图片上,不仅可以减少图片的请求数量(集合到js、css代码中),还可以防止因为一些相对路径等问题导致图片404错误。
推荐功能 / 猜你喜欢 | Suggest
评论 | Comments
公众号: atool-org

我要回帖

更多关于 杭州市民卡 的文章

 

随机推荐