PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ThinkPHP/Extend/Engine/Sae/Lib/Extend/Library/ORG/Util/Image_sae.class.php

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