07 09,2014

swfupload上传失败返回RESIZE_ERROR

上传用的是swfupload插件,今天有几张照片死活传不上去,调试发现返回的errorCode为-300。

找到-300在SWFUpload.UPLOAD_ERROR中的RESIZE,Ps:很奇怪,网上很少看到有-300的错误说明,倒是有ERROR_RESIZE这个变量

RESIZE_ERROR - 当调整图像大小时出现了某错误啊

看到这个提示一头雾水,图片本身是没问题的。用PHP 的GD库是可以裁剪的。

后来将message信息打印出来发现如下:Error generating resized image. Resizing: Error #2015

google 搜索发现这个是flash player的问题:

In AIR 1.5 and Flash Player 10, the maximum size for 
a BitmapData object is 8,191 pixels in width or height, 
and the total number of pixels cannot exceed 16,777,215 pixels. 
(So, if a BitmapData object is 8,191 pixels wide, 
it can only be 2,048 pixels high.) 

In Flash Player 9 and earlier and AIR 1.1 and earlier, 
the limitation is 2,880 pixels in height and 2,880 in width.
Starting with AIR 3 and Flash player 11, 
the size limits for a BitmapData object have been removed. 
The maximum size of a bitmap is now dependent on the operating system.

如上说明,图片的高宽不能超过8191px,然后总的像素值不能超过16777215px,总像素值计算为:height px * width px。

查看了下上传图片的px为:高度:5184px, 宽度:3456px。总像素值:5184*3456 = 17915904,这个值大于了flash player的限制了。

用ps打开图片修改下图片大小为:高度:4984px,宽度:3323px,总像素值:4984*3323 = 16561832,刚刚好小于flash player的限制,上传,ok,没问题了。果然是这个限制的问题!

PS:上面说flash player 11版本已经移除了这个限制,改为根据系统进行限制,但是不知道系统限制是如何计算的。反正我升级为flash player14上传也提示这个错误。

2014-11-17日更新

推荐一个由百度开源的上传插件webuploader,可以解决此问题。
官方地址: http://fex.baidu.com/webuploader/
其他有点可以查看官网介绍。

06 16,2014

Centos下安装Ice php扩展

安装ICE的方式有两种,一种下载安装包自己编译,一种是通过yum命令进行安装。

因为Ice需要的包比较多,所以编译安装的话比较麻烦,这里我们就只介绍第二种方式,通过yum安装。以下是官方描述:

The Ice run times and developer kits for all supported language mappings (i386 and x86_64).

You can also install Ice using yum by downloading the repository description (zeroc-ice-amzn1.repo) and installing it in /etc/yum.repos.d. Once installed, you can view the available packages using the following command:

# yum list ice* db53* mcpp-devel

Refer to the EC2 documentation for more information.

yum 安装

首先需要添加一个yum源。

# vi /etc/yum.repos.d/zeroc-ice-amzn.repo

写入:

[zeroc-ice]name=Ice 5 for Amazon Linux $releasever - $basearch

baseurl=http://www.zeroc.com/download/Ice/5/el6/$basearchenabled=1gpgcheck=1gpgkey=http://www.zeroc.com/download/RPM-GPG-KEY-zeroc-release

官网上说可以通过

# yum list ice* db53* mcpp-devel

查看安装包列表。真正的安装命令是

# yum install -y ice* db53* mcpp-devel

安装完成可以通过命令搜索文件所在路径:

# find / -name IcePHP.so
06 05,2014

关于PHP使用ICE出现Ice_MemoryLimitException

场景:使用ICE做中间件,C++做服务端,PHP做客户端。

由于某个接口的传输数据过大,接口异常:Ice_MemoryLimitException,搜索了下是由于Ice的默认传输大小是1MB,最大可以设置为2G。

很明显我们传输的数据超过了1M。找到问题那就好办了,设置下Ice.MessageMaxSize即可,找到PHP.ini加上这面这句话:

ice.options="--Ice.MessageSizeMax=2048"

重启php-fpm,在phpinfo中查看确实是生效了。本以为这样就解决了。

但是刷新页面,异常依旧,Google了下,发现C#或者python都可以在代码中设置MessageSizeMax参数。想到PHP应该也可以直接在代码中设置。

在官方网站找到以下例子:

<?php
require 'Ice.php';
require 'Hello.php';
$communicator = null;
try {
    $data = new Ice_InitializationData;
    $data->properties = Ice_createProperties();
    //记载配置文件
    $data->properties->load("props.cfg");
    $communicator = Ice_initialize($data);
    $proxy = $communicator->stringToProxy("...");
    $hello = Demo_HelloPrxHelper::checkedCast($proxy);
    $hello->sayHello();
} catch(Ice_LocalException $ex) {
    // Deal with exception...
}
if($communicator) {
    try {
        $communicator->destroy();
    } catch(Ice_LocalException $ex) {
        // Ignore.
    }
}
?>

确实是可以设置参数,但是这里是配置的文件的,为了方便我直接使用以下代码进行设置:

$data = new Ice_InitializationData;

$data->properties = Ice_createProperties();
//设置传输数据大小, 单位KB
$data->properties->setProperty('Ice.MessageSizeMax', 2048);


$init = Ice_initialize($data);

OK,刷新之后问题解决。

PS:注意Ice.MessageSizeMax参数需要服务端和客户端同时设置。

参考资料:

C#的设置方式:http://stackoverflow.com/questions/8434429/why-do-i-get-icememorylimitexception-even-with-ice-messagesizemax-2000000

官方提供的调用实例:http://doc.zeroc.com/display/Ice/Application+Notes+for+PHP

Ice.MessageMaxSize的设置BUG:http://blog.chinaunix.net/uid-24439730-id-144103.html

05 09,2014

Sublime Text3 + Golang搭建开发环境

本次安装是基于win8 x64系统,sublime text3,Golang1.4.2。

安装GIT

因为golang是通过git来管理远程包的,所以我们首先要安装git,下载地址:http://www.git-scm.com/download/

git安装比较简单,直接下一步即可(在Windows Explorer integration选项中将“Git Bash here”和“Git GUI here”打对勾),需要了解具体安装的可以看这里

安装Golang

1) 首先到https://golang.org/dl/ 选择适合你系统的安装包,(墙内:http://golangtc.com/download )。

2)我这里选择的是:go1.4.2.windows-amd64.msi,下载完成安装到指定目录即可。我这里是(E:\Go)。

3)安装完成后环境变量已经自动设置好了,可以呼出cmd命令行输入命令查看

go env

可以看到,已经出现go命令了。如果你的不能出现,那可能是环境变量设置的问题。
进入我的电脑->高级设置->环境变量查看下,GOROOT变量是否存在,以及Path变量中是否也设置了GOROOT。没有的话,新增一个GOROOT变量,路径则输入你刚刚安装完成后Go的根目录,如我的是:E:\Go\。然后编辑Path变量,在最后面新增”;%GOROOT%bin“。

4)go命令依赖一个环境变量:GOPATH,这不是Go的安装目录,而是你的工作(工程)目录(你的代码都会在该目录下)。GOPATH可以有多个,windows以分号(;)进行区分,Linux系统是冒号(:),当有多个GOPATH时,默认会将go get的内容放在第一个目录下。这里我们新建一个GOPATH变量,以我的为例:F:\mygo

05 06,2014

imagepng压缩等级(quality)

今天在使用imagepng输出图片的,发一个错误 Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib error。

bool imagepng( resource $image [, string $filename [, int $quality ]] )

然后网上搜索了一下,说到quality参数错误了,5.1以前版本的是0-99, 然后5.1以后改为0-9了,这个参数指的是图片的压缩等级。
超出这个0-9这个范围就会提示上述错误。
看下官方的解释吧:

   Compression level: from 0 (no compression) to 9.

数值越大,压缩得越大,不过注意不要超过9。

以下是网上找来的一段测试:

原图体积是125k,现在我想看看在不同的等级压缩下会得到什么样的结果。如下为测试记录。

imagepng($img,null,0); --> Size = 225K
imagepng($img,null,1); --> Size = 85.9K
imagepng($img,null,2); --> Size = 83.7K
imagepng($img,null,3); --> Size = 80.9K
imagepng($img,null,4); --> Size = 74.6K
imagepng($img,null,5); --> Size = 73.8K
imagepng($img,null,6); --> Size = 73K
imagepng($img,null,7); --> Size = 72.4K
imagepng($img,null,8); --> Size = 71K
imagepng($img,null,9); --> Size = 70.6K

我不明白的是为什么压缩等级为0时,它的体积反而变大了?

我们在优化的时候,一般采用2等级比较合适,图片没有失真,但体积减少了30%,达到优化的目的了。如果采用6、7、8、9等级压缩的话,图片有所失真,所以不宜采用这些等级进行压缩优化图片。

我们看看老外是怎么解释这个现象的:

The result seems accurate since 0 means no compression

quality Compression level: from 0 (no compression) to 9.

It's normal for the 0ed file to be larger than the original (that can be slightly compressed to begin with). You need to understand file compression and PHP GD image constructor.

老外的解释有没有道理呢?他认为原始图片其实是经过了轻微的压缩,而0等级的压缩就是一点压缩都没有,所以它的体积会大于原始图片。我感觉此话不无道理。

上一页 下一页