es_image.php 25 KB

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