PageRenderTime 102ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/ThinkPHP/Extend/Engine/Cluster/DefaultApp/Lib/ORG/Image.class.php

https://github.com/huangshan/yuqing
PHP | 502 lines | 345 code | 27 blank | 130 comment | 87 complexity | 95f81dc6f328035ba4f7b32aaa7f423b MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * 图像操作类库
  13. * @category ORG
  14. * @package ORG
  15. * @subpackage Util
  16. * @author liu21st <liu21st@gmail.com>
  17. */
  18. class Image {
  19. /**
  20. * 取得图像信息
  21. * @static
  22. * @access public
  23. * @param string $image 图像文件名
  24. * @return mixed
  25. */
  26. static function getImageInfo($img) {
  27. $imageInfo = getimagesize($img);
  28. if ($imageInfo !== false) {
  29. $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
  30. $imageSize = filesize($img);
  31. $info = array(
  32. "width" => $imageInfo[0],
  33. "height" => $imageInfo[1],
  34. "type" => $imageType,
  35. "size" => $imageSize,
  36. "mime" => $imageInfo['mime']
  37. );
  38. return $info;
  39. } else {
  40. return false;
  41. }
  42. }
  43. /**
  44. * 为图片添加水印
  45. * @static public
  46. * @param string $source 原文件名
  47. * @param string $water 水印图片
  48. * @param string $$savename 添加水印后的图片名
  49. * @param string $alpha 水印的透明度
  50. * @return void
  51. */
  52. static public function water($source, $water, $savename=null, $alpha=80,$get_source=true) {
  53. //[cluster]
  54. if($get_source){
  55. $source_tmp_name=tempnam(sys_get_temp_dir(),'tp_');
  56. file_put_contents($source_tmp_name,file_get($source));
  57. $origin_savename=is_null($savename)?$source:$savename;
  58. $source=$source_tmp_name;
  59. }
  60. //检查文件是否存在
  61. if (!file_exists($source) || !file_exists($water))
  62. return false;
  63. //图片信息
  64. $sInfo = self::getImageInfo($source);
  65. $wInfo = self::getImageInfo($water);
  66. //如果图片小于水印图片,不生成图片
  67. if ($sInfo["width"] < $wInfo["width"] || $sInfo['height'] < $wInfo['height'])
  68. return false;
  69. //建立图像
  70. $sCreateFun = "imagecreatefrom" . $sInfo['type'];
  71. $sImage = $sCreateFun($source);
  72. $wCreateFun = "imagecreatefrom" . $wInfo['type'];
  73. $wImage = $wCreateFun($water);
  74. //设定图像的混色模式
  75. imagealphablending($wImage, true);
  76. //图像位置,默认为右下角右对齐
  77. $posY = $sInfo["height"] - $wInfo["height"];
  78. $posX = $sInfo["width"] - $wInfo["width"];
  79. //生成混合图像
  80. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'], $wInfo['height'], $alpha);
  81. //输出图像
  82. $ImageFun = 'Image' . $sInfo['type'];
  83. //如果没有给出保存文件名,默认为原图像名
  84. if (!$savename) {
  85. $savename = $source;
  86. @unlink($source);
  87. }elseif($get_source){
  88. //[cluster] 临时保存文件
  89. $savename=tempnam(sys_get_temp_dir(),'tp_');
  90. @unlink($source);
  91. }
  92. //保存图像
  93. $ImageFun($sImage, $savename);
  94. imagedestroy($sImage);
  95. //[cluster] 上传文件
  96. if($get_source){
  97. file_upload($savename,$origin_savename);
  98. @unlink($savename);
  99. }
  100. }
  101. function showImg($imgFile, $text='', $x='10', $y='10', $alpha='50') {
  102. //获取图像文件信息
  103. //2007/6/26 增加图片水印输出,$text为图片的完整路径即可
  104. $info = Image::getImageInfo($imgFile);
  105. if ($info !== false) {
  106. $createFun = str_replace('/', 'createfrom', $info['mime']);
  107. $im = $createFun($imgFile);
  108. if ($im) {
  109. $ImageFun = str_replace('/', '', $info['mime']);
  110. //水印开始
  111. if (!empty($text)) {
  112. $tc = imagecolorallocate($im, 0, 0, 0);
  113. if (is_file($text) && file_exists($text)) {//判断$text是否是图片路径
  114. // 取得水印信息
  115. $textInfo = Image::getImageInfo($text);
  116. $createFun2 = str_replace('/', 'createfrom', $textInfo['mime']);
  117. $waterMark = $createFun2($text);
  118. //$waterMark=imagecolorallocatealpha($text,255,255,0,50);
  119. $imgW = $info["width"];
  120. $imgH = $info["width"] * $textInfo["height"] / $textInfo["width"];
  121. //$y = ($info["height"]-$textInfo["height"])/2;
  122. //设置水印的显示位置和透明度支持各种图片格式
  123. imagecopymerge($im, $waterMark, $x, $y, 0, 0, $textInfo['width'], $textInfo['height'], $alpha);
  124. } else {
  125. imagestring($im, 80, $x, $y, $text, $tc);
  126. }
  127. //ImageDestroy($tc);
  128. }
  129. //水印结束
  130. if ($info['type'] == 'png' || $info['type'] == 'gif') {
  131. imagealphablending($im, FALSE); //取消默认的混色模式
  132. imagesavealpha($im, TRUE); //设定保存完整的 alpha 通道信息
  133. }
  134. Header("Content-type: " . $info['mime']);
  135. $ImageFun($im);
  136. @ImageDestroy($im);
  137. return;
  138. }
  139. //保存图像
  140. $ImageFun($sImage, $savename);
  141. imagedestroy($sImage);
  142. //获取或者创建图像文件失败则生成空白PNG图片
  143. $im = imagecreatetruecolor(80, 30);
  144. $bgc = imagecolorallocate($im, 255, 255, 255);
  145. $tc = imagecolorallocate($im, 0, 0, 0);
  146. imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
  147. imagestring($im, 4, 5, 5, "no pic", $tc);
  148. Image::output($im);
  149. return;
  150. }
  151. }
  152. /**
  153. * 生成缩略图
  154. * @static
  155. * @access public
  156. * @param string $image 原图
  157. * @param string $type 图像格式
  158. * @param string $thumbname 缩略图文件名
  159. * @param string $maxWidth 宽度
  160. * @param string $maxHeight 高度
  161. * @param string $position 缩略图保存目录
  162. * @param boolean $interlace 启用隔行扫描
  163. * @return void
  164. */
  165. static function thumb($image, $thumbname, $type='', $maxWidth=200, $maxHeight=50, $interlace=true) {
  166. // 获取原图信息
  167. $info = Image::getImageInfo($image);
  168. if ($info !== false) {
  169. $srcWidth = $info['width'];
  170. $srcHeight = $info['height'];
  171. $type = empty($type) ? $info['type'] : $type;
  172. $type = strtolower($type);
  173. $interlace = $interlace ? 1 : 0;
  174. unset($info);
  175. $scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 计算缩放比例
  176. if ($scale >= 1) {
  177. // 超过原图大小不再缩略
  178. $width = $srcWidth;
  179. $height = $srcHeight;
  180. } else {
  181. // 缩略图尺寸
  182. $width = (int) ($srcWidth * $scale);
  183. $height = (int) ($srcHeight * $scale);
  184. }
  185. // 载入原图
  186. $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
  187. if(!function_exists($createFun)) {
  188. return false;
  189. }
  190. $srcImg = $createFun($image);
  191. //创建缩略图
  192. if ($type != 'gif' && function_exists('imagecreatetruecolor'))
  193. $thumbImg = imagecreatetruecolor($width, $height);
  194. else
  195. $thumbImg = imagecreate($width, $height);
  196. //png和gif的透明处理 by luofei614
  197. if('png'==$type){
  198. imagealphablending($thumbImg, false);//取消默认的混色模式(为解决阴影为绿色的问题)
  199. imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息(为解决阴影为绿色的问题)
  200. }elseif('gif'==$type){
  201. $trnprt_indx = imagecolortransparent($srcImg);
  202. if ($trnprt_indx >= 0) {
  203. //its transparent
  204. $trnprt_color = imagecolorsforindex($srcImg , $trnprt_indx);
  205. $trnprt_indx = imagecolorallocate($thumbImg, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  206. imagefill($thumbImg, 0, 0, $trnprt_indx);
  207. imagecolortransparent($thumbImg, $trnprt_indx);
  208. }
  209. }
  210. // 复制图片
  211. if (function_exists("ImageCopyResampled"))
  212. imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
  213. else
  214. imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
  215. // 对jpeg图形设置隔行扫描
  216. if ('jpg' == $type || 'jpeg' == $type)
  217. imageinterlace($thumbImg, $interlace);
  218. // 生成图片
  219. $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
  220. $imageFun($thumbImg, $thumbname);
  221. imagedestroy($thumbImg);
  222. imagedestroy($srcImg);
  223. return $thumbname;
  224. }
  225. return false;
  226. }
  227. /**
  228. * 根据给定的字符串生成图像
  229. * @static
  230. * @access public
  231. * @param string $string 字符串
  232. * @param string $size 图像大小 width,height 或者 array(width,height)
  233. * @param string $font 字体信息 fontface,fontsize 或者 array(fontface,fontsize)
  234. * @param string $type 图像格式 默认PNG
  235. * @param integer $disturb 是否干扰 1 点干扰 2 线干扰 3 复合干扰 0 无干扰
  236. * @param bool $border 是否加边框 array(color)
  237. * @return string
  238. */
  239. static function buildString($string, $rgb=array(), $filename='', $type='png', $disturb=1, $border=true) {
  240. if (is_string($size))
  241. $size = explode(',', $size);
  242. $width = $size[0];
  243. $height = $size[1];
  244. if (is_string($font))
  245. $font = explode(',', $font);
  246. $fontface = $font[0];
  247. $fontsize = $font[1];
  248. $length = strlen($string);
  249. $width = ($length * 9 + 10) > $width ? $length * 9 + 10 : $width;
  250. $height = 22;
  251. if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
  252. $im = @imagecreatetruecolor($width, $height);
  253. } else {
  254. $im = @imagecreate($width, $height);
  255. }
  256. if (empty($rgb)) {
  257. $color = imagecolorallocate($im, 102, 104, 104);
  258. } else {
  259. $color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
  260. }
  261. $backColor = imagecolorallocate($im, 255, 255, 255); //背景色(随机)
  262. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  263. $pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //点颜色
  264. @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  265. @imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
  266. @imagestring($im, 5, 5, 3, $string, $color);
  267. if (!empty($disturb)) {
  268. // 添加干扰
  269. if ($disturb = 1 || $disturb = 3) {
  270. for ($i = 0; $i < 25; $i++) {
  271. imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
  272. }
  273. } elseif ($disturb = 2 || $disturb = 3) {
  274. for ($i = 0; $i < 10; $i++) {
  275. imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $pointColor);
  276. }
  277. }
  278. }
  279. Image::output($im, $type, $filename);
  280. }
  281. /**
  282. * 生成图像验证码
  283. * @static
  284. * @access public
  285. * @param string $length 位数
  286. * @param string $mode 类型
  287. * @param string $type 图像格式
  288. * @param string $width 宽度
  289. * @param string $height 高度
  290. * @return string
  291. */
  292. static function buildImageVerify($length=4, $mode=1, $type='png', $width=48, $height=22, $verifyName='verify') {
  293. import('ORG.Util.String');
  294. $randval = String::randString($length, $mode);
  295. session($verifyName, md5($randval));
  296. $width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;
  297. if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
  298. $im = imagecreatetruecolor($width, $height);
  299. } else {
  300. $im = imagecreate($width, $height);
  301. }
  302. $r = Array(225, 255, 255, 223);
  303. $g = Array(225, 236, 237, 255);
  304. $b = Array(225, 236, 166, 125);
  305. $key = mt_rand(0, 3);
  306. $backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]); //背景色(随机)
  307. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  308. imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  309. imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
  310. $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
  311. // 干扰
  312. for ($i = 0; $i < 10; $i++) {
  313. imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $stringColor);
  314. }
  315. for ($i = 0; $i < 25; $i++) {
  316. imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $stringColor);
  317. }
  318. for ($i = 0; $i < $length; $i++) {
  319. imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);
  320. }
  321. Image::output($im, $type);
  322. }
  323. // 中文验证码
  324. static function GBVerify($length=4, $type='png', $width=180, $height=50, $fontface='simhei.ttf', $verifyName='verify') {
  325. import('ORG.Util.String');
  326. $code = String::randString($length, 4);
  327. $width = ($length * 45) > $width ? $length * 45 : $width;
  328. session($verifyName, md5($code));
  329. $im = imagecreatetruecolor($width, $height);
  330. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  331. $bkcolor = imagecolorallocate($im, 250, 250, 250);
  332. imagefill($im, 0, 0, $bkcolor);
  333. @imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
  334. // 干扰
  335. for ($i = 0; $i < 15; $i++) {
  336. $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  337. imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
  338. }
  339. for ($i = 0; $i < 255; $i++) {
  340. $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  341. imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $fontcolor);
  342. }
  343. if (!is_file($fontface)) {
  344. $fontface = dirname(__FILE__) . "/" . $fontface;
  345. }
  346. for ($i = 0; $i < $length; $i++) {
  347. $fontcolor = imagecolorallocate($im, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); //这样保证随机出来的颜色较深。
  348. $codex = String::msubstr($code, $i, 1);
  349. imagettftext($im, mt_rand(16, 20), mt_rand(-60, 60), 40 * $i + 20, mt_rand(30, 35), $fontcolor, $fontface, $codex);
  350. }
  351. Image::output($im, $type);
  352. }
  353. /**
  354. * 把图像转换成字符显示
  355. * @static
  356. * @access public
  357. * @param string $image 要显示的图像
  358. * @param string $type 图像类型,默认自动获取
  359. * @return string
  360. */
  361. static function showASCIIImg($image, $string='', $type='') {
  362. $info = Image::getImageInfo($image);
  363. if ($info !== false) {
  364. $type = empty($type) ? $info['type'] : $type;
  365. unset($info);
  366. // 载入原图
  367. $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
  368. $im = $createFun($image);
  369. $dx = imagesx($im);
  370. $dy = imagesy($im);
  371. $i = 0;
  372. $out = '<span style="padding:0px;margin:0;line-height:100%;font-size:1px;">';
  373. set_time_limit(0);
  374. for ($y = 0; $y < $dy; $y++) {
  375. for ($x = 0; $x < $dx; $x++) {
  376. $col = imagecolorat($im, $x, $y);
  377. $rgb = imagecolorsforindex($im, $col);
  378. $str = empty($string) ? '*' : $string[$i++];
  379. $out .= sprintf('<span style="margin:0px;color:#%02x%02x%02x">' . $str . '</span>', $rgb['red'], $rgb['green'], $rgb['blue']);
  380. }
  381. $out .= "<br>\n";
  382. }
  383. $out .= '</span>';
  384. imagedestroy($im);
  385. return $out;
  386. }
  387. return false;
  388. }
  389. /**
  390. * 生成UPC-A条形码
  391. * @static
  392. * @param string $type 图像格式
  393. * @param string $type 图像格式
  394. * @param string $lw 单元宽度
  395. * @param string $hi 条码高度
  396. * @return string
  397. */
  398. static function UPCA($code, $type='png', $lw=2, $hi=100) {
  399. static $Lencode = array('0001101', '0011001', '0010011', '0111101', '0100011',
  400. '0110001', '0101111', '0111011', '0110111', '0001011');
  401. static $Rencode = array('1110010', '1100110', '1101100', '1000010', '1011100',
  402. '1001110', '1010000', '1000100', '1001000', '1110100');
  403. $ends = '101';
  404. $center = '01010';
  405. /* UPC-A Must be 11 digits, we compute the checksum. */
  406. if (strlen($code) != 11) {
  407. die("UPC-A Must be 11 digits.");
  408. }
  409. /* Compute the EAN-13 Checksum digit */
  410. $ncode = '0' . $code;
  411. $even = 0;
  412. $odd = 0;
  413. for ($x = 0; $x < 12; $x++) {
  414. if ($x % 2) {
  415. $odd += $ncode[$x];
  416. } else {
  417. $even += $ncode[$x];
  418. }
  419. }
  420. $code.= ( 10 - (($odd * 3 + $even) % 10)) % 10;
  421. /* Create the bar encoding using a binary string */
  422. $bars = $ends;
  423. $bars.=$Lencode[$code[0]];
  424. for ($x = 1; $x < 6; $x++) {
  425. $bars.=$Lencode[$code[$x]];
  426. }
  427. $bars.=$center;
  428. for ($x = 6; $x < 12; $x++) {
  429. $bars.=$Rencode[$code[$x]];
  430. }
  431. $bars.=$ends;
  432. /* Generate the Barcode Image */
  433. if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
  434. $im = imagecreatetruecolor($lw * 95 + 30, $hi + 30);
  435. } else {
  436. $im = imagecreate($lw * 95 + 30, $hi + 30);
  437. }
  438. $fg = ImageColorAllocate($im, 0, 0, 0);
  439. $bg = ImageColorAllocate($im, 255, 255, 255);
  440. ImageFilledRectangle($im, 0, 0, $lw * 95 + 30, $hi + 30, $bg);
  441. $shift = 10;
  442. for ($x = 0; $x < strlen($bars); $x++) {
  443. if (($x < 10) || ($x >= 45 && $x < 50) || ($x >= 85)) {
  444. $sh = 10;
  445. } else {
  446. $sh = 0;
  447. }
  448. if ($bars[$x] == '1') {
  449. $color = $fg;
  450. } else {
  451. $color = $bg;
  452. }
  453. ImageFilledRectangle($im, ($x * $lw) + 15, 5, ($x + 1) * $lw + 14, $hi + 5 + $sh, $color);
  454. }
  455. /* Add the Human Readable Label */
  456. ImageString($im, 4, 5, $hi - 5, $code[0], $fg);
  457. for ($x = 0; $x < 5; $x++) {
  458. ImageString($im, 5, $lw * (13 + $x * 6) + 15, $hi + 5, $code[$x + 1], $fg);
  459. ImageString($im, 5, $lw * (53 + $x * 6) + 15, $hi + 5, $code[$x + 6], $fg);
  460. }
  461. ImageString($im, 4, $lw * 95 + 17, $hi - 5, $code[11], $fg);
  462. /* Output the Header and Content. */
  463. Image::output($im, $type);
  464. }
  465. static function output($im, $type='png', $filename='') {
  466. header("Content-type: image/" . $type);
  467. $ImageFun = 'image' . $type;
  468. if (empty($filename)) {
  469. $ImageFun($im);
  470. } else {
  471. $ImageFun($im, $filename);
  472. }
  473. imagedestroy($im);
  474. }
  475. }