PHP

浏览模式: 标准 | 列表 | 图文
imageworkshop中的position的说明及代码

imageworkshop中的position的说明及代码

position有9个点:lt,mt,rt,lm,mm,rm,lb,mb,rb,其实对应的是几个英文:lt(left top),mt(middle top),rt(right top),以此类推

上官方原图:

5a70fbb6e56219909a7964d99c01c8a4

OK,然后我们看一下他的代码是怎么写的,如果有人涉及到也可以借鉴:

<?php

/**
* Calculate the left top positions of a layer inside a parent layer container
* $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
*
* @param integer $containerWidth
* @param integer $containerHeight
* @param integer $layerWidth
* @param integer $layerHeight
* @param integer $layerPositionX
* @param integer $layerPositionY
* @param string $position
*
* @return array
*/
public static function calculatePositions($containerWidth, $containerHeight, $layerWidth, $layerHeight, $layerPositionX, $layerPositionY, $position = 'LT')
{
$position = strtolower($position);

if ($position == 'rt') {

$layerPositionX = $containerWidth - $layerWidth - $layerPositionX;

} elseif ($position == 'lb') {

$layerPositionY = $containerHeight - $layerHeight - $layerPositionY;

} elseif ($position == 'rb') {

$layerPositionX = $containerWidth - $layerWidth - $layerPositionX;
$layerPositionY = $containerHeight - $layerHeight - $layerPositionY;

} elseif ($position == 'mm') {

$layerPositionX = (($containerWidth - $layerWidth) / 2) + $layerPositionX;
$layerPositionY = (($containerHeight - $layerHeight) / 2) + $layerPositionY;

} elseif ($position == 'mt') {

$layerPositionX = (($containerWidth - $layerWidth) / 2) + $layerPositionX;

} elseif ($position == 'mb') {

$layerPositionX = (($containerWidth - $layerWidth) / 2) + $layerPositionX;
$layerPositionY = $containerHeight - $layerHeight - $layerPositionY;

} elseif ($position == 'lm') {

$layerPositionY = (($containerHeight - $layerHeight) / 2) + $layerPositionY;

} elseif ($position == 'rm') {

$layerPositionX = $containerWidth - $layerWidth - $layerPositionX;
$layerPositionY = (($containerHeight - $layerHeight) / 2) + $layerPositionY;
}

return array(
'x' => $layerPositionX,
'y' => $layerPositionY,
);
}

END;

access-control-allow-origin-multiple-origin-domains

access-control-allow-origin-multiple-origin-domains

原文来自:http://stackoverflow.com/questions/1653308/access-control-allow-origin-multiple-origin-domains

主要是因为涉及到CSS和JS文件是挂在别的域,然后又遇到了ajax之类的操作,所以就需要有跨域操作了,本来很方便的,只是在.htaccess文件中加了一句
Header add Access-Control-Allow-Origin "http://xxxxxx.com"
但因为备案原因,所以又添加了一个备用域名,结果,就报错了,报错提示是:
font from origin 'http://xxxxx.com' has been blocked from loading by Cross-Origin Resource Sharing policy: The 'Access-Control-Allow-Origin' header contains multiple values 'http://yyyy.com, http://zzzz.com', but only one is allowed. Origin 'http://xxxxx.com' is therefore not allowed access.
我晶,不支持多域名啊,google了一下,找到了这一篇:http://stackoverflow.com/questions/1653308/access-control-allow-origin-multiple-origin-domains
不多贴,其实挺有意思。。方法也有不少
1、利用apache的环境变量:
SetEnvIf Origin "^(.*\.example\.com)$" ORIGIN_SUB_DOMAIN=$1
<FilesMatch "\.woff$">
Header set Access-Control-Allow-Origin "%{ORIGIN_SUB_DOMAIN}e" env=ORIGIN_SUB_DOMAIN
</FilesMatch>
2、用PHP来判断:
$http_origin = $_SERVER['HTTP_ORIGIN'];

if ($http_origin == "http://www.domain1.com" || $http_origin == "http://www.domain2.com" || $http_origin == "http://www.domain3.info")
{
header("Access-Control-Allow-Origin: $http_origin");
}
3、和1有点类似
SetEnvIf Origin "^http(s)?://(.+\.)?(domain\.org|domain2\.com)$" origin_is=$0
Header always set Access-Control-Allow-Origin %{origin_is}e env=origin_is
4、这是nginx的:
location /fonts {
# this will echo back the origin header
if ($http_origin ~ "example.org$") {
add_header "Access-Control-Allow-Origin" $http_origin;
}
}
更多的,还是去原网页看吧。。
利用pubu.im来做BUG提醒工具

利用pubu.im来做BUG提醒工具

BUG提醒工具其实有很多,比如可以在有BUG的时候发封邮件到你的常用邮箱,这样你的手机上(邮件APP)就会收到一条提醒。或者有短信SP通道的话,你也可以有类似处理。当然还有monolog,支持php-console插件,可以发送到你的chrome上面,我在这里再换个小方式,利用pubu.im这个IM工具(或者说。。。。说不出来)来做提醒

流程很简单

1、去pubu.im测试一个帐户。。这不用我多说吧

2、下载MAC客户端登录,自己添加自己为一个机器人,相当于自己和自己聊天,如果你的消息不算隐私,你可以直接用现成的:小布 这个通道

3、在聊天界面选择插件,点击更多,打开网页,添加“incoming”插件,生成一个URL,选择你刚才聊天组

4、根据提示信息在你的APP里写上一段测试代码,POST方式的,可以用curl/file_get_contents/Requests/guzzle等,工具实在太多,不想多说

5、测试通过后,可以尝试自己用set_error_handle,自己处理出错信息,在出错信息前进行判断:

if(debug_mode){ //send report }

6、你会发现 右上角弹了一个小窗,就是你刚才的测试标题!

----EOF---

就这样,你在工作的时候不需要打开手机,电脑上会直接有提醒哦~~~而且因为是聊天记录,你还可以往前翻,到底是什么BUG,嗯,再也不用客户端开发人员和你说,XXX接口出错了。。。因为他一出错你就收到,然后你就可以在他没有和你提的时候悄悄的改掉,等他提出来有BUG的时候,你说,在哪里?一定是你访问的姿势不对,不信你试试

哈哈

aliyun的dotdeb源有BUG

aliyun的dotdeb源有BUG

纯笔记做个记录
1、http://mirrors.aliyun.com/ ,这是阿里的源

2、这是dotdeb的说明:http://mirrors.aliyun.com/help/dotdeb

3、阿里云的说明:

编辑/etc/apt/sources.list文件(需要使用sudo), 在文件最后面添加以下条目(操作前请做好相应备份)

deb http://mirrors.aliyun.com/dotdeb/packages.dotdeb.org wheezy all
deb-src http://mirrors.aliyun.com/dotdeb/packages.dotdeb.org wheezy all

其实上面的源的写法是错误的,不需要package.dotdeb.org,应该是这样


deb http://mirrors.aliyun.com/dotdeb wheezy all
deb-src http://mirrors.aliyun.com/dotdeb wheezy all

不过,如果是默认这样其实没有什么特别用,PHP还是5.4,所以如果要针对PHP,就需要再加上

deb http://mirrors.aliyun.com/dotdeb wheezy-php56 all
deb-src http://mirrors.aliyun.com/dotdeb wheezy-php56 all

其实说白了就是看:http://mirrors.aliyun.com/dotdeb/dists/,看这一页支持哪些,然后加在源里就OK了。就象官网说的,如果是阿里云服务器,请使用aliyuncs,流量直接从内网走~

curl上传注意事项【新】

curl上传注意事项【新】

众所周知,用curl上传只要设置4个变量即可(非HTTPS网站),那就是curlopt_url,curlopt_post,curlopt_returntransfer,curlopt_postfields,当然这4个都是大写。

curlopt_post,curlopt_returntransfer 都是 true,curlopt_url则是你要提交的网址,那么剩下的,curlopt_postfields就是你要上传的内容了。

上传文件在这里也变得非常简单,只需对应的值前面加个“@”即可,如curl_setopt($ch,CURLOPT_POSTFIELDS,array('file'=>'@/var/www/images/abc.jpg')); 文件通过realpath判断后存在的即可。

但是,这一切在php5.6里就发生了改成。php5.6不再支持@这样的上传方式,只能使用curl_file_create的方式来,所以上面的代码要改成

curl_setopt($ch,CURLOPT_POSTFIELDS,array('file'=>curl_file_create('/var/www/images/abc.jpg','image/jpg','abc')));

看看文档里怎么说:

CURLFile should be used to upload a file with CURLOPT_POSTFIELDS.

然后官网的手册中的最后一条评论,仿佛是看不下去,写了个处理上传的程序

There are "@" issue on multipart POST requests.

Solution for PHP 5.5 or later:
- Enable CURLOPT_SAFE_UPLOAD.
- Use CURLFile instead of "@".

Solution for PHP 5.4 or earlier:
- Build up multipart content body by youself.
- Change "Content-Type" header by yourself.

The following snippet will help you :D

<?php

/**
* For safe multipart POST request for PHP5.3 ~ PHP 5.4.
*
* @param resource $ch cURL resource
* @param array $assoc "name => value"
* @param array $files "name => path"
* @return bool
*/
function curl_custom_postfields($ch, array $assoc = array(), array $files = array()) {

// invalid characters for "name" and "filename"
static $disallow = array("\0", "\"", "\r", "\n");

// build normal parameters
foreach ($assoc as $k => $v) {
$k = str_replace($disallow, "_", $k);
$body[] = implode("\r\n", array(
"Content-Disposition: form-data; name=\"{$k}\"",
"",
filter_var($v),
));
}

// build file parameters
foreach ($files as $k => $v) {
switch (true) {
case false === $v = realpath(filter_var($v)):
case !is_file($v):
case !is_readable($v):
continue; // or return false, throw new InvalidArgumentException
}
$data = file_get_contents($v);
$v = call_user_func("end", explode(DIRECTORY_SEPARATOR, $v));
$k = str_replace($disallow, "_", $k);
$v = str_replace($disallow, "_", $v);
$body[] = implode("\r\n", array(
"Content-Disposition: form-data; name=\"{$k}\"; filename=\"{$v}\"",
"Content-Type: application/octet-stream",
"",
$data,
));
}

// generate safe boundary
do {
$boundary = "---------------------" . md5(mt_rand() . microtime());
} while (preg_grep("/{$boundary}/", $body));

// add boundary for each parameters
array_walk($body, function (&$part) use ($boundary) {
$part = "--{$boundary}\r\n{$part}";
});

// add final boundary
$body[] = "--{$boundary}--";
$body[] = "";

// set options
return @curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => implode("\r\n", $body),
CURLOPT_HTTPHEADER => array(
"Expect: 100-continue",
"Content-Type: multipart/form-data; boundary={$boundary}", // change Content-Type
),
));
}

好吧,其实多文件上传用这样的方式就挺好。

几个常见的 Composer 国内镜像

几个常见的 Composer 国内镜像

现在写PHP好象不用Composer总感觉有点Low了吧?好吧,即使这样,你在使用composer update的时候是不是也发现网速、进度low的不行。一个composer update要等半天。不说这个吧,你就是一个composer init也要等半天,这只是个空项目,你都要等这么久?composer 如果不加-vvv参数,你就感觉整个世界都停在那里了。所以,如果连接原始的composer还是加个-vvv参数吧

国内常见镜像有三个:

http://pkg.phpcomposer.com/
https://toran.reimu.io/
http://packagist.cn/

使用方法很简单一种是在~/.composer/config.json中加入相应的信息,一种是在当前项目的composer.json中加入:

"repositories": [
{"type": "composer", "url": "http://pkg.phpcomposer.com/repo/packagist/"},
{"packagist": false}
]

加入的时候注意语法,如果是json的最后一个元素,记得不要加","即可,否则 会报错

OK这样就可以了,尽情的尝试吧(packagist.cn更新的有点慢,toran.reimu.io 是https的,如果你本机openssl版本低可能会更新失败)。好象话这么一说,只能用pkg.phpcomposer.com的源了(嗯,忍忍吧,如果他不能用,你再换)

更新:composer 源

PHP Warning: Module 'modulename' already loaded in Unknown on line 0

PHP Warning: Module 'modulename' already loaded in Unknown on line 0

出现标题这样的错误大概是:

1、模块加载了两次,所以php -i|grep Configure,看一下配置文件和配置include的目录,对于这些文件中是否有同名的module

2、动态加载模块时,模块的目录下与php.ini中都有一个同名so

知道上述的问题就好办了,先看php配置文件和include目录下的文件,先整理内容。然后查看extension_dir,看看这个目录下的文件与include中的文件引用路径是否一致。

array_key_exists 和 isset

array_key_exists 和 isset

看到andot的hprose中有使用array_key_exists,就想问为什么不用isset,再一想哦。isset不能判断$a['xxx'] = null的情况。这时候看了一下评论,有人建议用isset() || array_key_exists来辅助判断,说是性能有大提升。

于是写了点代码测试,果然:

count array: 999900 ; loop :100000
$array['109011'] = 109011;
1. array_key_exists(109011,$array);
bool(true)
0.021718

2. isset($array['109011']);
bool(true)
0.004888

3. isset( $array['109011'] ) || array_key_exists( '109011', $array )
bool(true)
0.005100


count array: 999900 ; loop :100000
$array['109011'] = null;
1. array_key_exists(109011,$array);
bool(true)
0.026091

2. isset($array['109011']);
bool(false) //注意看这里
0.006697

3. isset( $array['109011'] ) || array_key_exists( '109011', $array )
bool(true)
0.026621

基本上,在$a['x'] 不是null的情况下,性能提升4倍左右 ,如果该值是null,这损耗可以接受。

数组长度是999900,循环100000次

所以以后代码还是 isset($a[$key])||array_key_exists($key,$a) 比较好。
当然,如果你平时处理isset($_GET['xxx'])的时候,不需要用array_key_exists的,因为URL中都是string

又一个不错的UI皮肤

又一个不错的UI皮肤

平时的工作中,除了bootstrap,其实还是有一些其他的皮肤是可以尝试的,比如这个“http://semantic-ui.com/”,你就可以看到与bootstrap等不同的效果。

semantic-ui准备了一些常用的小效果,而且自带了一些小皮肤,比如github的效果,比如bootstrap,twitter的小效果,你在使用中会发现它的好的

不多介绍,直接去官网看吧。如果你偷懒,就用这个链接下载吧:https://github.com/Semantic-Org/Semantic-UI/archive/1.10.2.zip

官网:http://semantic-ui.com/

github: https://github.com/semantic-org/semantic-ui/

yiisoft/yii2 2.0.2 requires bower-asset/jquery 2.1.*@stable | 1.11.*@stable -> no matching package found

yiisoft/yii2 2.0.2 requires bower-asset/jquery 2.1.*@stable | 1.11.*@stable -> no matching package found

在composer update项目的时候,报了如标题一样的错误,当然这个错误 有一大堆

composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- The requested package bower-asset/jquery could not be found in any version, there may be a typo in the package name.
Problem 2
- Installation request for yiisoft/yii2 dev-master -> satisfiable by yiisoft/yii2[dev-master].
- yiisoft/yii2 dev-master requires bower-asset/jquery 2.1.*@stable | 1.11.*@stable -> no matching package found.
Problem 3
- yiisoft/yii2 2.0.x-dev requires bower-asset/jquery 2.1.*@stable | 1.11.*@stable -> no matching package found.
- yiisoft/yii2 dev-master requires bower-asset/jquery 2.1.*@stable | 1.11.*@stable -> no matching package found.
- Removal request for yiisoft/yii2-imagine == 2.0.9999999.9999999-dev
- yiisoft/yii2-imagine 2.0.0 requires yiisoft/yii2 * -> satisfiable by yiisoft/yii2[dev-master, 2.0.x-dev].
- yiisoft/yii2-imagine 2.0.0-rc requires yiisoft/yii2 * -> satisfiable by yiisoft/yii2[dev-master, 2.0.x-dev].
- yiisoft/yii2-imagine 2.0.1 requires yiisoft/yii2 * -> satisfiable by yiisoft/yii2[dev-master, 2.0.x-dev].
- yiisoft/yii2-imagine 2.0.2 requires yiisoft/yii2 * -> satisfiable by yiisoft/yii2[dev-master, 2.0.x-dev].
- Removal request for yiisoft/yii2-imagine == 9999999-dev
- Installation request for yiisoft/yii2-imagine * -> satisfiable by yiisoft/yii2-imagine[2.0.0, 2.0.0-rc, 2.0.1, 2.0.2, 2.0.x-dev, dev-master].

Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting
see <https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion> for more details.

Read <http://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.
看上去好纠结,但其实官方有过提示,通过查看:http://www.yiiframework.com/doc-2.0/guide-start-installation.html#installing-via-composer,可知,只要运行:
composer global require "fxp/composer-asset-plugin:1.0.0"
问题解决。
composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Reading bower.json of bower-asset/jquery.inputmask (3.1.47)
Could not fetch https://api.github.com/repos/RobinHerbots/jquery.inputmask/contents/bower.json?ref=9ff37e85bc2fa2350475c4a7fa60aa20cd211481, enter your GitHub credentials to go over the API rate limit
The credentials will be swapped for an OAuth token stored in /Users/*******/.composer/auth.json, your password will not be stored
To revoke access to this token you can visit https://github.com/settings/applications
Username:********
Password:********
Token successfully created
Reading bower.json of bower-asset/jquery.inputmask (3.1.44)
。。。。。。。此处省略500字