cls_img_cutter.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. <?php
  2. /*
  3. * Krpano100 图片裁剪
  4. * ============================================================================
  5. * 技术支持:2015-2099 成都世纪川翔科技有限公司
  6. * 官网地址: http://www.krpano100.com
  7. * ----------------------------------------------------------------------------
  8. * $Author: wanghao 932625974#qq.com $
  9. * $Id: cls_img_cutter.php 28028 2016-06-19Z wanghao $
  10. */
  11. class ThumbHandler
  12. {
  13. var $dst_img;// 目标文件
  14. var $h_src; // 图片资源句柄
  15. var $h_dst;// 新图句柄
  16. var $h_mask;// 水印句柄
  17. var $img_create_quality = 100;// 图片生成质量
  18. var $img_display_quality = 80;// 图片显示质量
  19. var $img_scale = 0;// 图片缩放比例
  20. var $src_w = 0;// 原图宽度
  21. var $src_h = 0;// 原图高度
  22. var $dst_w = 0;// 新图总宽度
  23. var $dst_h = 0;// 新图总高度
  24. var $fill_w;// 填充图形宽
  25. var $fill_h;// 填充图形高
  26. var $copy_w;// 拷贝图形宽
  27. var $copy_h;// 拷贝图形高
  28. var $src_x = 0;// 原图绘制起始横坐标
  29. var $src_y = 0;// 原图绘制起始纵坐标
  30. var $start_x;// 新图绘制起始横坐标
  31. var $start_y;// 新图绘制起始纵坐标
  32. var $mask_word;// 水印文字
  33. var $mask_img;// 水印图片
  34. var $mask_pos_x = 0;// 水印横坐标
  35. var $mask_pos_y = 0;// 水印纵坐标
  36. var $mask_offset_x = 5;// 水印横向偏移
  37. var $mask_offset_y = 5;// 水印纵向偏移
  38. var $font_w;// 水印字体宽
  39. var $font_h;// 水印字体高
  40. var $mask_w;// 水印宽
  41. var $mask_h;// 水印高
  42. var $mask_font_color = "#ffffff";// 水印文字颜色
  43. var $mask_font = 2;// 水印字体
  44. var $font_size;// 尺寸
  45. var $mask_position = 0;// 水印位置
  46. var $mask_img_pct = 50;// 图片合并程度,值越大,合并程序越低
  47. var $mask_txt_pct = 50;// 文字合并程度,值越小,合并程序越低
  48. var $img_border_size = 0;// 图片边框尺寸
  49. var $img_border_color;// 图片边框颜色
  50. var $_flip_x=0;// 水平翻转次数
  51. var $_flip_y=0;// 垂直翻转次数
  52. var $cut_type=0;// 剪切类型
  53. var $img_type;// 文件类型
  54. // 文件类型定义,并指出了输出图片的函数
  55. var $all_type = array(
  56. "jpg" => array("output"=>"imagejpeg"),
  57. "gif" => array("output"=>"imagegif"),
  58. "png" => array("output"=>"imagepng"),
  59. "wbmp" => array("output"=>"image2wbmp"),
  60. "jpeg" => array("output"=>"imagejpeg"));
  61. /**
  62. * 构造函数
  63. */
  64. function ThumbHandler()
  65. {
  66. $this->mask_font_color = "#ffffff";
  67. $this->font = 2;
  68. $this->font_size = 12;
  69. }
  70. /**
  71. * 取得图片的宽
  72. */
  73. function getImgWidth($src)
  74. {
  75. return imagesx($src);
  76. }
  77. /**
  78. * 取得图片的高
  79. */
  80. function getImgHeight($src)
  81. {
  82. return imagesy($src);
  83. }
  84. /**
  85. * 设置图片生成路径
  86. *
  87. * @param string $src_img 图片生成路径
  88. */
  89. function setSrcImg($src_img, $img_type=null)
  90. {
  91. if(!file_exists($src_img))
  92. {
  93. die("图片不存在");
  94. }
  95. if(!empty($img_type))
  96. {
  97. $this->img_type = $img_type;
  98. }
  99. else
  100. {
  101. $this->img_type = $this->_getImgType($src_img);
  102. }
  103. $this->_checkValid($this->img_type);
  104. if($this->img_type=="png"){
  105. //PNG的质量范围是0-9,而JPG的质量范围在0-100
  106. $this->img_display_quality = 9;
  107. }
  108. // file_get_contents函数要求php版本>4.3.0
  109. $src = '';
  110. if(function_exists("file_get_contents"))
  111. {
  112. $src = file_get_contents($src_img);
  113. }
  114. else
  115. {
  116. $handle = fopen ($src_img, "r");
  117. while (!feof ($handle))
  118. {
  119. $src .= fgets($fd, 4096);
  120. }
  121. fclose ($handle);
  122. }
  123. if(empty($src))
  124. {
  125. die("图片源为空");
  126. }
  127. $this->h_src = @ImageCreateFromString($src);
  128. $this->src_w = $this->getImgWidth($this->h_src);
  129. $this->src_h = $this->getImgHeight($this->h_src);
  130. }
  131. /**
  132. * 设置图片生成路径
  133. *
  134. * @param string $dst_img 图片生成路径
  135. */
  136. function setDstImg($dst_img)
  137. {
  138. $arr = explode('/',$dst_img);
  139. $last = array_pop($arr);
  140. $path = implode('/',$arr);
  141. $this->_mkdirs($path);
  142. $this->dst_img = $dst_img;
  143. }
  144. /**
  145. * 设置图片的显示质量
  146. *
  147. * @param string $n 质量
  148. */
  149. function setImgDisplayQuality($n)
  150. {
  151. $this->img_display_quality = (int)$n;
  152. }
  153. /**
  154. * 设置图片的生成质量
  155. *
  156. * @param string $n 质量
  157. */
  158. function setImgCreateQuality($n)
  159. {
  160. $this->img_create_quality = (int)$n;
  161. }
  162. /**
  163. * 设置文字水印
  164. *
  165. * @param string $word 水印文字
  166. * @param integer $font 水印字体
  167. * @param string $color 水印字体颜色
  168. */
  169. function setMaskWord($word)
  170. {
  171. $this->mask_word .= $word;
  172. }
  173. /**
  174. * 设置字体颜色
  175. *
  176. * @param string $color 字体颜色
  177. */
  178. function setMaskFontColor($color="#ffffff")
  179. {
  180. $this->mask_font_color = $color;
  181. }
  182. /**
  183. * 设置水印字体
  184. *
  185. * @param string|integer $font 字体
  186. */
  187. function setMaskFont($font=2)
  188. {
  189. if(!is_numeric($font) && !file_exists($font))
  190. {
  191. die("字体文件不存在");
  192. }
  193. $this->font = $font;
  194. }
  195. /**
  196. * 设置文字字体大小,仅对truetype字体有效
  197. */
  198. function setMaskFontSize($size = "12")
  199. {
  200. $this->font_size = $size;
  201. }
  202. /**
  203. * 设置图片水印
  204. *
  205. * @param string $img 水印图片源
  206. */
  207. function setMaskImg($img)
  208. {
  209. $this->mask_img = $img;
  210. }
  211. /**
  212. * 设置水印横向偏移
  213. *
  214. * @param integer $x 横向偏移量
  215. */
  216. function setMaskOffsetX($x)
  217. {
  218. $this->mask_offset_x = (int)$x;
  219. }
  220. /**
  221. * 设置水印纵向偏移
  222. *
  223. * @param integer $y 纵向偏移量
  224. */
  225. function setMaskOffsetY($y)
  226. {
  227. $this->mask_offset_y = (int)$y;
  228. }
  229. /**
  230. * 指定水印位置
  231. *
  232. * @param integer $position 位置,1:左上,2:左下,3:右上,0/4:右下
  233. */
  234. function setMaskPosition($position=0)
  235. {
  236. $this->mask_position = (int)$position;
  237. }
  238. /**
  239. * 设置图片合并程度
  240. *
  241. * @param integer $n 合并程度
  242. */
  243. function setMaskImgPct($n)
  244. {
  245. $this->mask_img_pct = (int)$n;
  246. }
  247. /**
  248. * 设置文字合并程度
  249. *
  250. * @param integer $n 合并程度
  251. */
  252. function setMaskTxtPct($n)
  253. {
  254. $this->mask_txt_pct = (int)$n;
  255. }
  256. /**
  257. * 设置缩略图边框
  258. *
  259. * @param (类型) (参数名) (描述)
  260. */
  261. function setDstImgBorder($size=1, $color="#000000")
  262. {
  263. $this->img_border_size = (int)$size;
  264. $this->img_border_color = $color;
  265. }
  266. /**
  267. * 水平翻转
  268. */
  269. function flipH()
  270. {
  271. $this->_flip_x++;
  272. }
  273. /**
  274. * 垂直翻转
  275. */
  276. function flipV()
  277. {
  278. $this->_flip_y++;
  279. }
  280. /**
  281. * 设置剪切类型
  282. *
  283. * @param (类型) (参数名) (描述)
  284. */
  285. function setCutType($type)
  286. {
  287. $this->cut_type = (int)$type;
  288. }
  289. /**
  290. * 设置图片剪切
  291. *
  292. * @param integer $width 矩形剪切
  293. */
  294. function setRectangleCut($width, $height)
  295. {
  296. $this->fill_w = (int)$width;
  297. $this->fill_h = (int)$height;
  298. }
  299. /**
  300. * 设置源图剪切起始坐标点
  301. *
  302. * @param (类型) (参数名) (描述)
  303. */
  304. function setSrcCutPosition($x, $y)
  305. {
  306. $this->src_x = (int)$x;
  307. $this->src_y = (int)$y;
  308. }
  309. /**
  310. * 创建图片,主函数
  311. * @param integer $a 当缺少第二个参数时,此参数将用作百分比,
  312. * 否则作为宽度值
  313. * @param integer $b 图片缩放后的高度
  314. */
  315. function createImg($a, $b=null)
  316. {
  317. $num = func_num_args();
  318. if(1 == $num)
  319. {
  320. $r = (int)$a;
  321. if($r < 1)
  322. {
  323. die("图片缩放比例不得小于1");
  324. }
  325. $this->img_scale = $r;
  326. $this->_setNewImgSize($r);
  327. }
  328. if(2 == $num)
  329. {
  330. $w = (int)$a;
  331. $h = (int)$b;
  332. if(0 == $w)
  333. {
  334. die("目标宽度不能为0");
  335. }
  336. if(0 == $h)
  337. {
  338. die("目标高度不能为0");
  339. }
  340. $this->_setNewImgSize($w, $h);
  341. }
  342. if($this->_flip_x%2!=0)
  343. {
  344. $this->_flipH($this->h_src);
  345. }
  346. if($this->_flip_y%2!=0)
  347. {
  348. $this->_flipV($this->h_src);
  349. }
  350. $this->_createMask();
  351. $this->_output();
  352. // 释放
  353. if(imagedestroy($this->h_src) && imagedestroy($this->h_dst))
  354. {
  355. Return true;
  356. }
  357. else
  358. {
  359. Return false;
  360. }
  361. }
  362. /**
  363. * 生成水印,调用了生成水印文字和水印图片两个方法
  364. */
  365. function _createMask()
  366. {
  367. if($this->mask_word)
  368. {
  369. // 获取字体信息
  370. $this->_setFontInfo();
  371. if($this->_isFull())
  372. {
  373. die("水印文字过大");
  374. }
  375. else
  376. {
  377. $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  378. $white = ImageColorAllocate($this->h_dst,255,255,255);
  379. imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  380. $this->_drawBorder();
  381. imagecopyresampled( $this->h_dst, $this->h_src,
  382. $this->start_x, $this->start_y,
  383. $this->src_x, $this->src_y,
  384. $this->fill_w, $this->fill_h,
  385. $this->copy_w, $this->copy_h);
  386. $this->_createMaskWord($this->h_dst);
  387. }
  388. }
  389. if($this->mask_img)
  390. {
  391. $this->_loadMaskImg();//加载时,取得宽高
  392. if($this->_isFull())
  393. {
  394. // 将水印生成在原图上再拷
  395. $this->_createMaskImg($this->h_src);
  396. $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  397. $white = ImageColorAllocate($this->h_dst,255,255,255);
  398. imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  399. $this->_drawBorder();
  400. imagecopyresampled( $this->h_dst, $this->h_src,
  401. $this->start_x, $this->start_y,
  402. $this->src_x, $this->src_y,
  403. $this->fill_w, $this->start_y,
  404. $this->copy_w, $this->copy_h);
  405. }
  406. else
  407. {
  408. // 创建新图并拷贝
  409. $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  410. $white = ImageColorAllocate($this->h_dst,255,255,255);
  411. imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  412. $this->_drawBorder();
  413. imagecopyresampled( $this->h_dst, $this->h_src,
  414. $this->start_x, $this->start_y,
  415. $this->src_x, $this->src_y,
  416. $this->fill_w, $this->fill_h,
  417. $this->copy_w, $this->copy_h);
  418. $this->_createMaskImg($this->h_dst);
  419. }
  420. }
  421. if(empty($this->mask_word) && empty($this->mask_img))
  422. {
  423. $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  424. $white = ImageColorAllocate($this->h_dst,255,255,255);
  425. imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  426. $this->_drawBorder();
  427. imagecopyresampled( $this->h_dst, $this->h_src,
  428. $this->start_x, $this->start_y,
  429. $this->src_x, $this->src_y,
  430. $this->fill_w, $this->fill_h,
  431. $this->copy_w, $this->copy_h);
  432. }
  433. }
  434. /**
  435. * 画边框
  436. */
  437. function _drawBorder()
  438. {
  439. if(!empty($this->img_border_size))
  440. {
  441. $c = $this->_parseColor($this->img_border_color);
  442. $color = ImageColorAllocate($this->h_src,$c[0], $c[1], $c[2]);
  443. imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$color);// 填充背景色
  444. }
  445. }
  446. /**
  447. * 生成水印文字
  448. */
  449. function _createMaskWord($src)
  450. {
  451. $this->_countMaskPos();
  452. $this->_checkMaskValid();
  453. $c = $this->_parseColor($this->mask_font_color);
  454. $color = imagecolorallocatealpha($src, $c[0], $c[1], $c[2], $this->mask_txt_pct);
  455. if(is_numeric($this->font))
  456. {
  457. imagestring($src,
  458. $this->font,
  459. $this->mask_pos_x, $this->mask_pos_y,
  460. $this->mask_word,
  461. $color);
  462. }
  463. else
  464. {
  465. imagettftext($src,
  466. $this->font_size, 0,
  467. $this->mask_pos_x, $this->mask_pos_y,
  468. $color,
  469. $this->font,
  470. $this->mask_word);
  471. }
  472. }
  473. /**
  474. * 生成水印图
  475. */
  476. function _createMaskImg($src)
  477. {
  478. $this->_countMaskPos();
  479. $this->_checkMaskValid();
  480. imagecopymerge($src,
  481. $this->h_mask,
  482. $this->mask_pos_x ,$this->mask_pos_y,
  483. 0, 0,
  484. $this->mask_w, $this->mask_h,
  485. $this->mask_img_pct);
  486. imagedestroy($this->h_mask);
  487. }
  488. /**
  489. * 加载水印图
  490. */
  491. function _loadMaskImg()
  492. {
  493. $mask_type = $this->_getImgType($this->mask_img);
  494. $this->_checkValid($mask_type);
  495. // file_get_contents函数要求php版本>4.3.0
  496. $src = '';
  497. if(function_exists("file_get_contents"))
  498. {
  499. $src = file_get_contents($this->mask_img);
  500. }
  501. else
  502. {
  503. $handle = fopen ($this->mask_img, "r");
  504. while (!feof ($handle))
  505. {
  506. $src .= fgets($fd, 4096);
  507. }
  508. fclose ($handle);
  509. }
  510. if(empty($this->mask_img))
  511. {
  512. die("水印图片为空");
  513. }
  514. $this->h_mask = ImageCreateFromString($src);
  515. $this->mask_w = $this->getImgWidth($this->h_mask);
  516. $this->mask_h = $this->getImgHeight($this->h_mask);
  517. }
  518. /**
  519. * 图片输出
  520. */
  521. function _output()
  522. {
  523. $img_type = $this->img_type;
  524. $func_name = $this->all_type[$img_type]['output'];
  525. if(function_exists($func_name))
  526. {
  527. // 判断浏览器,若是IE就不发送头
  528. if(isset($_SERVER['HTTP_USER_AGENT']))
  529. {
  530. $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);
  531. if(!preg_match('/^.*MSIE.*\)$/i',$ua))
  532. {
  533. header("Content-type:$img_type");
  534. }
  535. }
  536. $func_name($this->h_dst, $this->dst_img, $this->img_display_quality);
  537. }
  538. else
  539. {
  540. Return false;
  541. }
  542. }
  543. /**
  544. * 分析颜色
  545. *
  546. * @param string $color 十六进制颜色
  547. */
  548. function _parseColor($color)
  549. {
  550. $arr = array();
  551. for($ii=1; $ii<strlen ($color); $ii++)
  552. {
  553. $arr[] = hexdec(substr($color,$ii,2));
  554. $ii++;
  555. }
  556. Return $arr;
  557. }
  558. /**
  559. * 计算出位置坐标
  560. */
  561. function _countMaskPos()
  562. {
  563. if($this->_isFull())
  564. {
  565. switch($this->mask_position)
  566. {
  567. case 1:
  568. // 左上
  569. $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
  570. $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
  571. break;
  572. case 2:
  573. // 左下
  574. $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
  575. $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
  576. break;
  577. case 3:
  578. // 右上
  579. $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
  580. $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
  581. break;
  582. case 4:
  583. // 右下
  584. $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
  585. $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
  586. break;
  587. default:
  588. // 默认将水印放到右下,偏移指定像素
  589. $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
  590. $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
  591. break;
  592. }
  593. }
  594. else
  595. {
  596. switch($this->mask_position)
  597. {
  598. case 1:
  599. // 左上
  600. $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
  601. $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
  602. break;
  603. case 2:
  604. // 左下
  605. $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
  606. $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
  607. break;
  608. case 3:
  609. // 右上
  610. $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
  611. $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
  612. break;
  613. case 4:
  614. // 右下
  615. $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
  616. $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
  617. break;
  618. default:
  619. // 默认将水印放到右下,偏移指定像素
  620. $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
  621. $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
  622. break;
  623. }
  624. }
  625. }
  626. /**
  627. * 设置字体信息
  628. */
  629. function _setFontInfo()
  630. {
  631. if(is_numeric($this->font))
  632. {
  633. $this->font_w = imagefontwidth($this->font);
  634. $this->font_h = imagefontheight($this->font);
  635. // 计算水印字体所占宽高
  636. $word_length = strlen($this->mask_word);
  637. $this->mask_w = $this->font_w*$word_length;
  638. $this->mask_h = $this->font_h;
  639. }
  640. else
  641. {
  642. $arr = imagettfbbox ($this->font_size,0, $this->font,$this->mask_word);
  643. $this->mask_w = abs($arr[0] - $arr[2]);
  644. $this->mask_h = abs($arr[7] - $arr[1]);
  645. }
  646. }
  647. /**
  648. * 设置新图尺寸
  649. *
  650. * @param integer $img_w 目标宽度
  651. * @param integer $img_h 目标高度
  652. */
  653. function _setNewImgSize($img_w, $img_h=null)
  654. {
  655. $num = func_num_args();
  656. if(1 == $num)
  657. {
  658. $this->img_scale = $img_w;// 宽度作为比例
  659. $this->fill_w = round($this->src_w * $this->img_scale / 100) - $this->img_border_size*2;
  660. $this->fill_h = round($this->src_h * $this->img_scale / 100) - $this->img_border_size*2;
  661. // 源文件起始坐标
  662. $this->src_x = 0;
  663. $this->src_y = 0;
  664. $this->copy_w = $this->src_w;
  665. $this->copy_h = $this->src_h;
  666. // 目标尺寸
  667. $this->dst_w = $this->fill_w + $this->img_border_size*2;
  668. $this->dst_h = $this->fill_h + $this->img_border_size*2;
  669. }
  670. if(2 == $num)
  671. {
  672. $fill_w = (int)$img_w - $this->img_border_size*2;
  673. $fill_h = (int)$img_h - $this->img_border_size*2;
  674. if($fill_w < 0 || $fill_h < 0)
  675. {
  676. die("图片边框过大,已超过了图片的宽度");
  677. }
  678. $rate_w = $this->src_w/$fill_w;
  679. $rate_h = $this->src_h/$fill_h;
  680. switch($this->cut_type)
  681. {
  682. case 0:
  683. // 如果原图大于缩略图,产生缩小,否则不缩小
  684. if($rate_w < 1 && $rate_h < 1)
  685. {
  686. $this->fill_w = (int)$this->src_w;
  687. $this->fill_h = (int)$this->src_h;
  688. }
  689. else
  690. {
  691. if($rate_w >= $rate_h)
  692. {
  693. $this->fill_w = (int)$fill_w;
  694. $this->fill_h = round($this->src_h/$rate_w);
  695. }
  696. else
  697. {
  698. $this->fill_w = round($this->src_w/$rate_h);
  699. $this->fill_h = (int)$fill_h;
  700. }
  701. }
  702. $this->src_x = 0;
  703. $this->src_y = 0;
  704. $this->copy_w = $this->src_w;
  705. $this->copy_h = $this->src_h;
  706. // 目标尺寸
  707. $this->dst_w = $this->fill_w + $this->img_border_size*2;
  708. $this->dst_h = $this->fill_h + $this->img_border_size*2;
  709. break;
  710. // 自动裁切
  711. case 1:
  712. // 如果图片是缩小剪切才进行操作
  713. if($rate_w >= 1 && $rate_h >=1)
  714. {
  715. if($this->src_w > $this->src_h)
  716. {
  717. $src_x = round($this->src_w-$this->src_h)/2;
  718. $this->setSrcCutPosition($src_x, 0);
  719. $this->setRectangleCut($fill_h, $fill_h);
  720. $this->copy_w = $this->src_h;
  721. $this->copy_h = $this->src_h;
  722. }
  723. elseif($this->src_w < $this->src_h)
  724. {
  725. $src_y = round($this->src_h-$this->src_w)/2;
  726. $this->setSrcCutPosition(0, $src_y);
  727. $this->setRectangleCut($fill_w, $fill_h);
  728. $this->copy_w = $this->src_w;
  729. $this->copy_h = $this->src_w;
  730. }
  731. else
  732. {
  733. $this->setSrcCutPosition(0, 0);
  734. $this->copy_w = $this->src_w;
  735. $this->copy_h = $this->src_w;
  736. $this->setRectangleCut($fill_w, $fill_h);
  737. }
  738. }
  739. else
  740. {
  741. $this->setSrcCutPosition(0, 0);
  742. $this->setRectangleCut($this->src_w, $this->src_h);
  743. $this->copy_w = $this->src_w;
  744. $this->copy_h = $this->src_h;
  745. }
  746. // 目标尺寸
  747. $this->dst_w = $this->fill_w + $this->img_border_size*2;
  748. $this->dst_h = $this->fill_h + $this->img_border_size*2;
  749. break;
  750. // 手工裁切
  751. case 2:
  752. $this->copy_w = $this->fill_w;
  753. $this->copy_h = $this->fill_h;
  754. // 目标尺寸
  755. $this->dst_w = $this->fill_w + $this->img_border_size*2;
  756. $this->dst_h = $this->fill_h + $this->img_border_size*2;
  757. break;
  758. default:
  759. break;
  760. }
  761. }
  762. // 目标文件起始坐标
  763. $this->start_x = $this->img_border_size;
  764. $this->start_y = $this->img_border_size;
  765. }
  766. /**
  767. * 检查水印图是否大于生成后的图片宽高
  768. */
  769. function _isFull()
  770. {
  771. Return ( $this->mask_w + $this->mask_offset_x > $this->fill_w
  772. || $this->mask_h + $this->mask_offset_y > $this->fill_h)
  773. ?true:false;
  774. }
  775. /**
  776. * 检查水印图是否超过原图
  777. */
  778. function _checkMaskValid()
  779. {
  780. if( $this->mask_w + $this->mask_offset_x > $this->src_w
  781. || $this->mask_h + $this->mask_offset_y > $this->src_h)
  782. {
  783. die("水印图片尺寸大于原图,请缩小水印图");
  784. }
  785. }
  786. /**
  787. * 取得图片类型
  788. *
  789. * @param string $file_path 文件路径
  790. */
  791. function _getImgType($file_path)
  792. {
  793. $type_list = array("1"=>"gif","2"=>"jpg","3"=>"png","4"=>"swf","5" => "psd","6"=>"bmp","15"=>"wbmp");
  794. if(file_exists($file_path))
  795. {
  796. $img_info = @getimagesize($file_path);
  797. if(isset($type_list[$img_info[2]]))
  798. {
  799. Return $type_list[$img_info[2]];
  800. }
  801. }
  802. else
  803. {
  804. die("文件不存在,不能取得文件类型!");
  805. }
  806. }
  807. /**
  808. * 检查图片类型是否合法,调用了array_key_exists函数,此函数要求
  809. * php版本大于4.1.0
  810. *
  811. * @param string $img_type 文件类型
  812. */
  813. function _checkValid($img_type)
  814. {
  815. if(!array_key_exists($img_type, $this->all_type))
  816. {
  817. Return false;
  818. }
  819. }
  820. /**
  821. * 按指定路径生成目录
  822. *
  823. * @param string $path 路径
  824. */
  825. function _mkdirs($path)
  826. {
  827. $adir = explode('/',$path);
  828. $dirlist = '';
  829. $rootdir = array_shift($adir);
  830. if(($rootdir!='.'||$rootdir!='..')&&!file_exists($rootdir))
  831. {
  832. @mkdir($rootdir);
  833. }
  834. foreach($adir as $key=>$val)
  835. {
  836. if($val!='.'&&$val!='..')
  837. {
  838. $dirlist .= "/".$val;
  839. $dirpath = $rootdir.$dirlist;
  840. if(!file_exists($dirpath))
  841. {
  842. @mkdir($dirpath);
  843. @chmod($dirpath,0777);
  844. }
  845. }
  846. }
  847. }
  848. /**
  849. * 垂直翻转
  850. *
  851. * @param string $src 图片源
  852. */
  853. function _flipV($src)
  854. {
  855. $src_x = $this->getImgWidth($src);
  856. $src_y = $this->getImgHeight($src);
  857. $new_im = imagecreatetruecolor($src_x, $src_y);
  858. for ($y = 0; $y < $src_y; $y++)
  859. {
  860. imagecopy($new_im, $src, 0, $src_y - $y - 1, 0, $y, $src_x, 1);
  861. }
  862. $this->h_src = $new_im;
  863. }
  864. /**
  865. * 水平翻转
  866. *
  867. * @param string $src 图片源
  868. */
  869. function _flipH($src)
  870. {
  871. $src_x = $this->getImgWidth($src);
  872. $src_y = $this->getImgHeight($src);
  873. $new_im = imagecreatetruecolor($src_x, $src_y);
  874. for ($x = 0; $x < $src_x; $x++)
  875. {
  876. imagecopy($new_im, $src, $src_x - $x - 1, 0, $x, 0, 1, $src_y);
  877. }
  878. $this->h_src = $new_im;
  879. }
  880. }
  881. ?>