2009-03-31

在Symfony中使用第三方图片处理工具WideImage

类归于: PHP, symfony — 标签:, , , maker @ 14:01

最近在整理php的一些库,同时也在研究图片处理的相关功能,所以就发现了WideImage, 虽然是很强大的一个图片处理库,但貌似在国内没有很多人知道。

看了网上很多的图片处理类,功能也就是简单的水印和缩略图,这些WideImage都不在话下,在WideImage源代码中的demos里,我们可以看到转换灰度图,反色,遮罩(Mask)处理,通道(Channels)处理,水印,滤镜,缩放,旋转,翻转和Canvas(我理解也就是在图片上写字)等等功能。

下面简单记录一下如何将WideImage用在symfony中。

首先我们去WideImage的主页(http://wideimage.sourceforge.net/)下载源码包,解包之后会有三个目录,lib,tests,demos,lib是库文件,tests是测试文件,demos中是作者提供的代码示例,可以直接运行访问,要在symfony中使用WideImage,只要将lib目录放在项目的lib目录中就可以。

下面是一些简单的例子:

/**
* 遮罩处理
*/
public function executeMask(sfWebRequest $request)
{
$dir = sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR;
$img = wiImage::load($dir.'test.gif');
$mask = wiImage::load($dir.'m.gif');
$img->applyMask($mask, 0, 0)->saveToFile($dir.'mask.png');
return sfView::NONE;
}

/**
* 过滤器和格式转换
*
*/
public function executeFilter(sfWebRequest $request)
{
$dir = sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR;
$img = wiImage::load($dir.'test.gif');
$img->applyFilter(IMG_FILTER_GRAYSCALE, 111, 222, 111)->saveToFile($dir.'filter.png');
return sfView::NONE;
}

/**
* 缩放
*/
public function executeResize(sfWebRequest $request)
{
$dir = sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR;
$img = wiImage::load($dir.'test.jpg');
$img->resize(100, '30%', 'fill')->saveToFile($dir.'resizefill.jpg');
$img->resize(100, '30%', 'inside')->saveToFile($dir.'resizeinside.jpg');
$img->resize(100, '30%', 'outside')->saveToFile($dir.'resizeoutside.jpg');
return sfView::NONE;
}

/**
* 文字水印
*/
public function executeFont(sfWebRequest $request)
{
$dir = sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR;
$img = wiImage::load($dir.'test.jpg');
/*
在图片上输出中文的问题困扰了我很久,有些字体始终是乱码,这里使
用的是方正仿宋简体,可以正常输出UTF-8编码的中文简体字符串。
*/
$font = $dir.'fzfsjt.ttf';
$text = '你好';
$canvas = $img->getCanvas();
$canvas->setFont(new wiFont_TTF($font, 10, $img->allocateColor(0,0,255)));
// 参数 x y text angel
$canvas->writeText(60, 50, $text, 0);
$img->saveToFile($dir.'font.png');
return sfView::NONE;
}

更多的示例请查阅WideImage的主页(http://wideimage.sourceforge.net/)或者查看demos。

2009-03-26

如何在WINDOWS下设置SYMFONY1.2

类归于: symfony — 标签:, kthiz2006 @ 16:24

1. SVN SYMFONY
1. svn http://svn.symfony-project.com/branches/1.2
2. 设置PHP环境变量
1. 设置系统的 path 将 php.exe 的目录添加到path中,这样就可以用 php symfony 来运行命令
3. 安装pear
1. cmd 下运行  go-pear.bat
2. pear upgrade-all 将pear 升级到最新版
1. 过程中可能会报内存不足的错,自行修改php.ini 的设置 可以解决
4. 设置虚拟机
1. 需要打开rewrite功能
2.
DocumentRoot “d:/wamp/www/sftest/web”
ServerName sftest
alias /sf “d:/symfony1.2/data/web/sf/”

AllowOverride All
Allow from All


AllowOverride All
Allow from All


5. 设置php扩展
1. xsl 扩展需要打开
2. mysqlpdo需要打开
3. domxml扩展需要屏闭掉,否则会影响symfony解析xml
6. 以 php d:\symfony1.2\data\bin\symfony init-proj sftest 的形式 来使用命令行(因为symonfy文件命的不同,地址也会不同) 或将 symfony 命令所在目录设在PATH 里 就可以直接通过 symfony init-proj sftest 这样的办法执行命令了(这一办法在1.1的时候行不通,在1.2下测试通过.

2009-03-24

Add table prefix in symfony-在symfony中使用表前缀

类归于: symfony — 标签:, , , maker @ 16:51

The propel 1.3 is support table prefixes.

In your config/propel.ini add the following:

propel.tablePrefix=prefix_

then run the propel:build-all-load task.

Done!

网上找不到任何关于symfony或者propel支持表前缀的资料,只在propel1.3的修改历史中发现了1.3是支持表前缀的,但文档却没有更新相关内容,无奈之下只能查看源码,最后终于让我找到了。

补遗:还有一种方法我觉得是更加可靠的,那就是手动修改schema.yml中的全部数据库名,手动加前缀,同时强制设定表的phpName保证其不会随着表名一同变化,然后重新./symfony propel:build-all

2009-03-17

Symfony中的action和actions

类归于: symfony — 标签:zhuozi @ 16:10

Action Class with Two Actions, in frontend/modules/mymodule/actions/actions.class.php

class mymoduleActions extends sfActions
{
public function executeIndex()
{
// ...
}
public function executeList()
{
// ...
}
}

Single Action File, in frontend/modules/mymodule/actions/indexAction.class.php

class indexAction extends sfAction
{
public function execute($request)
{
// ...
}
}

Single Action File, in frontend/modules/mymodule/actions/listAction.class.php

class listAction extends sfAction
{
public function execute($request)
{
// ...
}
}

由于项目需要,需要多人同时针对一个module的相关操作进行修改,虽然是各修改各的,可是在合并的时候,如果采用acitons的方法,文件一定会出现很大的冲突,还好symfony可以定义单个的action,这样我们就可以有效的避免这个问题。

跳并快乐着-symfony跳转指南

类归于: symfony — 标签:, , , maker @ 12:24

项目接近尾声,闲来无事写一点东西活跃活跃气氛。在日常的web开发中,跳转是一个很常见的操作,成功了要跳转,错误了要跳转,满足某某条件也要跳转,symfony作为一款成熟的开发框架对跳转的处理也是非常多样的。

一. 关于跳转

虽说多样,但万变不离其宗,一般的跳转分为两种,forward(转发)和redirect(重定向),两种方法虽然目的都是为了进行跳转但是本质上还是有很大差别的。

所谓forward,是一个action将当前的请求交给另外一个action处理,这个过程是在同一个http请求中完成的,整个过程不涉及客户端,url地址也不会发生改变。

示例1

public function executeActionA($request)
{
// 一些处理
// to ActionB: 剩下的就交给你了
$this->forward('default', 'actionb');
}
public function executeActionB($request)
{
// to ActionA: 交给我你就放心吧
// 又一些处理
}

示例一中,ActionA将处理工作交给的ActionB,其实其效果是和下例一样的。

示例2

public function executeActionC($request)
{
// 一些处理
// 又一些处理
// ps : 我是全能,ActionA和ActionB能做的我自己就能做
}

上面这两个例子就如同我们雇佣一个程序员一个美工或者雇佣一个懂美工的程序员一样,我们将事件处理过程分开总是有各种各样的原因,比如为了逻辑清晰,代码重用等等等等,所以在很多情况下我们就需要使用forward。

说完forward我们再来说redirect,其实我觉得redirect才能够叫做真正的跳转,很多时候我觉得forward就象调用函数。一个真正的redirect涉及了两次http请求,浏览器发送了一个http请求之后,服务端根据业务需要进行跳转,这样服务端会向浏览器返回一个跳转信息,这个跳转信息可能是一个重定向的http header,也可能是一段带有跳转代码的html或者javascript,浏览器接到请求根据跳转信息进行第二次http 请求,这个第二次请求不同于forward的第二个Action,这个请求可以在程序内,也可以在程序外,比如说跳到google, 而用forward就象蹲监狱,再怎么跳也就是换换牢房,想出去是不可能了,除非你是Michael Scofield。

示例3

public function executeActionA($request)
{
// 我要换牢房
$this->redirect('fox/room');
}
public function executeActionB($request)
{
// 我要换去Sucre的牢房拆马桶
$this->redirect("fox/room?user='Fernando Sucre'");
}
public function executeActionC($request)
{
// 和哥哥越狱到谷歌去
$this->redirect('http://g.cn/?with_my_brother=true');
}

这里扯的有点远,其实我觉得在看我文章的朋友其实已经有一定的symfony基础的,所以不需要细细的去讲,只要给大家一些提示就可以。forward和redirect还有很关键的一点区别,就是forward进行转发之后原来的请求参数不会变,这里的请求参数指post, get, cookie什么什么什么的, 而redirect就需要你通过url将你需要的参数再传过去, symfony中貌似有http_build_query()这么一个函数.

示例4

public function executeActionA($request)
{
// 所有和我从Fox River跑出来的朋友一起去巴拿马
$this->redirect('panama.com?'.http_build_query($request->getPostParameters()));
}

二. symfony中的跳转

关于forward和redirect就介绍到这里,下面来介绍symfony中的跳转方法。

function forward($module, $action)

最基本的转发动作,参数是模块名和动作名。

示例5,新建立模块的Index方法都是这样的。

public function executeIndex(sfWebRequest $request)
{
// 将默认动作转发到默认模块
$this->forward('default', 'index');
}

function forward404($message = null)

forward404动作其实和throw new sfError404Exception的效果是一样的, 在symfony中可以通过设置404动作来控制404的输出,这样实际上就是一个转发到404错误处理动作的跳转, 返回404异常在程序中是很常见的,比如参数错误,数据错误等等等等等等,我们都可以直接抛出404异常。

示例6

public function executeActionA($request)
{
$id = (int) $request->getParameter('id');
if ($id < 1) {
$this->forward404();
}
$product = ProductPeer::retrieveByPk($id);
if (!is_object($product)) {
$this->forward404();
}
//我们将对象注册到模板中去,其实和$this->product=$product是一样的
$this->setVar('product', $product);
}

function forwardIf($condition, $module, $action)

当$condition为”真”的时候转发到$module/$action

示例7

public function executeCreate($request)
{
$product = new Product;
$product->setName('产品1');
// 创建产品,成功则跳转到列表,创建失败则抛出404
$this->forwardIf($product, 'product', 'list');
$this->forward404();
}

function forwardUnless($condition, $module, $action)

当$condition为”假”的时候转发到$module/$action

示例8

public function  executeAction($request)
{
$product = ProductPeer::retrieveByPk($this->getParameter('id'));
$this->forward($product, 'default', 'error404');
}

function forward404If($condition, $message = null)

当$condition为“真“的时候抛出404异常

示例9

public function executeShow($request)
{
$id = intval($request->getParameter('id'));
// 如果id小于0抛出异常
$this->forward404If($id<0, 'parameter error');
$product = ProductPeer::retrieveByPk($id);
// 如果产品不存在抛出异常
$this->forward404If(!$product, 'product not exists');
$this->object = $product;
}

function forward404Unless($condition, $message = null)

当$condition为“假“的时候抛出404异常, 实际应用中这个方法比forward404If更方便

示例10

public function executeShow($request)
{
$id = intval($request->getParameter('id'));
// 如果id小于0抛出异常
$this->forward404If($id<0, 'parameter error');
$product = ProductPeer::retrieveByPk($id);
// 如果产品不存在抛出异常
$this->forward404Unless($product, 'product not exists');
$this->object = $product;
}

function redirect($url, $statusCode = 302)

跳转到$url,返回http状态码,$url可以是内部url也可以是外部url.

示例11

public function executeActionA($request)
{
$this->redirect('g.cn');
}
public function executeActionB($request)
{
$this->redirect('default/index?action=b');
}

function redirect404()

跳转到404动作,404处理动作可以通过settings.yml进行设置, 这个动作是不用给参数的,symfony会自动调用系统设置。

function redirectIf($condition, $url, $statusCode = 302)

如果$condition为“真”则跳转到$url

示例12

public function executeCreate($request)
{
$product = new Product;
$product->setName('产品2');
//创建成功进入编辑页面
$this->redirectIf($product->save(), 'product/update?id='.$product->getId());
//创建失败加载Error模板
return sfView::ERROR;
}

function redirectUnless($condition, $url, $statusCode = 302)

如果$condition为“假”则跳转到$url, 参考forwardUnless

三. 结束语

本文旨在抛砖引玉,其实关于跳转还有很多的技巧,这个就要在实际应用中慢慢积累了。

« 较近文章早前文章 »

WordPress 所驱动