PageRenderTime 59ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/ThinkPHP/Extend/Library/ORG/Util/Image.class.php

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