Image.class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Fanwe 方维直播系统
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: 云淡风轻(1956838968@qq.com)
  8. // +----------------------------------------------------------------------
  9. /**
  10. * 本类复制于 ThinkPHP Image.class.php 类, 修改水印以及合成的位置,透明度等的参数调用
  11. */
  12. class Image extends Think
  13. {//类定义开始
  14. /**
  15. +----------------------------------------------------------
  16. * 取得图像信息
  17. *
  18. +----------------------------------------------------------
  19. * @static
  20. * @access public
  21. +----------------------------------------------------------
  22. * @param string $image 图像文件名
  23. +----------------------------------------------------------
  24. * @return mixed
  25. +----------------------------------------------------------
  26. */
  27. static function getImageInfo($img) {
  28. $imageInfo = getimagesize($img);
  29. if( $imageInfo!== false) {
  30. $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]),1));
  31. $imageSize = filesize($img);
  32. $info = array(
  33. "width"=>$imageInfo[0],
  34. "height"=>$imageInfo[1],
  35. "type"=>$imageType,
  36. "size"=>$imageSize,
  37. "mime"=>$imageInfo['mime']
  38. );
  39. return $info;
  40. }else {
  41. return false;
  42. }
  43. }
  44. /**
  45. +----------------------------------------------------------
  46. * 显示服务器图像文件
  47. * 支持URL方式
  48. +----------------------------------------------------------
  49. * @static
  50. * @access public
  51. +----------------------------------------------------------
  52. * @param string $imgFile 图像文件名
  53. * @param string $text 文字字符串
  54. * @param string $width 图像宽度
  55. * @param string $height 图像高度
  56. +----------------------------------------------------------
  57. * @return void
  58. +----------------------------------------------------------
  59. */
  60. static function showImg($imgFile,$text='',$width=80,$height=30) {
  61. //获取图像文件信息
  62. $info = Image::getImageInfo($imgFile);
  63. if($info !== false) {
  64. $createFun = str_replace('/','createfrom',$info['mime']);
  65. $im = $createFun($imgFile);
  66. if($im) {
  67. $ImageFun= str_replace('/','',$info['mime']);
  68. if(!empty($text)) {
  69. $tc = imagecolorallocate($im, 0, 0, 0);
  70. imagestring($im, 3, 5, 5, $text, $tc);
  71. }
  72. if($info['type']=='png' || $info['type']=='gif') {
  73. imagealphablending($im, false);//取消默认的混色模式
  74. imagesavealpha($im,true);//设定保存完整的 alpha 通道信息
  75. }
  76. header("Content-type: ".$info['mime']);
  77. $ImageFun($im);
  78. imagedestroy($im);
  79. return ;
  80. }
  81. }
  82. //获取或者创建图像文件失败则生成空白PNG图片
  83. $im = imagecreatetruecolor($width, $height);
  84. $bgc = imagecolorallocate($im, 255, 255, 255);
  85. $tc = imagecolorallocate($im, 0, 0, 0);
  86. imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
  87. imagestring($im, 4, 5, 5, "NO PIC", $tc);
  88. Image::output($im);
  89. return ;
  90. }
  91. /**
  92. +----------------------------------------------------------
  93. * 生成缩略图
  94. +----------------------------------------------------------
  95. * @static
  96. * @access public
  97. +----------------------------------------------------------
  98. * @param string $image 原图
  99. * @param string $type 图像格式
  100. * @param string $thumbname 缩略图文件名
  101. * @param string $maxWidth 宽度
  102. * @param string $maxHeight 高度
  103. * @param string $position 缩略图保存目录
  104. * @param boolean $interlace 启用隔行扫描
  105. +----------------------------------------------------------
  106. * @return void
  107. +----------------------------------------------------------
  108. */
  109. static function thumb($image,$thumbname,$type='',$width=200,$height=50,$interlace=true)
  110. {
  111. // 获取原图信息
  112. $info = Image::getImageInfo($image);
  113. if($info !== false) {
  114. $srcWidth = $info['width'];
  115. $srcHeight = $info['height'];
  116. $type = empty($type)?$info['type']:$type;
  117. $type = strtolower($type);
  118. $interlace = $interlace? 1:0;
  119. unset($info);
  120. // 载入原图
  121. $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
  122. $srcImg = $createFun($image);
  123. //创建缩略图
  124. if($type!='gif' && function_exists('imagecreatetruecolor'))
  125. $thumbImg = imagecreatetruecolor($width, $height);
  126. else
  127. $thumbImg = imagecreate($width, $height);
  128. // 复制图片
  129. $bgcolor = trim(conf("BG_COLOR"),"#");
  130. sscanf($bgcolor, "%2x%2x%2x", $red, $green, $blue);
  131. $clr = imagecolorallocate($thumbImg, $red, $green, $blue);
  132. imagefilledrectangle($thumbImg, 0, 0, $width, $height, $clr);
  133. $scale_org = $srcWidth/$srcHeight;
  134. if ($srcWidth / $thumb_width > $srcHeight / $thumb_height)
  135. {
  136. $lessen_width = $width;
  137. $lessen_height = $width / $scale_org;
  138. }
  139. else
  140. {
  141. /* 原始图片比较高,则以高度为准 */
  142. $lessen_width = $height * $scale_org;
  143. $lessen_height = $height;
  144. }
  145. $dst_x = ($width - $lessen_width) / 2;
  146. $dst_y = ($height - $lessen_height) / 2;
  147. if(function_exists("ImageCopyResampled"))
  148. imagecopyresampled($thumbImg, $srcImg, $dst_x, $dst_y, 0, 0, $lessen_width, $lessen_height, $srcWidth,$srcHeight);
  149. else
  150. imagecopyresized($thumbImg, $srcImg, $dst_x, $dst_y, 0, 0, $lessen_width, $lessen_height, $srcWidth,$srcHeight);
  151. if('gif'==$type || 'png'==$type) {
  152. //imagealphablending($thumbImg, false);//取消默认的混色模式
  153. //imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息
  154. $background_color = imagecolorallocate($thumbImg, 0,255,0); // 指派一个绿色
  155. imagecolortransparent($thumbImg,$background_color); // 设置为透明色,若注释掉该行则输出绿色的图
  156. }
  157. // 对jpeg图形设置隔行扫描
  158. if('jpg'==$type || 'jpeg'==$type) imageinterlace($thumbImg,$interlace);
  159. //$gray=ImageColorAllocate($thumbImg,255,0,0);
  160. //ImageString($thumbImg,2,5,5,"ThinkPHP",$gray);
  161. // 生成图片
  162. $imageFun = 'image'.($type=='jpg'?'jpeg':$type);
  163. $imageFun($thumbImg,$thumbname);
  164. imagedestroy($thumbImg);
  165. imagedestroy($srcImg);
  166. return $thumbname;
  167. }
  168. return false;
  169. }
  170. /**
  171. +----------------------------------------------------------
  172. * 根据给定的字符串生成图像
  173. +----------------------------------------------------------
  174. * @static
  175. * @access public
  176. +----------------------------------------------------------
  177. * @param string $string 字符串
  178. * @param string $size 图像大小 width,height 或者 array(width,height)
  179. * @param string $font 字体信息 fontface,fontsize 或者 array(fontface,fontsize)
  180. * @param string $type 图像格式 默认PNG
  181. * @param integer $disturb 是否干扰 1 点干扰 2 线干扰 3 复合干扰 0 无干扰
  182. * @param bool $border 是否加边框 array(color)
  183. +----------------------------------------------------------
  184. * @return string
  185. +----------------------------------------------------------
  186. */
  187. static function buildString($string,$rgb=array(),$filename='',$type='png',$disturb=1,$border=true) {
  188. if(is_string($size)) $size = explode(',',$size);
  189. $width = $size[0];
  190. $height = $size[1];
  191. if(is_string($font)) $font = explode(',',$font);
  192. $fontface = $font[0];
  193. $fontsize = $font[1];
  194. $length = strlen($string);
  195. $width = ($length*9+10)>$width?$length*9+10:$width;
  196. $height = 22;
  197. if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
  198. $im = @imagecreatetruecolor($width,$height);
  199. }else {
  200. $im = @imagecreate($width,$height);
  201. }
  202. if(empty($rgb)) {
  203. $color = imagecolorallocate($im, 102, 104, 104);
  204. }else{
  205. $color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
  206. }
  207. $backColor = imagecolorallocate($im, 255,255,255); //背景色(随机)
  208. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  209. $pointColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //点颜色
  210. @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  211. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  212. @imagestring($im, 5, 5, 3, $string, $color);
  213. if(!empty($disturb)) {
  214. // 添加干扰
  215. if($disturb=1 || $disturb=3) {
  216. for($i=0;$i<25;$i++){
  217. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pointColor);
  218. }
  219. }elseif($disturb=2 || $disturb=3){
  220. for($i=0;$i<10;$i++){
  221. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$pointColor);
  222. }
  223. }
  224. }
  225. Image::output($im,$type,$filename);
  226. }
  227. /**
  228. +----------------------------------------------------------
  229. * 生成图像验证码
  230. +----------------------------------------------------------
  231. * @static
  232. * @access public
  233. +----------------------------------------------------------
  234. * @param string $length 位数
  235. * @param string $mode 类型
  236. * @param string $type 图像格式
  237. * @param string $width 宽度
  238. * @param string $height 高度
  239. +----------------------------------------------------------
  240. * @return string
  241. +----------------------------------------------------------
  242. */
  243. static function buildImageVerify($length=4,$mode=1,$type='gif',$width=48,$height=22,$verifyName='verify')
  244. {
  245. import('ORG.Util.String');
  246. $randval = String::rand_string($length,$mode);
  247. es_session::start();
  248. es_session::set($verifyName,md5($randval));
  249. $width = ($length*10+10)>$width?$length*10+10:$width;
  250. if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
  251. $im = @imagecreatetruecolor($width,$height);
  252. }else {
  253. $im = @imagecreate($width,$height);
  254. }
  255. $r = Array(225,255,255,223);
  256. $g = Array(225,236,237,255);
  257. $b = Array(225,236,166,125);
  258. $key = mt_rand(0,3);
  259. $backColor = imagecolorallocate($im, $r[$key],$g[$key],$b[$key]); //背景色(随机)
  260. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  261. $pointColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //点颜色
  262. @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  263. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  264. $stringColor = imagecolorallocate($im,mt_rand(0,200),mt_rand(0,120),mt_rand(0,120));
  265. // 干扰
  266. for($i=0;$i<10;$i++){
  267. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  268. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  269. }
  270. for($i=0;$i<25;$i++){
  271. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  272. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pointColor);
  273. }
  274. for($i=0;$i<$length;$i++) {
  275. imagestring($im,5,$i*10+5,mt_rand(1,8),$randval{$i}, $stringColor);
  276. }
  277. // @imagestring($im, 5, 5, 3, $randval, $stringColor);
  278. Image::output($im,$type);
  279. }
  280. // 中文验证码
  281. static function GBVerify($length=4,$type='png',$width=180,$height=50,$fontface='simhei.ttf',$verifyName='verify') {
  282. import('ORG.Util.String');
  283. $code = String::rand_string($length,4);
  284. $width = ($length*45)>$width?$length*45:$width;
  285. es_session::set($verifyName,md5($code));
  286. $im=imagecreatetruecolor($width,$height);
  287. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  288. $bkcolor=imagecolorallocate($im,250,250,250);
  289. imagefill($im,0,0,$bkcolor);
  290. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  291. // 干扰
  292. for($i=0;$i<15;$i++){
  293. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  294. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  295. }
  296. for($i=0;$i<255;$i++){
  297. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  298. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
  299. }
  300. if(!is_file($fontface)) {
  301. $fontface = dirname(__FILE__)."/".$fontface;
  302. }
  303. for($i=0;$i<$length;$i++){
  304. $fontcolor=imagecolorallocate($im,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); //这样保证随机出来的颜色较深。
  305. $codex= msubstr($code,$i,1);
  306. imagettftext($im,mt_rand(16,20),mt_rand(-60,60),40*$i+20,mt_rand(30,35),$fontcolor,$fontface,$codex);
  307. }
  308. Image::output($im,$type);
  309. }
  310. /**
  311. +----------------------------------------------------------
  312. * 把图像转换成字符显示
  313. +----------------------------------------------------------
  314. * @static
  315. * @access public
  316. +----------------------------------------------------------
  317. * @param string $image 要显示的图像
  318. * @param string $type 图像类型,默认自动获取
  319. +----------------------------------------------------------
  320. * @return string
  321. +----------------------------------------------------------
  322. */
  323. static function showASCIIImg($image,$string='',$type='')
  324. {
  325. $info = Image::getImageInfo($image);
  326. if($info !== false) {
  327. $type = empty($type)?$info['type']:$type;
  328. unset($info);
  329. // 载入原图
  330. $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
  331. $im = $createFun($image);
  332. $dx = imagesx($im);
  333. $dy = imagesy($im);
  334. $i = 0;
  335. $out = '<span style="padding:0px;margin:0;line-height:100%;font-size:1px;">';
  336. set_time_limit(0);
  337. for($y = 0; $y < $dy; $y++) {
  338. for($x=0; $x < $dx; $x++) {
  339. $col = imagecolorat($im, $x, $y);
  340. $rgb = imagecolorsforindex($im,$col);
  341. $str = empty($string)?'*':$string[$i++];
  342. $out .= sprintf('<span style="margin:0px;color:#%02x%02x%02x">'.$str.'</span>',$rgb['red'],$rgb['green'],$rgb['blue']);
  343. }
  344. $out .= "<br>\n";
  345. }
  346. $out .= '</span>';
  347. imagedestroy($im);
  348. return $out;
  349. }
  350. return false;
  351. }
  352. /**
  353. +----------------------------------------------------------
  354. * 生成高级图像验证码
  355. +----------------------------------------------------------
  356. * @static
  357. * @access public
  358. +----------------------------------------------------------
  359. * @param string $type 图像格式
  360. * @param string $width 宽度
  361. * @param string $height 高度
  362. +----------------------------------------------------------
  363. * @return string
  364. +----------------------------------------------------------
  365. */
  366. static function showAdvVerify($type='png',$width=180,$height=40,$verifyName='verifyCode')
  367. {
  368. $rand = range('a','z');
  369. shuffle($rand);
  370. $verifyCode = array_slice($rand,0,10);
  371. $letter = implode(" ",$verifyCode);
  372. es_session::set($verifyName,$verifyCode);
  373. $im = imagecreate($width,$height);
  374. $r = array(225,255,255,223);
  375. $g = array(225,236,237,255);
  376. $b = array(225,236,166,125);
  377. $key = mt_rand(0,3);
  378. $backColor = imagecolorallocate($im, $r[$key],$g[$key],$b[$key]);
  379. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  380. imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  381. imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  382. $numberColor = imagecolorallocate($im, 255,rand(0,100), rand(0,100));
  383. $stringColor = imagecolorallocate($im, rand(0,100), rand(0,100), 255);
  384. // 添加干扰
  385. /*
  386. for($i=0;$i<10;$i++){
  387. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  388. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  389. }
  390. for($i=0;$i<255;$i++){
  391. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  392. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
  393. }*/
  394. imagestring($im, 5, 5, 1, "0 1 2 3 4 5 6 7 8 9", $numberColor);
  395. imagestring($im, 5, 5, 20, $letter, $stringColor);
  396. Image::output($im,$type);
  397. }
  398. /**
  399. +----------------------------------------------------------
  400. * 生成UPC-A条形码
  401. +----------------------------------------------------------
  402. * @static
  403. +----------------------------------------------------------
  404. * @param string $type 图像格式
  405. * @param string $type 图像格式
  406. * @param string $lw 单元宽度
  407. * @param string $hi 条码高度
  408. +----------------------------------------------------------
  409. * @return string
  410. +----------------------------------------------------------
  411. */
  412. static function UPCA($code,$type='png',$lw=2,$hi=100) {
  413. static $Lencode = array('0001101','0011001','0010011','0111101','0100011',
  414. '0110001','0101111','0111011','0110111','0001011');
  415. static $Rencode = array('1110010','1100110','1101100','1000010','1011100',
  416. '1001110','1010000','1000100','1001000','1110100');
  417. $ends = '101';
  418. $center = '01010';
  419. /* UPC-A Must be 11 digits, we compute the checksum. */
  420. if ( strlen($code) != 11 ) { die("UPC-A Must be 11 digits."); }
  421. /* Compute the EAN-13 Checksum digit */
  422. $ncode = '0'.$code;
  423. $even = 0; $odd = 0;
  424. for ($x=0;$x<12;$x++) {
  425. if ($x % 2) { $odd += $ncode[$x]; } else { $even += $ncode[$x]; }
  426. }
  427. $code.=(10 - (($odd * 3 + $even) % 10)) % 10;
  428. /* Create the bar encoding using a binary string */
  429. $bars=$ends;
  430. $bars.=$Lencode[$code[0]];
  431. for($x=1;$x<6;$x++) {
  432. $bars.=$Lencode[$code[$x]];
  433. }
  434. $bars.=$center;
  435. for($x=6;$x<12;$x++) {
  436. $bars.=$Rencode[$code[$x]];
  437. }
  438. $bars.=$ends;
  439. /* Generate the Barcode Image */
  440. if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
  441. $im = imagecreatetruecolor($lw*95+30,$hi+30);
  442. }else {
  443. $im = imagecreate($lw*95+30,$hi+30);
  444. }
  445. $fg = ImageColorAllocate($im, 0, 0, 0);
  446. $bg = ImageColorAllocate($im, 255, 255, 255);
  447. ImageFilledRectangle($im, 0, 0, $lw*95+30, $hi+30, $bg);
  448. $shift=10;
  449. for ($x=0;$x<strlen($bars);$x++) {
  450. if (($x<10) || ($x>=45 && $x<50) || ($x >=85)) { $sh=10; } else { $sh=0; }
  451. if ($bars[$x] == '1') { $color = $fg; } else { $color = $bg; }
  452. ImageFilledRectangle($im, ($x*$lw)+15,5,($x+1)*$lw+14,$hi+5+$sh,$color);
  453. }
  454. /* Add the Human Readable Label */
  455. ImageString($im,4,5,$hi-5,$code[0],$fg);
  456. for ($x=0;$x<5;$x++) {
  457. ImageString($im,5,$lw*(13+$x*6)+15,$hi+5,$code[$x+1],$fg);
  458. ImageString($im,5,$lw*(53+$x*6)+15,$hi+5,$code[$x+6],$fg);
  459. }
  460. ImageString($im,4,$lw*95+17,$hi-5,$code[11],$fg);
  461. /* Output the Header and Content. */
  462. Image::output($im,$type);
  463. }
  464. static function output($im,$type='gif',$filename='')
  465. {
  466. ob_clean();
  467. header("Content-type: image/".$type);
  468. $ImageFun='image'.$type;
  469. if(empty($filename)) {
  470. if(!$ImageFun($im))
  471. {
  472. ob_clean();
  473. header("Content-type: image/jpeg");
  474. if(!imagejpeg($im))
  475. {
  476. ob_clean();
  477. header("Content-type: image/png");
  478. if(!imagepng($im))
  479. {
  480. es_session::delete("verify");
  481. }
  482. }
  483. }
  484. }else{
  485. $ImageFun($im,$filename);
  486. }
  487. imagedestroy($im);
  488. }
  489. /**
  490. +----------------------------------------------------------
  491. * 为图片添加水印
  492. +----------------------------------------------------------
  493. * @static public
  494. +----------------------------------------------------------
  495. * @param string $source 原文件名
  496. * @param string $water 水印图片
  497. * @param string $$savename 添加水印后的图片名
  498. * @param string $alpha 水印的透明度
  499. +----------------------------------------------------------
  500. * @return string
  501. +----------------------------------------------------------
  502. * @throws ThinkExecption
  503. +----------------------------------------------------------
  504. */
  505. //修改 by matthew 增加水印位置
  506. static public function water($source,$water,$savename=null,$alpha=80,$position="0")
  507. {
  508. //检查文件是否存在
  509. if(!file_exists($source)||!file_exists($water))
  510. return false;
  511. //图片信息
  512. $sInfo=self::getImageInfo($source);
  513. $wInfo=self::getImageInfo($water);
  514. //如果图片小于水印图片,不生成图片
  515. if($sInfo["width"]<$wInfo["width"] || $sInfo['height']<$wInfo['height'])
  516. return false;
  517. //建立图像
  518. $sCreateFun="imagecreatefrom".$sInfo['type'];
  519. $sImage=$sCreateFun($source);
  520. $wCreateFun="imagecreatefrom".$wInfo['type'];
  521. $wImage=$wCreateFun($water);
  522. //设定图像的混色模式
  523. imagealphablending($wImage, true);
  524. switch (intval($position))
  525. {
  526. case 0: break;
  527. //左上
  528. case 1:
  529. $posY=0;
  530. $posX=0;
  531. //生成混合图像
  532. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'],$wInfo['height'],$alpha);
  533. break;
  534. //右上
  535. case 2:
  536. $posY=0;
  537. $posX=$sInfo["width"]-$wInfo["width"];
  538. //生成混合图像
  539. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'],$wInfo['height'],$alpha);
  540. break;
  541. //左下
  542. case 3:
  543. $posY=$sInfo["height"]-$wInfo["height"];
  544. $posX=0;
  545. //生成混合图像
  546. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'],$wInfo['height'],$alpha);
  547. break;
  548. //右下
  549. case 4:
  550. $posY=$sInfo["height"]-$wInfo["height"];
  551. $posX=$sInfo["width"]-$wInfo["width"];
  552. //生成混合图像
  553. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'],$wInfo['height'],$alpha);
  554. break;
  555. //居中
  556. case 5:
  557. $posY=$sInfo["height"]/2-$wInfo["height"]/2;
  558. $posX=$sInfo["width"]/2-$wInfo["width"]/2;
  559. //生成混合图像
  560. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'],$wInfo['height'],$alpha);
  561. break;
  562. }
  563. //输出图像
  564. $ImageFun='Image'.$sInfo['type'];
  565. //如果没有给出保存文件名,默认为原图像名
  566. if(!$savename){
  567. $savename=$source;
  568. @unlink($source);
  569. }
  570. //保存图像
  571. $ImageFun($sImage,$savename);
  572. imagedestroy($sImage);
  573. }
  574. }//类定义结束
  575. if(!function_exists('image_type_to_extension'))
  576. {
  577. function image_type_to_extension($imagetype)
  578. {
  579. if(empty($imagetype)) return false;
  580. switch($imagetype)
  581. {
  582. case IMAGETYPE_GIF : return '.gif';
  583. case IMAGETYPE_JPEG : return '.jpeg';
  584. case IMAGETYPE_PNG : return '.png';
  585. case IMAGETYPE_SWF : return '.swf';
  586. case IMAGETYPE_PSD : return '.psd';
  587. case IMAGETYPE_BMP : return '.bmp';
  588. case IMAGETYPE_TIFF_II : return '.tiff';
  589. case IMAGETYPE_TIFF_MM : return '.tiff';
  590. case IMAGETYPE_JPC : return '.jpc';
  591. case IMAGETYPE_JP2 : return '.jp2';
  592. case IMAGETYPE_JPX : return '.jpf';
  593. case IMAGETYPE_JB2 : return '.jb2';
  594. case IMAGETYPE_SWC : return '.swc';
  595. case IMAGETYPE_IFF : return '.aiff';
  596. case IMAGETYPE_WBMP : return '.wbmp';
  597. case IMAGETYPE_XBM : return '.xbm';
  598. default : return false;
  599. }
  600. }
  601. }
  602. ?>