利用php的curl库上传文件真的很方便
注意几点
1、只能是POST
2、可以不写头(平时的form有文件和无文件时发送的时候,)
3、postfields的时候,不要你傻傻的http_build_query,你越是这么做了。黑黑。。。就反而不一定对了
4、上传的文件用@来连接。
一个很小的例子
PHP代码
- $formAction = 'http://xxx/xxx/xx.upload.php';
- $postVals = array(
-
-
- 'img'=>'@/var/www/xxx.jpg',
-
- );
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $formAction);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, ($postVals));
- $res = curl_exec($ch);
- curl_close($ch);
看看多简单,一下子就成功了
这是一个单文件处理缩略图的程序,直接引用生成缩略图。有点意思,主要是方便啦 .。
官方地址是:http://www.binarymoon.co.uk/projects/timthumb/
文件也很小,只有一点点大,更关键的是可以缓存回来。也能取远程的图片,也可以设置allow的安全站点,这样就相对安全一点了,官方这么介绍 :
TimThumb is a simple, flexible, PHP script that resizes images. You give it a bunch of parameters, and it spits out a thumbnail image that you can display on your site.
TimThumb has seen a massive amount of use across the WordPress world, and a few months after we released it, I took over development from Tim, a friend of Darren Hoyts, who created the original script. Since I took over there have been a whole host of changes, bug fixes, and additions.
I set up this page to act as an archive/ resource, showing the best documentation and demos available for TimThumb. If you have any TimThumb related articles then please feel free to send them over to me so that I can add them to the list.
功能也相对比较方便,用法也很简单,官方现成就举了一些例子,并通过这些例子介绍了它的参数:
How to use TimThumb
看了上面的使用方法,再看看,这个,你就知道很多常见的方法和用法了:
stands for |
values |
What it does |
src |
source |
url to image |
Tells TimThumb which image to resize › tutorial |
w |
width |
the width to resize to |
Remove the width to scale proportionally (will then need the height) › tutorial |
h |
height |
the height to resize to |
Remove the height to scale proportionally (will then need the width) › tutorial |
q |
quality |
0 - 100 |
Compression quality. The higher the number the nicer the image will look. I wouldn't recommend going any higher than about 95 else the image will get too large › tutorial |
a |
alignment |
c, t, l, r, b, tl, tr, bl, br |
Crop alignment. c = center, t = top, b = bottom, r = right, l = left. The positions can be joined to create diagonal positions › tutorial |
zc |
zoom / crop |
0, 1, 2, 3 |
Change the cropping and scaling settings › tutorial |
f |
filters |
too many to mention |
Let's you apply image filters to change the resized picture. For instance you can change brightness/ contrast or even blur the image › tutorial |
s |
sharpen |
|
Apply a sharpen filter to the image, makes scaled down images look a little crisper › tutorial |
cc |
canvas colour |
hexadecimal colour value (#ffffff) |
Change background colour. Most used when changing the zoom and crop settings, which in turn can add borders to the image. |
ct |
canvas transparency |
true (1) |
Use transparency and ignore background colour |
参数很简单,就是不太好记。。。纠结啊
不过用起来的话,是真心简单
这几个函数还是来自于QeePHP的核心类Q中。不过,我是自认为,我的configure类有部分写的比他好,不过我没有考虑删除之类的。呵呵。
PHP代码
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static function ini($option, $default = null)
- {
- if ($option == '/') return self::$_ini;
-
- if (strpos($option, '/') === false)
- {
- return array_key_exists($option, self::$_ini)
- ? self::$_ini[$option]
- : $default;
- }
-
- $parts = explode('/', $option);
- $pos =& self::$_ini;
- foreach ($parts as $part)
- {
- if (!isset($pos[$part])) return $default;
- $pos =& $pos[$part];
- }
- return $pos;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static function changeIni($option, $data = null)
- {
- if (is_array($option))
- {
- foreach ($option as $key => $value)
- {
- self::changeIni($key, $value);
- }
- return;
- }
-
- if (!is_array($data))
- {
- if (strpos($option, '/') === false)
- {
- self::$_ini[$option] = $data;
- return;
- }
-
- $parts = explode('/', $option);
- $max = count($parts) - 1;
- $pos =& self::$_ini;
- for ($i = 0; $i < = $max; $i ++)
- {
- $part = $parts[$i];
- if ($i < $max)
- {
- if (!isset($pos[$part]))
- {
- $pos[$part] = array();
- }
- $pos =& $pos[$part];
- }
- else
- {
- $pos[$part] = $data;
- }
- }
- }
- else
- {
- foreach ($data as $key => $value)
- {
- self::changeIni($option . '/' . $key, $value);
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static function replaceIni($option, $data = null)
- {
- if (is_array($option))
- {
- self::$_ini = array_merge(self::$_ini, $option);
- }
- else
- {
- self::$_ini[$option] = $data;
- }
- }
-
-
-
-
-
-
-
-
- static function cleanIni($option)
- {
- if (strpos($option, '/') === false)
- {
- unset(self::$_ini[$option]);
- }
- else
- {
- $parts = explode('/', $option);
- $max = count($parts) - 1;
- $pos =& self::$_ini;
- for ($i = 0; $i < = $max; $i ++)
- {
- $part = $parts[$i];
- if ($i < $max)
- {
- if (!isset($pos[$part]))
- {
- $pos[$part] = array();
- }
- $pos =& $pos[$part];
- }
- else
- {
- unset($pos[$part]);
- }
- }
- }
- }
这两个函数来自于Helper_Array,我觉得是非常常用的方法,功能也比较强大。适合大家使用。
不过显而易见,这两个函数,都不需要多介绍,tree2list,list2tree,想想也知道怎么用,再加上注释又比较全。
可惜QeePHP不再开发,而ThinkPHP积下来的问题又很多,改动起来也非常痛苦。所以我开始慢慢分析一下。
基类Q中的normalize。
PHP代码
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static function normalize($input, $delimiter = ',')
- {
- if (!is_array($input))
- {
- $input = explode($delimiter, $input);
- }
- $input = array_map('trim', $input);
- return array_filter($input, 'strlen');
- }
看到这个方法,其实应该感觉得到它的用户,确实,大多数情况下,我们都是用explode来分割字符串的,但事实上,你不能保证别人给你的字符串有没有多 余的空格,或者无效的字符串,因此,通过这个函数,就可以去除掉很无效的值 。建议使用它来代替explode,但,如果确实是很规则的,还是用explode吧,毕竟,如果这个分割出来的数组很大,效率肯定比explode低很 多了。