本来以为不能转,因为原作者在文章头部写:未经许可不得转载,但又在文末留下了,转载请保留地址。因此,先保留地址转载,再申请一下授权,原文地址是: http://blog.csdn.net/cuixiping/article/details/45932793
之所以转这个,是因为最近在处理上传的时候遇到了问题,原来xhr可以直接将file对象提交,如果用fileReader,出来的其实是base64的string,这时候怎么办是个问题。
好了不说废话,上内容。。。。
canvas转换为dataURL (从canvas获取dataURL)
XML/HTML代码
- var dataurl = canvas.toDataURL('image/png');
- var dataurl2 = canvas.toDataURL('image/jpeg', 0.8);
File对象转换为dataURL、Blob对象转换为dataURL
File对象也是一个Blob对象,二者的处理相同。
XML/HTML代码
- function readBlobAsDataURL(blob, callback) {
- var a = new FileReader();
- a.onload = function(e) {callback(e.target.result);};
- a.readAsDataURL(blob);
- }
- //example:
- readBlobAsDataURL(blob, function (dataurl){
- console.log(dataurl);
- });
- readBlobAsDataURL(file, function (dataurl){
- console.log(dataurl);
- });
dataURL转换为Blob对象
XML/HTML代码
- function dataURLtoBlob(dataurl) {
- var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
- bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
- while(n--){
- u8arr[n] = bstr.charCodeAt(n);
- }
- return new Blob([u8arr], {type:mime});
- }
- //test:
- var blob = dataURLtoBlob('data:text/plain;base64,YWFhYWFhYQ==');
dataURL图片数据绘制到canvas
先构造Image对象,src为dataURL,图片onload之后绘制到canvas
XML/HTML代码
- var img = new Image();
- img.onload = function(){
- canvas.drawImage(img);
- };
- img.src = dataurl;
File,Blob的图片文件数据绘制到canvas
还是先转换成一个url,然后构造Image对象,src为dataURL,图片onload之后绘制到canvas
利用上面的 readBlobAsDataURL 函数,由File,Blob对象得到dataURL格式的url,再参考 dataURL图片数据绘制到canvas
XML/HTML代码
- readBlobAsDataURL(file, function (dataurl){
- var img = new Image();
- img.onload = function(){
- canvas.drawImage(img);
- };
- img.src = dataurl;
- });
不同的方法用于构造不同类型的url (分别是 dataURL, objectURL(blobURL), filesystemURL)。这里不一一介绍,仅以dataURL为例。
filesystemURL不是指本地文件URL的形式(file:///….), 而是格式类似于 filesystem:http://... 的一种URL,支持沙盒文件系统的浏览器支持(目前仅Chrome)支持。
Canvas转换为Blob对象并使用Ajax发送
转换为Blob对象后,可以使用Ajax上传图像文件。
先从canvas获取dataurl, 再将dataurl转换为Blob对象
XML/HTML代码
- var dataurl = canvas.toDataURL('image/png');
- var blob = dataURLtoBlob(dataurl);
- //使用ajax发送
- var fd = new FormData();
- fd.append("image", blob, "image.png");
- var xhr = new XMLHttpRequest();
- xhr.open('POST', '/server', true);
- xhr.send(fd);
EOF--
整个世界清静 了
苹果下面安装php5.4还算是比较方便的,主要是默认为php5.3,想用trait的时候,没5.4跑不了啊。。
于是直接使用brew install php54,结果报错,查了下资料,原来,需要:
- brew tap josegonzalez/homebrew-php
- brew tap homebrew/dupes
- brew install php54 --with-mysql --with-intl --with-fpm --without-apache
因为有装nginx。所以就without-apache了。。。
安装好后,brew 会告诉你:
XML/HTML代码
- The php.ini file can be found in:
- /usr/local/etc/php/5.4/php.ini
-
- ✩✩✩✩ PEAR ✩✩✩✩
-
- If PEAR complains about permissions, 'fix' the default PEAR permissions and config:
- chmod -R ug+w /usr/local/Cellar/php54/5.4.13/lib/php
- pear config-set php_ini /usr/local/etc/php/5.4/php.ini
-
- ✩✩✩✩ Extensions ✩✩✩✩
-
- If you are having issues with custom extension compiling, ensure that this php is
- in your PATH:
- PATH="$(brew --prefix josegonzalez/php/php54)/bin:$PATH"
-
- PHP54 Extensions will always be compiled against this PHP. Please install them
- using --without-homebrew-php to enable compiling against system PHP.
-
- ✩✩✩✩✩ INTL Support ✩✩✩✩✩
-
- icu4c is broken as of mxcl/homebrew#03ed757c, so you will need to install intl as
- a separate extension:
-
- brew install php54-intl
-
- ✩✩✩✩ FPM ✩✩✩✩
-
- To launch php-fpm on startup:
- * If this is your first install:
- mkdir -p ~/Library/LaunchAgents
- cp /usr/local/Cellar/php54/5.4.13/homebrew-php.josegonzalez.php54.plist ~/Library/LaunchAgents/
- launchctl load -w ~/Library/LaunchAgents/homebrew-php.josegonzalez.php54.plist
-
- * If this is an upgrade and you already have the homebrew-php.josegonzalez.php54.plist loaded:
- launchctl unload -w ~/Library/LaunchAgents/homebrew-php.josegonzalez.php54.plist
- cp /usr/local/Cellar/php54/5.4.13/homebrew-php.josegonzalez.php54.plist ~/Library/LaunchAgents/
- launchctl load -w ~/Library/LaunchAgents/homebrew-php.josegonzalez.php54.plist
-
- The control script is located at /usr/local/Cellar/php54/5.4.13/sbin/php54-fpm
-
- Mountain Lion comes with php-fpm pre-installed, to ensure you are using the brew version you need to make sure /sbin is before /usr/sbin in your PATH:
-
- PATH="/sbin:$PATH"
-
- You may also need to edit the plist to use the correct "UserName".
-
- Please note that the plist was called 'org.php-fpm.plist' in old versions
- of this formula.
-
- To have launchd start php54 at login:
- ln -sfv /usr/local/opt/php54/*.plist ~/Library/LaunchAgents
- Then to load php54 now:
- launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php54.plist
- Warning: /usr/local/sbin is not in your PATH
- You can amend this by altering your ~/.bashrc file
记下这些路径和使用方法,以后会用得到
先继续昨天的话题,昨天说到type xx struct的时候,如果使用map,需要注意的事项,就是最后的逗号不少能少,例如:
var m = map[string]Vertex{
"aa":Vertex{1,2,},
"bb":Vertex{3,4,},
}
其实还是有简化写法。当然这是基于m["aa"]的类型也是Vertex的情况:
var m = map[string]Vertex{
"Bell Labs": {40.68433, -74.39967},
"Google": {37.42202, -122.08408},
}
这个时候。看到没,-74后面的逗号可以省掉了。但最后的逗号还是省不掉。。。切记
-------
读取、修改、删除map很简单,就象PHP的数组一样。不过删除不太一样。。
读取:s = m["test"];
修改:m["test"] = 123;
删除:delete(m,"test");
判断key是否在map中。PHP是isset($m[$key]),而go是:
value,ret = m["test"],看上去是不是和读取很象?嗯,不过要看ret返回什么,如果是false,则value返回0,否则才是真正的m["test"]的值。。
需要注意一下
-------
对于go的数组,可以用len()函数来获取它的长度。
顺便说一下,go的内置函数很容易分别,内置函数首字符都是小写,否则,import包的话。首字符都是大写,如:fmt.Println之类的
如果熟悉python,则会发现go的数组读取和python很象。当然。。。有一点不象的是go的数组的key不允许是负数。否则会报错。。而python的负数则是从相反的方向读数据,因此python的数组反转很容易。。