手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表2012年09月28日的文章

推荐小程序:TimThumb – PHP Image Resizer

这是一个单文件处理缩略图的程序,直接引用生成缩略图。有点意思,主要是方便啦 .。

官方地址是: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

参数很简单,就是不太好记。。。纠结啊

不过用起来的话,是真心简单

 

Tags: thumb

旧文:QeePHP中的优秀函数(三)

这几个函数还是来自于QeePHP的核心类Q中。不过,我是自认为,我的configure类有部分写的比他好,不过我没有考虑删除之类的。呵呵。

PHP代码
  1. /** 
  2.  * 获取指定的设置内容 
  3.  * 
  4.  * $option 参数指定要获取的设置名。 
  5.  * 如果设置中找不到指定的选项,则返回由 $default 参数指定的值。 
  6.  * 
  7.  * @code php 
  8.  * $option_value = Q::ini('my_option'); 
  9.  * @endcode 
  10.  * 
  11.  * 对于层次化的设置信息,可以通过在 $option 中使用“/”符号来指定。 
  12.  * 
  13.  * 例如有一个名为 option_group 的设置项,其中包含三个子项目。 
  14.  * 现在要查询其中的 my_option 设置项的内容。 
  15.  * 
  16.  * @code php 
  17.  * // +--- option_group 
  18.  * //   +-- my_option  = this is my_option 
  19.  * //   +-- my_option2 = this is my_option2 
  20.  * //   \-- my_option3 = this is my_option3 
  21.  * 
  22.  * // 查询 option_group 设置组里面的 my_option 项 
  23.  * // 将会显示 this is my_option 
  24.  * echo Q::ini('option_group/my_option'); 
  25.  * @endcode 
  26.  * 
  27.  * 要读取更深层次的设置项,可以使用更多的“/”符号,但太多层次会导致读取速度变慢。 
  28.  * 
  29.  * 如果要获得所有设置项的内容,将 $option 参数指定为 '/' 即可: 
  30.  * 
  31.  * @code php 
  32.  * // 获取所有设置项的内容 
  33.  * $all = Q::ini('/'); 
  34.  * @endcode 
  35.  * 
  36.  * @param string $option 要获取设置项的名称 
  37.  * @param mixed $default 当设置不存在时要返回的设置默认值 
  38.  * 
  39.  * @return mixed 返回设置项的值 
  40.  */  
  41. static function ini($option$default = null)  
  42. {  
  43.     if ($option == '/'return self::$_ini;  
  44.   
  45.     if (strpos($option'/') === false)  
  46.     {  
  47.         return array_key_exists($option, self::$_ini)  
  48.             ? self::$_ini[$option]  
  49.             : $default;  
  50.     }  
  51.   
  52.     $parts = explode('/'$option);  
  53.     $pos =& self::$_ini;  
  54.     foreach ($parts as $part)  
  55.     {  
  56.         if (!isset($pos[$part])) return $default;  
  57.         $pos =& $pos[$part];  
  58.     }  
  59.     return $pos;  
  60. }  
  61.   
  62. /** 
  63.  * 修改指定设置的内容 
  64.  * 
  65.  * 当 $option 参数是字符串时,$option 指定了要修改的设置项。 
  66.  * $data 则是要为该设置项指定的新数据。 
  67.  * 
  68.  * @code php 
  69.  * // 修改一个设置项 
  70.  * Q::changeIni('option_group/my_option2', 'new value'); 
  71.  * @endcode 
  72.  * 
  73.  * 如果 $option 是一个数组,则假定要修改多个设置项。 
  74.  * 那么 $option 则是一个由设置项名称和设置值组成的名值对,或者是一个嵌套数组。 
  75.  * 
  76.  * @code php 
  77.  * // 假设已有的设置为 
  78.  * // +--- option_1 = old value 
  79.  * // +--- option_group 
  80.  * //   +-- option1 = old value 
  81.  * //   +-- option2 = old value 
  82.  * //   \-- option3 = old value 
  83.  * 
  84.  * // 修改多个设置项 
  85.  * $arr = array( 
  86.  *      'option_1' => 'value 1', 
  87.  *      'option_2' => 'value 2', 
  88.  *      'option_group/option2' => 'new value', 
  89.  * ); 
  90.  * Q::changeIni($arr); 
  91.  * 
  92.  * // 修改后 
  93.  * // +--- option_1 = value 1 
  94.  * // +--- option_2 = value 2 
  95.  * // +--- option_group 
  96.  * //   +-- option1 = old value 
  97.  * //   +-- option2 = new value 
  98.  * //   \-- option3 = old value 
  99.  * @endcode 
  100.  * 
  101.  * 上述代码展示了 Q::changeIni() 的一个重要特性:保持已有设置的层次结构。 
  102.  * 
  103.  * 因此如果要完全替换某个设置项和其子项目,应该使用 Q::replaceIni() 方法。 
  104.  * 
  105.  * @param string|array $option 要修改的设置项名称,或包含多个设置项目的数组 
  106.  * @param mixed $data 指定设置项的新值 
  107.  */  
  108. static function changeIni($option$data = null)  
  109. {  
  110.     if (is_array($option))  
  111.     {  
  112.         foreach ($option as $key => $value)  
  113.         {  
  114.             self::changeIni($key$value);  
  115.         }  
  116.         return;  
  117.     }  
  118.   
  119.     if (!is_array($data))  
  120.     {  
  121.         if (strpos($option'/') === false)  
  122.         {  
  123.             self::$_ini[$option] = $data;  
  124.             return;  
  125.         }  
  126.   
  127.         $parts = explode('/'$option);  
  128.         $max = count($parts) - 1;  
  129.         $pos =& self::$_ini;  
  130.         for ($i = 0; $i < = $max$i ++)  
  131.         {  
  132.             $part = $parts[$i];  
  133.             if ($i < $max)  
  134.             {  
  135.                 if (!isset($pos[$part]))  
  136.                 {  
  137.                     $pos[$part] = array();  
  138.                 }  
  139.                 $pos =& $pos[$part];  
  140.             }  
  141.             else  
  142.             {  
  143.                 $pos[$part] = $data;  
  144.             }  
  145.         }  
  146.     }  
  147.     else  
  148.     {  
  149.         foreach ($data as $key => $value)  
  150.         {  
  151.             self::changeIni($option . '/' . $key$value);  
  152.         }  
  153.     }  
  154. }  
  155.   
  156. /** 
  157.  * 替换已有的设置值 
  158.  * 
  159.  * Q::replaceIni() 表面上看和 Q::changeIni() 类似。 
  160.  * 但是 Q::replaceIni() 不会保持已有设置的层次结构, 
  161.  * 而是直接替换到指定的设置项及其子项目。 
  162.  * 
  163.  * @code php 
  164.  * // 假设已有的设置为 
  165.  * // +--- option_1 = old value 
  166.  * // +--- option_group 
  167.  * //   +-- option1 = old value 
  168.  * //   +-- option2 = old value 
  169.  * //   \-- option3 = old value 
  170.  * 
  171.  * // 替换多个设置项 
  172.  * $arr = array( 
  173.  *      'option_1' => 'value 1', 
  174.  *      'option_2' => 'value 2', 
  175.  *      'option_group/option2' => 'new value', 
  176.  * ); 
  177.  * Q::replaceIni($arr); 
  178.  * 
  179.  * // 修改后 
  180.  * // +--- option_1 = value 1 
  181.  * // +--- option_2 = value 2 
  182.  * // +--- option_group 
  183.  * //   +-- option2 = new value 
  184.  * @endcode 
  185.  * 
  186.  * 从上述代码的执行结果可以看出 Q::replaceIni() 和 Q::changeIni() 的重要区别。 
  187.  * 
  188.  * 不过由于 Q::replaceIni() 速度比 Q::changeIni() 快很多, 
  189.  * 因此应该尽量使用 Q::replaceIni() 来代替 Q::changeIni()。 
  190.  * 
  191.  * @param string|array $option 要修改的设置项名称,或包含多个设置项目的数组 
  192.  * @param mixed $data 指定设置项的新值 
  193.  */  
  194. static function replaceIni($option$data = null)  
  195. {  
  196.     if (is_array($option))  
  197.     {  
  198.         self::$_ini = array_merge(self::$_ini$option);  
  199.     }  
  200.     else  
  201.     {  
  202.         self::$_ini[$option] = $data;  
  203.     }  
  204. }  
  205.   
  206. /** 
  207.  * 删除指定的设置 
  208.  * 
  209.  * Q::cleanIni() 可以删除指定的设置项目及其子项目。 
  210.  * 
  211.  * @param mixed $option 要删除的设置项名称 
  212.  */  
  213. static function cleanIni($option)  
  214. {  
  215.     if (strpos($option'/') === false)  
  216.     {  
  217.         unset(self::$_ini[$option]);  
  218.     }  
  219.     else  
  220.     {  
  221.         $parts = explode('/'$option);  
  222.         $max = count($parts) - 1;  
  223.         $pos =& self::$_ini;  
  224.         for ($i = 0; $i < = $max$i ++)  
  225.         {  
  226.             $part = $parts[$i];  
  227.             if ($i < $max)  
  228.             {  
  229.                 if (!isset($pos[$part]))  
  230.                 {  
  231.                     $pos[$part] = array();  
  232.                 }  
  233.                 $pos =& $pos[$part];  
  234.             }  
  235.             else  
  236.             {  
  237.                 unset($pos[$part]);  
  238.             }  
  239.         }  
  240.     }  
  241. }  

 

Tags: qeephp

旧文:QeePHP中的优秀函数(二)

这两个函数来自于Helper_Array,我觉得是非常常用的方法,功能也比较强大。适合大家使用。

PHP代码
  1. /** 
  2.  * 将一个平面的二维数组按照指定的字段转换为树状结构 
  3.  * 
  4.  * 用法: 
  5.  * @code php 
  6.  * $rows = array( 
  7.  *     array('id' => 1, 'value' => '1-1', 'parent' => 0), 
  8.  *     array('id' => 2, 'value' => '2-1', 'parent' => 0), 
  9.  *     array('id' => 3, 'value' => '3-1', 'parent' => 0), 
  10.  * 
  11.  *     array('id' => 7, 'value' => '2-1-1', 'parent' => 2), 
  12.  *     array('id' => 8, 'value' => '2-1-2', 'parent' => 2), 
  13.  *     array('id' => 9, 'value' => '3-1-1', 'parent' => 3), 
  14.  *     array('id' => 10, 'value' => '3-1-1-1', 'parent' => 9), 
  15.  * ); 
  16.  * 
  17.  * $tree = Helper_Array::tree($rows, 'id', 'parent', 'nodes'); 
  18.  * 
  19.  * dump($tree); 
  20.  *   // 输出结果为: 
  21.  *   // array( 
  22.  *   //   array('id' => 1, ..., 'nodes' => array()), 
  23.  *   //   array('id' => 2, ..., 'nodes' => array( 
  24.  *   //        array(..., 'parent' => 2, 'nodes' => array()), 
  25.  *   //        array(..., 'parent' => 2, 'nodes' => array()), 
  26.  *   //   ), 
  27.  *   //   array('id' => 3, ..., 'nodes' => array( 
  28.  *   //        array('id' => 9, ..., 'parent' => 3, 'nodes' => array( 
  29.  *   //             array(..., , 'parent' => 9, 'nodes' => array(), 
  30.  *   //        ), 
  31.  *   //   ), 
  32.  *   // ) 
  33.  * @endcode 
  34.  * 
  35.  * 如果要获得任意节点为根的子树,可以使用 $refs 参数: 
  36.  * @code php 
  37.  * $refs = null; 
  38.  * $tree = Helper_Array::tree($rows, 'id', 'parent', 'nodes', $refs); 
  39.  *  
  40.  * // 输出 id 为 3 的节点及其所有子节点 
  41.  * $id = 3; 
  42.  * dump($refs[$id]); 
  43.  * @endcode 
  44.  * 
  45.  * @param array $arr 数据源 
  46.  * @param string $key_node_id 节点ID字段名 
  47.  * @param string $key_parent_id 节点父ID字段名 
  48.  * @param string $key_childrens 保存子节点的字段名 
  49.  * @param boolean $refs 是否在返回结果中包含节点引用 
  50.  * 
  51.  * return array 树形结构的数组 
  52.  */  
  53. static function toTree($arr$key_node_id$key_parent_id = 'parent_id',  
  54.                        $key_childrens = 'childrens', & $refs = null)  
  55. {  
  56.     $refs = array();  
  57.     foreach ($arr as $offset => $row)   
  58.     {  
  59.         $arr[$offset][$key_childrens] = array();  
  60.         $refs[$row[$key_node_id]] =& $arr[$offset];  
  61.     }  
  62.   
  63.     $tree = array();  
  64.     foreach ($arr as $offset => $row)   
  65.     {  
  66.         $parent_id = $row[$key_parent_id];  
  67.         if ($parent_id)  
  68.         {  
  69.             if (!isset($refs[$parent_id]))  
  70.             {  
  71.                 $tree[] =& $arr[$offset];  
  72.                 continue;  
  73.             }  
  74.             $parent =& $refs[$parent_id];  
  75.             $parent[$key_childrens][] =& $arr[$offset];  
  76.         }  
  77.         else  
  78.         {  
  79.             $tree[] =& $arr[$offset];  
  80.         }  
  81.     }  
  82.   
  83.     return $tree;  
  84. }  
  85.   
  86. /** 
  87.  * 将树形数组展开为平面的数组 
  88.  * 
  89.  * 这个方法是 tree() 方法的逆向操作。 
  90.  * 
  91.  * @param array $tree 树形数组 
  92.  * @param string $key_childrens 包含子节点的键名 
  93.  * 
  94.  * @return array 展开后的数组 
  95.  */  
  96. static function treeToArray($tree$key_childrens = 'childrens')  
  97. {  
  98.     $ret = array();  
  99.     if (isset($tree[$key_childrens]) && is_array($tree[$key_childrens]))  
  100.     {  
  101.         $childrens = $tree[$key_childrens];  
  102.         unset($tree[$key_childrens]);  
  103.         $ret[] = $tree;  
  104.         foreach ($childrens as $node)   
  105.         {  
  106.             $ret = array_merge($ret, self::treeToArray($node$key_childrens));  
  107.         }  
  108.     }  
  109.     else  
  110.     {  
  111.         unset($tree[$key_childrens]);  
  112.         $ret[] = $tree;  
  113.     }  
  114.     return $ret;  
  115. }  

不过显而易见,这两个函数,都不需要多介绍,tree2list,list2tree,想想也知道怎么用,再加上注释又比较全。
可惜QeePHP不再开发,而ThinkPHP积下来的问题又很多,改动起来也非常痛苦。所以我开始慢慢分析一下。

Tags: qeephp

旧文:QeePHP中的优秀函数(一)

基类Q中的normalize。

PHP代码
  1. /** 
  2.  * 对字符串或数组进行格式化,返回格式化后的数组 
  3.  * 
  4.  * $input 参数如果是字符串,则首先以“,”为分隔符,将字符串转换为一个数组。 
  5.  * 接下来对数组中每一个项目使用 trim() 方法去掉首尾的空白字符。最后过滤掉空字符串项目。 
  6.  * 
  7.  * 该方法的主要用途是将诸如:“item1, item2, item3” 这样的字符串转换为数组。 
  8.  * 
  9.  * @code php 
  10.  * $input = 'item1, item2, item3'; 
  11.  * $output = Q::normalize($input); 
  12.  * // $output 现在是一个数组,结果如下: 
  13.  * // $output = array( 
  14.  * //   'item1', 
  15.  * //   'item2', 
  16.  * //   'item3', 
  17.  * // ); 
  18.  * 
  19.  * $input = 'item1|item2|item3'; 
  20.  * // 指定使用什么字符作为分割符 
  21.  * $output = Q::normalize($input, '|'); 
  22.  * @endcode 
  23.  * 
  24.  * @param array|string $input 要格式化的字符串或数组 
  25.  * @param string $delimiter 按照什么字符进行分割 
  26.  * 
  27.  * @return array 格式化结果 
  28.  */  
  29. static function normalize($input$delimiter = ',')  
  30. {  
  31.     if (!is_array($input))  
  32.     {  
  33.         $input = explode($delimiter$input);  
  34.     }  
  35.     $input = array_map('trim'$input);  
  36.     return array_filter($input'strlen');  
  37. }  
看到这个方法,其实应该感觉得到它的用户,确实,大多数情况下,我们都是用explode来分割字符串的,但事实上,你不能保证别人给你的字符串有没有多 余的空格,或者无效的字符串,因此,通过这个函数,就可以去除掉很无效的值 。建议使用它来代替explode,但,如果确实是很规则的,还是用explode吧,毕竟,如果这个分割出来的数组很大,效率肯定比explode低很 多了。

 

 

 

Tags: qeephp