2010年2月23日星期二

股指期货开户今日9时启动 投资者须现场申请(转)

摘自:http://finance.qq.com/a/20100222/001894.htm?qq=0

股指期货正式交易可能在3月18日 门槛高多数股民只能"看热闹"

中国金融期货交 易所日前表示,2010年2月22日9时起,正式受理客户开立股指期货交易编码申请。这意味着,投资者期待已久的股指期货开户正式启动。而中国证监会20 日刚刚宣布,已正式批复中国金融期货交易所沪深300股指期货合约和业务规则,并将修订后的合约最低保证金从10%提高到12%,投机持仓限额从600手 大幅压缩到100手。

投资者:须亲临现场申请

期货公司为投资者办理手续必须临柜开户,禁止非现场开户。投资者暂时不能在各券商营业部办理期货交易账户的开办事宜。

据了解,目前,有124家期货公司可以开股指期货帐户,投资者可到这些期货公司营业部办理开户手续。股指期货开户没有手续费。投资者也可同时在多家期货公司开户。

对于投资者来说,开户并不意味着可以交易,据相关人士透露,正式交易可能将在3月18日。

股指期货交易佣金目前仍没有明确规定,而有业内人士透露,根据目前各方面的建议,股指期货的交易佣金标准大约为1手100元左右,具体各期货公司可能根据客户的交易量和资金量进行调整。

开户资格:须符合四大条件

根 据股指期货投资者适当性管理的相关办法等规定,投资者如果想要赶上首批股指期货开户,须满足多个条件。首先,投资者开户的资金门槛为50万元。其次,拟参 与股指期货交易的投资者需通过股指期货知识测试。据了解,该测试将由中金所提供考题,合格分数线为80分。第三,投资者必须具有累计10个交易日、20笔 以上的股指期货仿真交易记录,或者最近三年内具有10笔以上的商品期货交易成交记录。第四是投资者须进行一个反映其综合情况的评估,投资者必须在综合评估 中拿到70分以上,才能算作"合格"。

股民机会:多数只能"看热闹"

尽管股指期货进行得如火如荼,但实际能够满足开户条件的股民不足3%,多数股民只能"看热闹"。开户条件中最重要的一条是资金要达到50万,而仅这一条,就将多数股民挡在股指期货大门外。

中 登公司1月18日发布的数据显示,截至去年底,两市持有A股流通市值的自然人账户数占比高达99.83%,而自然人账户市值又以10万以下为主,持股市值 在10万元以下的个人投资者账户数量占比高达83.47%,另有13.76%的投资者持股市值在10万到50万元之间。也就是说,按持有A股流通市值数据 大概计算,能够达到50万元股指期货门槛的个人投资者不足3%,即不足151万户。

股指期货开闸 四类题材受益

期货市场与股票市场不同,其同时为券商股、大盘蓝筹股、间接参与股指期货股、封闭式基金带来了历史性拉动与套利机遇。

券商股无疑是其中最稳健的受益者,做空机制的适时退出一改券商业绩随大盘单边跌涨的难题,一方面可在牛市中继续做多,另一方面亦可在熊市中套期保值。首批试点中的券商股,例如海通证券光大证券中信证券招商证券等。

其次,战略性配置大盘蓝筹股。重点关注招商银行中国铁建中国石油中国石化中国神华等流通市值比较大的股票。

第三,间接参与股指期货股。这类股分两类:一类为控股期货的上市公司一类为本身是纯期货类的上市公司。如中国中期新黄浦

第四,封闭式基金。股指期货的推出将一举改变封基高折价率普遍存在的历史,投资者可以通过买入高折价率的封基,同时卖空股指期货,实现套利。 (中国企业家报)

"游戏规则"敲定

股指期货三大谜待解

20日,中金所公布了沪深300股指期货正式合约和交易规则以及系列配套文件。围绕股指期货上市的种种疑问,各界高度关注。

谜题一:

会否牺牲流动性?

最 低资本要求50万元、交易所保证金比例提高至12%、单张合约价值近百万、投机头寸限额100手……投资者参与股指期货市场,投资者自身不仅需要符合适当 性标准,而且与国外成熟市场相比,在合约设计以及交易制度等方面的一些条款亦提出了较高的要求,股指期货将风险的控制提高到了"前所未有"的高度。

分析人士指出,投资者需要注意的是,股指期货并不是大众理财产品,在国际市场上也鲜有投资股指期货"一夜暴富"的案例,其本身只是中性的风险管理工具,由于现货股票市场本身规模较大,严格风险控制下的股指期货上市后的流动性并不需要太多担心。

海证期货研究总监段世华表示,尽管从各方面看,股指期货的投资门槛均较高,但是随着市场功能的不断发挥,参与群体的逐步扩大,按照国际市场上股指期货交易规模4倍于商品期货的比例,股指期货的年交易额至少会达到400万亿元左右。

谜题二:

能否提升券商业绩?

股指期货筹备三年多来,即将推出的传闻不断,相关概念股也掀起一浪接一浪的上扬,市场普遍认为,业务较强的期货公司以及大型券商等将成为最大的受益者,从而推动了相关期货公司以及券商股价一路狂奔。

东 证期货总经理党剑指出,在短期内,股指期货将为大型券商带来利好可能并不成立。他指出,实际上在目前的市场状况下,在股市下跌时,包括基金等机构都将卖出 股票离场,券商也相应在投资者的卖出中获取经纪业务收入,但在股指期货推出后,如果股市下跌,包括基金的股票仓位将可以保持不变,转而在股指期货市场卖出 对冲。

不过党剑也指出,股指期货长期对券商仍具积极意义。党剑认为,股指期货将吸引更多资金的参与,同时也将激励券商参与自营,摆脱"靠天吃饭"的状况。

谜题三:

对股票市场影响几何?

作 为资本市场基础性制度建设一个划时代的事件,业内人士普遍认为,股指期货上市后,由于引入了做空机制,并且具备了期货市场的价格发现、套期保值以及资产配 置的基本功能,股市的换手率将会大大降低,但是新增资金涌入,将会推动沪深300股票的活跃度上升,尤其是大盘蓝筹股。

党 剑说,目前的市场没有做空机制,证券市场换手率最高曾经达到9倍,这在世界证券市场较为罕见,投资者只能在需要做空的时候抛售股票,但股指期货上市后,如 果需要做空市场,现货股票仓位就不需要变动,投资者转而在股指期货市场抛空,从而实现降低股市波动性的同时降低换手率。 (新华社)

开户需要准备的材料

(1)银行卡复印件或者扫描件1份。

(2)身份证扫描件(电子版)(如果是旧身份证,扫描正面;如果是新身份证,正反两面都扫描)。

(3)个人数码大头照(500万以上像素,整体上身尺寸占整个照片比例的60%),签合同时候的正面照。

(4)个人:客户本人的身份证原件,客户本人的银行卡或者存折。

(5) 法人:营业执照(正本复印件,副本原件、复印件)、税务登记证(复印件)、组织机构代码证( 原件、复印件)机构法定代表人身份证件原件或加盖机构公章、法定代表人名章的《法人授权委托书》及开户代理人的身份证件原件、银行开户许可证 ,机构授权的指令下单人、资金调拨人、结算单确认人的身份证件原件。

开户须知

投资者须现场申请

投资者可开户的期货公司一览

股指期货开户全程至少1.5小时 满足三大条件

交易规则

中金所就股指期货交易规则等答记者问

中金所发布股指期货合约、交易规则及其实施细则

期指知识

股指期货的交易流程

股指期货的投机、套保及套利

法律规定

中金所:诱骗买卖证券期货合约最高可判刑10年

2010年2月21日星期日

PHP字符串mbstring处理中文字符串的具体方法解析(转)

摘自:http://developer.51cto.com/art/200911/165332.htm

大家通过对PHP的学习,可以运用这一高级语言创建一个性能较高的网站。对于初学者来说,对于PHP字符串mbstring还是比较陌生的,下面我们就来介绍一下PHP字符串mbstring的具体应用。

多国语言并存就意味着多字节,PHP内置的字符串长度函数strlen无法正确处理中文字符串,它得到的只是字符串所占的字节数。对于GB2312的中文编码,strlen得到的值是汉字个数的2倍,而对于UTF-8编码的中文,就是1~3倍的差异了。

采用PHP字符串mbstring可以较好地解决这个问题。mb_strlen的用法和strlen类似,只不过它有第二个可选参数用于指定字符编 码。例如得到UTF-8的字符串$str长度,可以用mb_strlen($str,’UTF-8′)。如果省略第二个参数,则会使用PHP的内部编码。 内部编码可以通过mb_internal_encoding()函数得到,设置有两种方式:

1. 在php.ini中设置mbstring.internal_encoding = UTF-8
2. 调用mb_internal_encoding(”GBK”)

除了PHP字符串mbstring,还有很多切割函数,其中mb_substr是按字来切分字符,而mb_strcut是按字节来切分字符,但是都 不会产生半个字符的现象。而且从函数切割对长度的作用也不同,mb_strcut的切割条件是小于strlen, mb_substr是等于strlen,看下面的例子,

  1. < ?  
  2. $str = ‘我是一串比较长的中文-www.jefflei.com’;  
  3. echo “mb_substr:” . mb_substr($str, 0, 6, ‘utf-8′);  
  4. echo ”  
  5. “;  
  6. echo “mb_strcut:” . mb_strcut($str, 0, 6, ‘utf-8′);  
  7. ?> 
  8.  

输出如下:
mb_substr:我是一串比较
mb_strcut:我是

需要注意的是,PHP字符串mbstring并不是PHP核心函数,使用前需要确保在php编译模块时加入mbstring的支持:
(1)编译时使用–enable-mbstring
(2)修改/usr/local/lib/php.inc
default_charset = “zh-cn”
mbstring.language = zh-cn
mbstring.internal_encoding =zh-cn

PHP字符串mbstring类库内容比较多,还包括mb_ send_ mail 之类的email处理函数等

2010年2月9日星期二

还记得个性金条么?自制玩笑超级轰动新闻(转)

摘自:http://www.aspxba.com/show.aspx?id=166&cid=18

最近很多大型网站相继推出了一些玩笑性质的新闻,网友们纷纷转载这些新闻让自己的朋友也感受一下新年的惊喜(图1)。

还记得个性金条么?自制玩笑超级轰动新闻


   这些玩笑新闻能在开始时骗倒网友的原因是新闻中都含有一张图片,图片中有着网友的网名或者姓名,而且又是来自大型门户网站的新闻,所以能起到以假乱真的 效果。更有趣的是这些新闻都提供一个输入别人名字的输入框,只要输入朋友的名字,那么整个新闻从图片到文字部分都会依照输入的新名字发生变化(图2)。

还记得个性金条么?自制玩笑超级轰动新闻


  一、基本原理分析

   通常来说,要让网站中的文字根据输入框变化是网页最基本的功能之一,所以使用虚拟主机的个人站点站长可以轻松地做出这样的网页,但是对于图片中的文字内 容要根据输入框内容变化,则涉及到绘图的问题。一直以来给图片绘图使用最多的地方都是各种水印和需要即时生成图片的股票走势等应用,对于个人站点的站长, 绘图并不是一件容易的事。主要原因不是技术上的,而是可能碰到服务器不支持,或者没有安装某些插件,如果自己有主机的站长,可以考虑使用一些容易的命令行 绘图软件,如ImageMagick来进行这个操作,只需在程序脚本中嵌入:

  安装路径convert -font 字体 -fill 颜色 -pointsize 字体大小 -draw 'text 5,15 "文字内容"'

   这个语句就可以使用Image Magick自带的convert程序来进行水印操作,其中text 5,15是文字在图片上左上角开始计算的坐标。ImageMagick的功能非常强大,而且同时支持各种操作系统,所以无论是Windows主机还是 Unix类主机,甚至是MacOS等都可以使用。ImageMagick还是免费的开源软件,功能非常强大,甚至可以将它看作是一个具备命令行功能的 Photoshop。但是ImageMagick的安装,特别是在Unix类主机下的安装非常麻烦,与很多在Windows系统下ASP程序用来做图的模 块一样,它们并不是每一个虚拟主机都会安装的软件。

  二、常规解决方案

   比较综合的考虑,最常见的环境是PHP+GD支持的虚拟主机。即便是Windows主机也基本对PHP提供了支持,不像ASP在Linux环境只能用模拟 器来执行。GD库作图长久以来都是被PHP很好支持的,特别是在PHP4.3.0版后。如果默认安装输入了参数--with-gd,那么会从自带程序包里 面安装好GD库,无需复杂的手续。但是PHP对于多字节的支持就没有这么好,在4.3.X版后有一套Multi-Byte String Functions,需要在安装的时候加入--enable-mbstring=all等多个参数才能正常使用,一般PHP环境都没有提供对它的支持。假 设你已经安装了对它的支持,那么可以在PHP代码中使用如下语句进行GB2313编码到UTF-8编码的转换,当然它还可以用来转换韩文和日文。

  $str = mb_convert_encoding($str, "GB2313", "UTF-8");

  对于一个只有PHP和GD的环境,我们需要一个码表(GB2312.TXT)和一个自定义函数(gb2utf8)来完成这个转码的工作。然后使用GD库的绘制图片代码生成一个带有输入文字的图片,最主要的几行代码如下:

  header ("Content-type: image/pjpeg");
  $photoImage=Image CreateFromJpeg('imgs.jpg');
  ImageAlphaBlending($photoImage, true);
  $info = urlencode(rawurlencode($_GET[info]));
  $info = urldecode(rawurldecode($_GET[info]));
  $font="c:windows ontssimsun.ttc";
  $str=gb2utf8($info);
  $white = imagecolorallocate($photoImage, 255,255,255);
  $black = imagecolorallocate($photoImage, 50,50,50);
  ImageTTFText ($photoImage, 15, 7, 130, 220, $black, $font, $str);
  ImageJPEG($photoImage); // output to browser
  ImageDestroy($photoImage);

   以上代码需要说明的是imgs.jpg是用来绘制文字的原图,simsun.ttc是绘制中文时需要的TTF字库,代码中默认原图在程序所在的目录,而 simsun.ttc是采用了Windows系统自带的宋体字库。如果不是在这两个目录,那么都需要给出文件所在的绝对路径,Linux系统中如果没有中 文字库,可以将Windows的字库上传到服务器中使用,特别注意的是如果需要输出中文,那么一定需要中文TTF字库,英文字库是不行的,繁体中文也就需 要繁体中文的TTF字库。

  最重要的一行代码是:

  ImageTTFText ($photoImage, 15, 7, 130, 220, $black, $font, $str);
  它的函数原型是:
  array imagettftext ( resource image, int size, int angle, int x, int y, int color, string fontfile, string text)
  将字符串 text 画到 image 所代表的图像上,从坐标 x,y(左上角为 0, 0)开始,角度为 angle,颜色为 color,使用 fontfile 所指定的 TrueType 字体文件。

   对于一些想要加字的比较特殊的图片,比如竖立的金条上加字,那么角度值就非常重要,关于GD库函数更多的细节,可以查询PHP说明手册。同时这个生成图 片的PHP文件输出的是一张图,还无法直接调用,那么就需要另外一个PHP程序中对输入的文字内容进行处理,并使用<img src=http://www.qqread.com/aspdotnet/文件名?参数名=参数值>这个html代码来调用生成图片用的PHP文 件。最后还可以在这个PHP文件中加上其他一些文字内容,让页面变得更充实,多次让人名出现在文章中,可以更好的迷惑来访者。

  三、网页安全及注意事项

   最后值得一提的是安全问题,因为直接在URL中跟上参数是网页设计比较忌讳的问题,所以在PHP中应该使用系统预定的传输变量$_GET,而不是直接调 用变量本身。对于文字输入还需要一定的安全过滤,把一些敏感的关键字也进行屏蔽。在输出图片的PHP文件中,还需要加上一行:

  if(!strstr($HTTP_ REFERER,"另外一个PHP程序的URL地址")) die( "请勿盗链哦,^^" );

  这样这个文件就只能被输出用的程序调用,而不会被盗链,用GD库调用TTF文件来绘图是相当占用系统资源的工作,访问量太大会影响服务器的正常使用。
  知道了原理和方法,现在有条件的读者就可以借新春或情人节之际,给朋友一个让他惊喜的自制玩笑版超级轰动新闻了。

  提示:文中涉及的代码及相关示例文件可在http://www.pcdiget.com/download/下载
进入讨论组讨论。

很强的PHP图片处理类(转)

摘自:http://club.topsage.com/viewthread.php?tid=491690

  1. <?php

  2. /**
  3. *  基本图片处理,用于完成图片缩入,水印添加
  4. *  当水印图超过目标图片尺寸时,水印图能自动适应目标图片而缩小
  5. *  水印图可以设置跟背景的合并度
  6. *
  7. *  Copyright(c) 2005 by ustb99. All rights reserved
  8. *
  9. *  To contact the author write to {@link mailto:ustb80@163.com}
  10. *
  11. * @author 偶然
  12. * @version $Id: thumb.class.php,v 1.9 2006/09/30 09:31:56 zengjian Exp $
  13. * @package system
  14. */

  15. /**
  16. * ThumbHandler
  17. * @access public
  18. */

  19. /*
  20. 使用方法:
  21.     自动裁切:
  22.     程序会按照图片的尺寸从中部裁切最大的正方形,并按目标尺寸进行缩略

  23.     $t->setSrcImg("img/test.jpg");
  24.     $t->setCutType(1);//这一句就OK了
  25.     $t->setDstImg("tmp/new_test.jpg");
  26.     $t->createImg(60,60);

  27.     手工裁切:
  28.     程序会按照指定的位置从源图上取图

  29.     $t->setSrcImg("img/test.jpg");
  30.     $t->setCutType(2);//指明为手工裁切
  31.     $t->setSrcCutPosition(100, 100);// 源图起点坐标
  32.     $t->setRectangleCut(300, 200);// 裁切尺寸
  33.     $t->setDstImg("tmp/new_test.jpg");
  34.     $t->createImg(300,200);
  35. */
  36. class ThumbHandler
  37. {
  38.     var $dst_img;// 目标文件
  39.     var $h_src; // 图片资源句柄
  40.     var $h_dst;// 新图句柄
  41.     var $h_mask;// 水印句柄
  42.     var $img_create_quality = 100;// 图片生成质量
  43.     var $img_display_quality = 80;// 图片显示质量,默认为75
  44.     var $img_scale = 0;// 图片缩放比例
  45.     var $src_w = 0;// 原图宽度
  46.     var $src_h = 0;// 原图高度
  47.     var $dst_w = 0;// 新图总宽度
  48.     var $dst_h = 0;// 新图总高度
  49.     var $fill_w;// 填充图形宽
  50.     var $fill_h;// 填充图形高
  51.     var $copy_w;// 拷贝图形宽
  52.     var $copy_h;// 拷贝图形高
  53.     var $src_x = 0;// 原图绘制起始横坐标
  54.     var $src_y = 0;// 原图绘制起始纵坐标
  55.     var $start_x;// 新图绘制起始横坐标
  56.     var $start_y;// 新图绘制起始纵坐标
  57.     var $mask_word;// 水印文字
  58.     var $mask_img;// 水印图片
  59.     var $mask_pos_x = 0;// 水印横坐标
  60.     var $mask_pos_y = 0;// 水印纵坐标
  61.     var $mask_offset_x = 5;// 水印横向偏移
  62.     var $mask_offset_y = 5;// 水印纵向偏移
  63.     var $font_w;// 水印字体宽
  64.     var $font_h;// 水印字体高
  65.     var $mask_w;// 水印宽
  66.     var $mask_h;// 水印高
  67.     var $mask_font_color = "#ffffff";// 水印文字颜色
  68.     var $mask_font = 2;// 水印字体
  69.     var $font_size;// 尺寸
  70.     var $mask_position = 0;// 水印位置
  71.     var $mask_img_pct = 50;// 图片合并程度,值越大,合并程序越低
  72.     var $mask_txt_pct = 50;// 文字合并程度,值越小,合并程序越低
  73.     var $img_border_size = 0;// 图片边框尺寸
  74.     var $img_border_color;// 图片边框颜色
  75.     var $_flip_x=0;// 水平翻转次数
  76.     var $_flip_y=0;// 垂直翻转次数

  77.     var $cut_type=0;// 剪切类型


  78.     var $img_type;// 文件类型

  79.     // 文件类型定义,并指出了输出图片的函数
  80.     var $all_type = array(
  81.         "jpg"  => array("output"=>"imagejpeg"),
  82.         "gif"  => array("output"=>"imagegif"),
  83.         "png"  => array("output"=>"imagepng"),
  84.         "wbmp" => array("output"=>"image2wbmp"),
  85.         "jpeg" => array("output"=>"imagejpeg"));

  86.     /**
  87.      * 构造函数
  88.      */
  89.     function ThumbHandler()
  90.     {
  91.         $this->mask_font_color = "#ffffff";
  92.         $this->font = 2;
  93.         $this->font_size = 12;
  94.     }

  95.     /**
  96.      * 取得图片的宽
  97.      */
  98.     function getImgWidth($src)
  99.     {
  100.         return imagesx($src);
  101.     }

  102.     /**
  103.      * 取得图片的高
  104.      */
  105.     function getImgHeight($src)
  106.     {
  107.         return imagesy($src);
  108.     }

  109.     /**
  110.      * 设置图片生成路径
  111.      *
  112.      * @param    string    $src_img   图片生成路径
  113.      */
  114.     function setSrcImg($src_img, $img_type=null)
  115.     {
  116.         if(!file_exists($src_img))
  117.         {
  118.             die("图片不存在");
  119.         }
  120.         
  121.         if(!empty($img_type))
  122.         {
  123.             $this->img_type = $img_type;
  124.         }
  125.         else
  126.         {
  127.             $this->img_type = $this->_getImgType($src_img);
  128.         }
  129.         
  130.         $this->_checkValid($this->img_type);

  131.         // file_get_contents函数要求php版本>4.3.0
  132.         $src = '';
  133.         if(function_exists("file_get_contents"))
  134.         {
  135.             $src = file_get_contents($src_img);
  136.         }
  137.         else
  138.         {
  139.             $handle = fopen ($src_img, "r");
  140.             while (!feof ($handle))
  141.             {
  142.                 $src .= fgets($fd, 4096);
  143.             }
  144.             fclose ($handle);
  145.         }
  146.         if(empty($src))
  147.         {
  148.             die("图片源为空");
  149.         }
  150.         $this->h_src = @ImageCreateFromString($src);
  151.         $this->src_w = $this->getImgWidth($this->h_src);
  152.         $this->src_h = $this->getImgHeight($this->h_src);
  153.     }

  154.     /**
  155.      * 设置图片生成路径
  156.      *
  157.      * @param    string    $dst_img   图片生成路径
  158.      */
  159.     function setDstImg($dst_img)
  160.     {
  161.         $arr  = explode('/',$dst_img);
  162.         $last = array_pop($arr);
  163.         $path = implode('/',$arr);
  164.         $this->_mkdirs($path);
  165.         $this->dst_img = $dst_img;
  166.     }

  167.     /**
  168.      * 设置图片的显示质量
  169.      *
  170.      * @param    string      $n    质量
  171.      */
  172.     function setImgDisplayQuality($n)
  173.     {
  174.         $this->img_display_quality = (int)$n;
  175.     }

  176.     /**
  177.      * 设置图片的生成质量
  178.      *
  179.      * @param    string      $n    质量
  180.      */
  181.     function setImgCreateQuality($n)
  182.     {
  183.         $this->img_create_quality = (int)$n;
  184.     }

  185.     /**
  186.      * 设置文字水印
  187.      *
  188.      * @param    string     $word    水印文字
  189.      * @param    integer    $font    水印字体
  190.      * @param    string     $color   水印字体颜色
  191.      */
  192.     function setMaskWord($word)
  193.     {
  194.         $this->mask_word .= $word;
  195.     }

  196.     /**
  197.      * 设置字体颜色
  198.      *
  199.      * @param    string     $color    字体颜色
  200.      */
  201.     function setMaskFontColor($color="#ffffff")
  202.     {
  203.         $this->mask_font_color = $color;
  204.     }

  205.     /**
  206.      * 设置水印字体
  207.      *
  208.      * @param    string|integer    $font    字体
  209.      */
  210.     function setMaskFont($font=2)
  211.     {
  212.         if(!is_numeric($font) && !file_exists($font))
  213.         {
  214.             die("字体文件不存在");
  215.         }
  216.         $this->font = $font;
  217.     }

  218.     /**
  219.      * 设置文字字体大小,仅对truetype字体有效
  220.      */
  221.     function setMaskFontSize($size = "12")
  222.     {
  223.         $this->font_size = $size;
  224.     }

  225.     /**
  226.      * 设置图片水印
  227.      *
  228.      * @param    string    $img     水印图片源
  229.      */
  230.     function setMaskImg($img)
  231.     {
  232.         $this->mask_img = $img;
  233.     }

  234.     /**
  235.      * 设置水印横向偏移
  236.      *
  237.      * @param    integer     $x    横向偏移量
  238.      */
  239.     function setMaskOffsetX($x)
  240.     {
  241.         $this->mask_offset_x = (int)$x;
  242.     }

  243.     /**
  244.      * 设置水印纵向偏移
  245.      *
  246.      * @param    integer     $y    纵向偏移量
  247.      */
  248.     function setMaskOffsetY($y)
  249.     {
  250.         $this->mask_offset_y = (int)$y;
  251.     }

  252.     /**
  253.      * 指定水印位置
  254.      *
  255.      * @param    integer     $position    位置,1:左上,2:左下,3:右上,0/4:右下
  256.      */
  257.     function setMaskPosition($position=0)
  258.     {
  259.         $this->mask_position = (int)$position;
  260.     }

  261.     /**
  262.      * 设置图片合并程度
  263.      *
  264.      * @param    integer     $n    合并程度
  265.      */
  266.     function setMaskImgPct($n)
  267.     {
  268.         $this->mask_img_pct = (int)$n;
  269.     }

  270.     /**
  271.      * 设置文字合并程度
  272.      *
  273.      * @param    integer     $n    合并程度
  274.      */
  275.     function setMaskTxtPct($n)
  276.     {
  277.         $this->mask_txt_pct = (int)$n;
  278.     }

  279.     /**
  280.      * 设置缩略图边框
  281.      *
  282.      * @param    (类型)     (参数名)    (描述)
  283.      */
  284.     function setDstImgBorder($size=1, $color="#000000")
  285.     {
  286.         $this->img_border_size  = (int)$size;
  287.         $this->img_border_color = $color;
  288.     }

  289.     /**
  290.      * 水平翻转
  291.      */
  292.     function flipH()
  293.     {
  294.         $this->_flip_x++;
  295.     }

  296.     /**
  297.      * 垂直翻转
  298.      */
  299.     function flipV()
  300.     {
  301.         $this->_flip_y++;
  302.     }

  303.     /**
  304.      * 设置剪切类型
  305.      *
  306.      * @param    (类型)     (参数名)    (描述)
  307.      */
  308.     function setCutType($type)
  309.     {
  310.         $this->cut_type = (int)$type;
  311.     }

  312.     /**
  313.      * 设置图片剪切
  314.      *
  315.      * @param    integer     $width    矩形剪切
  316.      */
  317.     function setRectangleCut($width, $height)
  318.     {
  319.         $this->fill_w = (int)$width;
  320.         $this->fill_h = (int)$height;
  321.     }

  322.     /**
  323.      * 设置源图剪切起始坐标点
  324.      *
  325.      * @param    (类型)     (参数名)    (描述)
  326.      */
  327.     function setSrcCutPosition($x, $y)
  328.     {
  329.         $this->src_x  = (int)$x;
  330.         $this->src_y  = (int)$y;
  331.     }

  332.     /**
  333.      * 创建图片,主函数
  334.      * @param    integer    $a     当缺少第二个参数时,此参数将用作百分比,
  335.      *                             否则作为宽度值
  336.      * @param    integer    $b     图片缩放后的高度
  337.      */
  338.     function createImg($a, $b=null)
  339.     {
  340.         $num = func_num_args();
  341.         if(1 == $num)
  342.         {
  343.             $r = (int)$a;
  344.             if($r < 1)
  345.             {
  346.                 die("图片缩放比例不得小于1");
  347.             }
  348.             $this->img_scale = $r;
  349.             $this->_setNewImgSize($r);
  350.         }

  351.         if(2 == $num)
  352.         {
  353.             $w = (int)$a;
  354.             $h = (int)$b;
  355.             if(0 == $w)
  356.             {
  357.                 die("目标宽度不能为0");
  358.             }
  359.             if(0 == $h)
  360.             {
  361.                 die("目标高度不能为0");
  362.             }
  363.             $this->_setNewImgSize($w, $h);
  364.         }

  365.         if($this->_flip_x%2!=0)
  366.         {
  367.             $this->_flipH($this->h_src);
  368.         }

  369.         if($this->_flip_y%2!=0)
  370.         {
  371.             $this->_flipV($this->h_src);
  372.         }
  373.         $this->_createMask();
  374.         $this->_output();

  375.         // 释放
  376.         if(imagedestroy($this->h_src) && imagedestroy($this->h_dst))
  377.         {
  378.             Return true;
  379.         }
  380.         else
  381.         {
  382.             Return false;
  383.         }
  384.     }

  385.     /**
  386.      * 生成水印,调用了生成水印文字和水印图片两个方法
  387.      */
  388.     function _createMask()
  389.     {
  390.         if($this->mask_word)
  391.         {
  392.             // 获取字体信息
  393.             $this->_setFontInfo();

  394.             if($this->_isFull())
  395.             {
  396.                 die("水印文字过大");
  397.             }
  398.             else
  399.             {
  400.                 $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  401.                 $white = ImageColorAllocate($this->h_dst,255,255,255);
  402.                 imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  403.                 $this->_drawBorder();
  404.                 imagecopyresampled( $this->h_dst, $this->h_src,
  405.                                     $this->start_x, $this->start_y,
  406.                                     $this->src_x, $this->src_y,
  407.                                     $this->fill_w, $this->fill_h,
  408.                                     $this->copy_w, $this->copy_h);
  409.                 $this->_createMaskWord($this->h_dst);
  410.             }
  411.         }

  412.         if($this->mask_img)
  413.         {
  414.             $this->_loadMaskImg();//加载时,取得宽高

  415.             if($this->_isFull())
  416.             {
  417.                 // 将水印生成在原图上再拷
  418.                 $this->_createMaskImg($this->h_src);
  419.                 $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  420.                 $white = ImageColorAllocate($this->h_dst,255,255,255);
  421.                 imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  422.                 $this->_drawBorder();
  423.                 imagecopyresampled( $this->h_dst, $this->h_src,
  424.                                     $this->start_x, $this->start_y,
  425.                                     $this->src_x, $this->src_y,
  426.                                     $this->fill_w, $this->start_y,
  427.                                     $this->copy_w, $this->copy_h);
  428.             }
  429.             else
  430.             {
  431.                 // 创建新图并拷贝
  432.                 $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  433.                 $white = ImageColorAllocate($this->h_dst,255,255,255);
  434.                 imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  435.                 $this->_drawBorder();
  436.                 imagecopyresampled( $this->h_dst, $this->h_src,
  437.                                     $this->start_x, $this->start_y,
  438.                                     $this->src_x, $this->src_y,
  439.                                     $this->fill_w, $this->fill_h,
  440.                                     $this->copy_w, $this->copy_h);
  441.                 $this->_createMaskImg($this->h_dst);
  442.             }
  443.         }

  444.         if(empty($this->mask_word) && empty($this->mask_img))
  445.         {
  446.             $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  447.             $white = ImageColorAllocate($this->h_dst,255,255,255);
  448.             imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  449.             $this->_drawBorder();

  450.             imagecopyresampled( $this->h_dst, $this->h_src,
  451.                         $this->start_x, $this->start_y,
  452.                         $this->src_x, $this->src_y,
  453.                         $this->fill_w, $this->fill_h,
  454.                         $this->copy_w, $this->copy_h);
  455.         }
  456.     }

  457.     /**
  458.      * 画边框
  459.      */
  460.     function _drawBorder()
  461.     {
  462.         if(!empty($this->img_border_size))
  463.         {
  464.             $c = $this->_parseColor($this->img_border_color);
  465.             $color = ImageColorAllocate($this->h_src,$c[0], $c[1], $c[2]);
  466.             imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$color);// 填充背景色
  467.         }
  468.     }

  469.     /**
  470.      * 生成水印文字
  471.      */
  472.     function _createMaskWord($src)
  473.     {
  474.         $this->_countMaskPos();
  475.         $this->_checkMaskValid();

  476.         $c = $this->_parseColor($this->mask_font_color);
  477.         $color = imagecolorallocatealpha($src, $c[0], $c[1], $c[2], $this->mask_txt_pct);

  478.         if(is_numeric($this->font))
  479.         {
  480.             imagestring($src,
  481.                         $this->font,
  482.                         $this->mask_pos_x, $this->mask_pos_y,
  483.                         $this->mask_word,
  484.                         $color);
  485.         }
  486.         else
  487.         {
  488.             imagettftext($src,
  489.                         $this->font_size, 0,
  490.                         $this->mask_pos_x, $this->mask_pos_y,
  491.                         $color,
  492.                         $this->font,
  493.                         $this->mask_word);
  494.         }
  495.     }

  496.     /**
  497.      * 生成水印图
  498.      */
  499.     function _createMaskImg($src)
  500.     {
  501.         $this->_countMaskPos();
  502.         $this->_checkMaskValid();
  503.         imagecopymerge($src,
  504.                         $this->h_mask,
  505.                         $this->mask_pos_x ,$this->mask_pos_y,
  506.                         0, 0,
  507.                         $this->mask_w, $this->mask_h,
  508.                         $this->mask_img_pct);

  509.         imagedestroy($this->h_mask);
  510.     }

  511.     /**
  512.      * 加载水印图
  513.      */
  514.     function _loadMaskImg()
  515.     {
  516.         $mask_type = $this->_getImgType($this->mask_img);
  517.         $this->_checkValid($mask_type);

  518.         // file_get_contents函数要求php版本>4.3.0
  519.         $src = '';
  520.         if(function_exists("file_get_contents"))
  521.         {
  522.             $src = file_get_contents($this->mask_img);
  523.         }
  524.         else
  525.         {
  526.             $handle = fopen ($this->mask_img, "r");
  527.             while (!feof ($handle))
  528.             {
  529.                 $src .= fgets($fd, 4096);
  530.             }
  531.             fclose ($handle);
  532.         }
  533.         if(empty($this->mask_img))
  534.         {
  535.             die("水印图片为空");
  536.         }
  537.         $this->h_mask = ImageCreateFromString($src);
  538.         $this->mask_w = $this->getImgWidth($this->h_mask);
  539.         $this->mask_h = $this->getImgHeight($this->h_mask);
  540.     }

  541.     /**
  542.      * 图片输出
  543.      */
  544.     function _output()
  545.     {
  546.         $img_type  = $this->img_type;
  547.         $func_name = $this->all_type[$img_type]['output'];
  548.         if(function_exists($func_name))
  549.         {
  550.             // 判断浏览器,若是IE就不发送头
  551.             if(isset($_SERVER['HTTP_USER_AGENT']))
  552.             {
  553.                 $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);
  554.                 if(!preg_match('/^.*MSIE.*\)$/i',$ua))
  555.                 {
  556.                     header("Content-type:$img_type");
  557.                 }
  558.             }
  559.             $func_name($this->h_dst, $this->dst_img, $this->img_display_quality);
  560.         }
  561.         else
  562.         {
  563.             Return false;
  564.         }
  565.     }

  566.     /**
  567.      * 分析颜色
  568.      *
  569.      * @param    string     $color    十六进制颜色
  570.      */
  571.     function _parseColor($color)
  572.     {
  573.         $arr = array();
  574.         for($ii=1; $ii<strlen ($color); $ii++)
  575.         {
  576.             $arr[] = hexdec(substr($color,$ii,2));
  577.             $ii++;
  578.         }

  579.         Return $arr;
  580.     }

  581.     /**
  582.      * 计算出位置坐标
  583.      */
  584.     function _countMaskPos()
  585.     {
  586.         if($this->_isFull())
  587.         {
  588.             switch($this->mask_position)
  589.             {
  590.                 case 1:
  591.                     // 左上
  592.                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
  593.                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
  594.                     break;

  595.                 case 2:
  596.                     // 左下
  597.                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
  598.                     $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
  599.                     break;

  600.                 case 3:
  601.                     // 右上
  602.                     $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
  603.                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
  604.                     break;

  605.                 case 4:
  606.                     // 右下
  607.                     $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
  608.                     $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
  609.                     break;

  610.                 default:
  611.                     // 默认将水印放到右下,偏移指定像素
  612.                     $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
  613.                     $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
  614.                     break;
  615.             }
  616.         }
  617.         else
  618.         {
  619.             switch($this->mask_position)
  620.             {
  621.                 case 1:
  622.                     // 左上
  623.                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
  624.                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
  625.                     break;

  626.                 case 2:
  627.                     // 左下
  628.                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
  629.                     $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
  630.                     break;

  631.                 case 3:
  632.                     // 右上
  633.                     $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
  634.                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
  635.                     break;

  636.                 case 4:
  637.                     // 右下
  638.                     $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
  639.                     $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
  640.                     break;

  641.                 default:
  642.                     // 默认将水印放到右下,偏移指定像素
  643.                     $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
  644.                     $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
  645.                     break;
  646.             }
  647.         }
  648.     }

  649.     /**
  650.      * 设置字体信息
  651.      */
  652.     function _setFontInfo()
  653.     {
  654.         if(is_numeric($this->font))
  655.         {
  656.             $this->font_w  = imagefontwidth($this->font);
  657.             $this->font_h  = imagefontheight($this->font);

  658.             // 计算水印字体所占宽高
  659.             $word_length   = strlen($this->mask_word);
  660.             $this->mask_w  = $this->font_w*$word_length;
  661.             $this->mask_h  = $this->font_h;
  662.         }
  663.         else
  664.         {
  665.             $arr = imagettfbbox ($this->font_size,0, $this->font,$this->mask_word);
  666.             $this->mask_w  = abs($arr[0] - $arr[2]);
  667.             $this->mask_h  = abs($arr[7] - $arr[1]);
  668.         }
  669.     }

  670.     /**
  671.      * 设置新图尺寸
  672.      *
  673.      * @param    integer     $img_w   目标宽度
  674.      * @param    integer     $img_h   目标高度
  675.      */
  676.     function _setNewImgSize($img_w, $img_h=null)
  677.     {
  678.         $num = func_num_args();
  679.         if(1 == $num)
  680.         {
  681.             $this->img_scale = $img_w;// 宽度作为比例
  682.             $this->fill_w = round($this->src_w * $this->img_scale / 100) - $this->img_border_size*2;
  683.             $this->fill_h = round($this->src_h * $this->img_scale / 100) - $this->img_border_size*2;

  684.             // 源文件起始坐标
  685.             $this->src_x  = 0;
  686.             $this->src_y  = 0;
  687.             $this->copy_w = $this->src_w;
  688.             $this->copy_h = $this->src_h;

  689.             // 目标尺寸
  690.             $this->dst_w   = $this->fill_w + $this->img_border_size*2;
  691.             $this->dst_h   = $this->fill_h + $this->img_border_size*2;
  692.         }

  693.         if(2 == $num)
  694.         {
  695.             $fill_w   = (int)$img_w - $this->img_border_size*2;
  696.             $fill_h   = (int)$img_h - $this->img_border_size*2;
  697.             if($fill_w < 0 || $fill_h < 0)
  698.             {
  699.                 die("图片边框过大,已超过了图片的宽度");
  700.             }
  701.             $rate_w = $this->src_w/$fill_w;
  702.             $rate_h = $this->src_h/$fill_h;

  703.             switch($this->cut_type)
  704.             {
  705.                 case 0:
  706.                     // 如果原图大于缩略图,产生缩小,否则不缩小
  707.                     if($rate_w < 1 && $rate_h < 1)
  708.                     {
  709.                         $this->fill_w = (int)$this->src_w;
  710.                         $this->fill_h = (int)$this->src_h;
  711.                     }
  712.                     else
  713.                     {
  714.                         if($rate_w >= $rate_h)
  715.                         {
  716.                             $this->fill_w = (int)$fill_w;
  717.                             $this->fill_h = round($this->src_h/$rate_w);
  718.                         }
  719.                         else
  720.                         {
  721.                             $this->fill_w = round($this->src_w/$rate_h);
  722.                             $this->fill_h = (int)$fill_h;
  723.                         }
  724.                     }

  725.                     $this->src_x  = 0;
  726.                     $this->src_y  = 0;

  727.                     $this->copy_w = $this->src_w;
  728.                     $this->copy_h = $this->src_h;

  729.                     // 目标尺寸
  730.                     $this->dst_w   = $this->fill_w + $this->img_border_size*2;
  731.                     $this->dst_h   = $this->fill_h + $this->img_border_size*2;
  732.                     break;

  733.                 // 自动裁切
  734.                 case 1:
  735.                     // 如果图片是缩小剪切才进行操作
  736.                     if($rate_w >= 1 && $rate_h >=1)
  737.                     {
  738.                         if($this->src_w > $this->src_h)
  739.                         {
  740.                             $src_x = round($this->src_w-$this->src_h)/2;
  741.                             $this->setSrcCutPosition($src_x, 0);
  742.                             $this->setRectangleCut($fill_h, $fill_h);

  743.                             $this->copy_w = $this->src_h;
  744.                             $this->copy_h = $this->src_h;
  745.                            
  746.                         }
  747.                         elseif($this->src_w < $this->src_h)
  748.                         {
  749.                             $src_y = round($this->src_h-$this->src_w)/2;
  750.                             $this->setSrcCutPosition(0, $src_y);
  751.                             $this->setRectangleCut($fill_w, $fill_h);

  752.                             $this->copy_w = $this->src_w;
  753.                             $this->copy_h = $this->src_w;
  754.                         }
  755.                         else
  756.                         {
  757.                             $this->setSrcCutPosition(0, 0);
  758.                             $this->copy_w = $this->src_w;
  759.                             $this->copy_h = $this->src_w;
  760.                             $this->setRectangleCut($fill_w, $fill_h);
  761.                         }
  762.                     }
  763.                     else
  764.                     {
  765.                         $this->setSrcCutPosition(0, 0);
  766.                         $this->setRectangleCut($this->src_w, $this->src_h);

  767.                         $this->copy_w = $this->src_w;
  768.                         $this->copy_h = $this->src_h;
  769.                     }

  770.                     // 目标尺寸
  771.                     $this->dst_w   = $this->fill_w + $this->img_border_size*2;
  772.                     $this->dst_h   = $this->fill_h + $this->img_border_size*2;
  773.                     
  774.                     break;

  775.                 // 手工裁切
  776.                 case 2:
  777.                     $this->copy_w = $this->fill_w;
  778.                     $this->copy_h = $this->fill_h;

  779.                     // 目标尺寸
  780.                     $this->dst_w   = $this->fill_w + $this->img_border_size*2;
  781.                     $this->dst_h   = $this->fill_h + $this->img_border_size*2;               
  782.                     
  783.                     break;
  784.                 default:
  785.                     break;

  786.             }
  787.         }

  788.         // 目标文件起始坐标
  789.         $this->start_x = $this->img_border_size;
  790.         $this->start_y = $this->img_border_size;
  791.     }

  792.     /**
  793.      * 检查水印图是否大于生成后的图片宽高
  794.      */
  795.     function _isFull()
  796.     {
  797.         Return (   $this->mask_w + $this->mask_offset_x > $this->fill_w
  798.                 || $this->mask_h + $this->mask_offset_y > $this->fill_h)
  799.                    ?true:false;
  800.     }

  801.     /**
  802.      * 检查水印图是否超过原图
  803.      */
  804.     function _checkMaskValid()
  805.     {
  806.         if(    $this->mask_w + $this->mask_offset_x > $this->src_w
  807.             || $this->mask_h + $this->mask_offset_y > $this->src_h)
  808.         {
  809.             die("水印图片尺寸大于原图,请缩小水印图");
  810.         }
  811.     }

  812.     /**
  813.      * 取得图片类型
  814.      *
  815.      * @param    string     $file_path    文件路径
  816.      */
  817.     function _getImgType($file_path)
  818.     {
  819.         $type_list = array("1"=>"gif","2"=>"jpg","3"=>"png","4"=>"swf","5" => "psd","6"=>"bmp","15"=>"wbmp");
  820.         if(file_exists($file_path))
  821.         {
  822.             $img_info = @getimagesize ($file_path);
  823.             if(isset($type_list[$img_info[2]]))
  824.             {
  825.                 Return $type_list[$img_info[2]];
  826.             }
  827.         }
  828.         else
  829.         {
  830.             die("文件不存在,不能取得文件类型!");
  831.         }
  832.     }

  833.     /**
  834.      * 检查图片类型是否合法,调用了array_key_exists函数,此函数要求
  835.      * php版本大于4.1.0
  836.      *
  837.      * @param    string     $img_type    文件类型
  838.      */
  839.     function _checkValid($img_type)
  840.     {
  841.         if(!array_key_exists($img_type, $this->all_type))
  842.         {
  843.             Return false;
  844.         }
  845.     }

  846.     /**
  847.      * 按指定路径生成目录
  848.      *
  849.      * @param    string     $path    路径
  850.      */
  851.     function _mkdirs($path)
  852.     {
  853.         $adir = explode('/',$path);
  854.         $dirlist = '';
  855.         $rootdir = array_shift($adir);
  856.         if(($rootdir!='.'||$rootdir!='..')&&!file_exists($rootdir))
  857.         {
  858.             @mkdir($rootdir);
  859.         }
  860.         foreach($adir as $key=>$val)
  861.         {
  862.             if($val!='.'&&$val!='..')
  863.             {
  864.                 $dirlist .= "/".$val;
  865.                 $dirpath = $rootdir.$dirlist;
  866.                 if(!file_exists($dirpath))
  867.                 {
  868.                     @mkdir($dirpath);
  869.                     @chmod($dirpath,0777);
  870.                 }
  871.             }
  872.         }
  873.     }

  874.     /**
  875.      * 垂直翻转
  876.      *
  877.      * @param    string     $src    图片源
  878.      */
  879.     function _flipV($src)
  880.     {
  881.         $src_x = $this->getImgWidth($src);
  882.         $src_y = $this->getImgHeight($src);

  883.         $new_im = imagecreatetruecolor($src_x, $src_y);
  884.         for ($y = 0; $y < $src_y; $y++)
  885.         {
  886.             imagecopy($new_im, $src, 0, $src_y - $y - 1, 0, $y, $src_x, 1);
  887.         }
  888.         $this->h_src = $new_im;
  889.     }

  890.     /**
  891.      * 水平翻转
  892.      *
  893.      * @param    string     $src    图片源
  894.      */
  895.     function _flipH($src)
  896.     {
  897.         $src_x = $this->getImgWidth($src);
  898.         $src_y = $this->getImgHeight($src);

  899.         $new_im = imagecreatetruecolor($src_x, $src_y);
  900.         for ($x = 0; $x < $src_x; $x++)
  901.         {
  902.             imagecopy($new_im, $src, $src_x - $x - 1, 0, $x, 0, 1, $src_y);
  903.         }
  904.         $this->h_src = $new_im;
  905.     }
  906. }
复制代码

--------------

使用实例:
  1. <?php
  2. require_once('lib/thumb.class.php');

  3. $t = new ThumbHandler();

  4. $t->setSrcImg("img/test.jpg");
  5. $t->setDstImg("tmp/new_test.jpg");
  6. $t->setMaskImg("img/test.gif");
  7. $t->setMaskPosition(1);
  8. $t->setMaskImgPct(80);
  9. $t->setDstImgBorder(4,"#dddddd");

  10. // 指定缩放比例
  11. $t->createImg(300,200);
  12. ?>
复制代码
  1. <?php
  2. require_once('lib/thumb.class.php');

  3. $t = new ThumbHandler();

  4. // 基本使用
  5. $t->setSrcImg("img/test.jpg");
  6. $t->setMaskWord("test");
  7. $t->setDstImgBorder(10,"#dddddd");

  8. // 指定缩放比例
  9. $t->createImg(50);
  10. ?>
复制代码
  1. <?php
  2. equire_once('lib/thumb.class.php');

  3. $t = new ThumbHandler();

  4. // 基本使用
  5. $t->setSrcImg("img/test.jpg");
  6. $t->setMaskWord("test");

  7. // 指定固定宽高
  8. $t->createImg(200,200);
  9. ?>
复制代码
  1. <?php
  2. require_once('lib/thumb.class.php');

  3. $t = new ThumbHandler();

  4. $t->setSrcImg("img/test.jpg");
  5. $t->setDstImg("tmp/new_test.jpg");
  6. $t->setMaskWord("test");

  7. // 指定固定宽高
  8. $t->createImg(200,200);
  9. ?>
复制代码
  1. <?php
  2. require_once('lib/thumb.class.php');

  3. $t = new ThumbHandler();

  4. $t->setSrcImg("img/test.jpg");

  5. // 指定字体文件地址
  6. $t->setMaskFont("c:/winnt/fonts/arial.ttf");
  7. $t->setMaskFontSize(20);
  8. $t->setMaskFontColor("#ffff00");
  9. $t->setMaskWord("test3333333");
  10. $t->setDstImgBorder(99,"#dddddd");
  11. $t->createImg(50);
  12. ?>
复制代码
  1. <?php
  2. require_once('lib/thumb.class.php');

  3. $t = new ThumbHandler();

  4. $t->setSrcImg("img/test.jpg");
  5. $t->setMaskOffsetX(55);
  6. $t->setMaskOffsetY(55);
  7. $t->setMaskPosition(1);
  8. //$t->setMaskPosition(2);
  9. //$t->setMaskPosition(3);
  10. //$t->setMaskPosition(4);
  11. $t->setMaskFontColor("#ffff00");
  12. $t->setMaskWord("test");

  13. // 指定固定宽高
  14. $t->createImg(50);
  15. ?>
复制代码
  1. <?php
  2. require_once('lib/thumb.class.php');

  3. $t = new ThumbHandler();

  4. $t->setSrcImg("img/test.jpg");
  5. $t->setMaskFont("c:/winnt/fonts/simyou.ttf");
  6. $t->setMaskFontSize(20);
  7. $t->setMaskFontColor("#ffffff");
  8. $t->setMaskTxtPct(20);
  9. $t->setDstImgBorder(10,"#dddddd");
  10. $text = "中文";
  11. $str = mb_convert_encoding($text, "UTF-8", "gb2312");
  12. $t->setMaskWord($str);
  13. $t->setMaskWord("test");

  14. // 指定固定宽高
  15. $t->createImg(50);
  16. ?>
  17. </strlen>
  18. ?>
复制代码


echo, print, printf 和 sprintf 区别(PHP)

摘自:http://www.lihaixin.com/2008/07/echo-print-printf-sprintf-php-156.html

echo
是命令,不能返回值。echo后面可以跟很多个参数,之间用分号隔开,如:
echo $myvar1;
echo 1,2,$myvar,”<b>bold</b>”;

print

是函数,可以返回一个值,只能有一个参数。

printf

函数,返回输出字符个数,把文字格式化以后输出,如:
$name=”hunte”;
$age=25;

$a=printf(”my name is %s, age %d”, $name, $age); //输出The dog likes to bark
echo $a // 输出21

sprintf

函数,将输出指派到字符串,而不是直接指派到标准输出,其他跟printf相似,但不打印,而是返回格式化后的文字,其他的与printf一样。
$b=sprintf(”The %2\$s likes to %1\$s”,”bark”,”dog”);
echo $b;  //输出变量b的字符串内容

2010年2月8日星期一

jquery的使用 关于 option ,append,attr,val()等的使用(转)

摘自:http://blog.csdn.net/showsunrise/archive/2009/12/10/4979457.aspx

//遍历option和添加、移除option

function changeShipMethod(shipping){

var len = $("select[@name=ISHIPTYPE] option").length

if(shipping.value != "CA"){

  $("select[@name=ISHIPTYPE] option").each(function(){

   if($(this).val() == 111){

    $(this).remove();

   }

  });

}else{

  $("UPS Ground").appendTo($("select[@name=ISHIPTYPE]"));

}

http://blog.csdn.net/ajaxchen_615/archive/2009/02/12/3878001.aspx>

 

 

}

//取得下拉选单的选取值

$('#testSelect option:selected').text();

或$("#testSelect").find('option:selected').text();

或$("#testSelect").val();

1,下拉框:

var cc1   = $(".formc select[@name='country'] option[@selected]").text(); //得到下拉菜单的选中项的文本(注意中间有空格)

var cc2 = $('.formc select[@name="country"]').val();   //得到下拉菜单的选中项的值

var cc3 = $('.formc select[@name="country"]').attr("id"); //得到下拉菜单的选中项的ID属性值

$("#select").empty();//清空下拉框//$("#select").html('');

$("1111").appendTo("#select")//添加下拉框的option

稍微解释一下:

1.select[@name='country'] option[@selected] 表示具有name 属性,

并且该属性值为'country' 的select元素 里面的具有selected 属性的option 元素;

可以看出有@开头的就表示后面跟的是属性。

2,单选框:

$("input[@type=radio][@checked]").val();   //得到单选框的选中项的值(注意中间没有空格)

$("input[@type=radio][@value=2]").attr("checked",'checked'); //设置单选框value=2的为选中状态.(注意中间没有空格)

3,复选框:

$("input[@type=checkbox][@checked]").val(); //得到复选框的选中的第一项的值

$("input[@type=checkbox][@checked]").each(function(){ //由于复选框一般选中的是多个,所以可以循环输出

   alert($(this).val());

   });

$("#chk1").attr("checked",'');//不打勾

$("#chk2").attr("checked",true);//打勾

if($("#chk1").attr('checked')==undefined){} //判断是否已经打勾

当然jquery的选择器是强大的. 还有很多方法.

< src="jquery-1.2.1.js" type="text/javascript">

< language="javascript" type="text/javascript">

$(document).ready(function(){

$("#selectTest").change(function()

{

       //alert("Hello");

       //alert($("#selectTest").attr("name"));

       //$("a").attr("href","xx.html");

       //window.location.href="xx.html";

       //alert($("#selectTest").val());

       alert($("#selectTest option[@selected]").text());

       $("#selectTest").attr("value", "2");

});

});

aaass

jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关获取一组radio被选中项的值

var item = $('input[@name=items][@checked]').val();

获取select被选中项的文本

var item = $("select[@name=items] option[@selected]").text();

select下拉框的第二个元素为当前选中值

$('#select_id')[0].selectedIndex = 1;

radio单选组的第二个元素为当前选中值

$('input[@name=items]').get(1).checked = true;

获取值:

文本框,文本区域:$("#txt").attr("value");

多选框checkbox:$("#checkbox_id").attr("value");

单选组radio: $("input[@type=radio][@checked]").val();

下拉框select: $('#sel').val();

控制表单元素:

文本框,文本区域:$("#txt").attr("value",'');//清空内容

                $("#txt").attr("value",'11');//填充内容

多选框checkbox: $("#chk1").attr("checked",'');//不打勾

                $("#chk2").attr("checked",true);//打勾

                if($("#chk1").attr('checked')==undefined) //判断是否已经打勾

单选组radio: $("input[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项

下拉框select: $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项

            $("11112222").appendTo("#sel")//添加下拉框的option

            $("#sel").empty();//清空下拉框

获取一组radio被选中项的值

var item = $('input[@name=items][@checked]').val();

获取select被选中项的文本

var item = $("select[@name=items] option[@selected]").text();

select下拉框的第二个元素为当前选中值

$('#select_id')[0].selectedIndex = 1;

radio单选组的第二个元素为当前选中值

$('input[@name=items]').get(1).checked = true;

获取值:

文本框,文本区域:$("#txt").attr("value");

多选框checkbox:$("#checkbox_id").attr("value");

单选组radio: $("input[@type=radio][@checked]").val();

下拉框select: $('#sel').val();

控制表单元素:

文本框,文本区域:$("#txt").attr("value",'');//清空内容

$("#txt").attr("value",'11');//填充内容

多选框checkbox: $("#chk1").attr("checked",'');//不打勾

$("#chk2").attr("checked",true);//打勾

if($("#chk1").attr('checked')==undefined) //判断是否已经打勾

单选组radio: $("input[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项

下拉框select: $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项

$("11112222").appendTo("#sel")//添加下拉框的option

$("#sel").empty();//清空下拉框