afca92333b4bbb10f88d2c190360eebe2e621200.svn-base 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. <?php namespace Maze\Img;
  2. use Imagick;
  3. use ImagickDraw;
  4. class Handle
  5. {
  6. /**
  7. * @desc 使用的图片转换类型,默认是gd库,也可以用imagemagick,值为im
  8. * @var string
  9. */
  10. private $_type = 'im';//另一个值是im
  11. /**
  12. * @desc 进行缩略图 值为'600_100,100_200'
  13. * @var string
  14. */
  15. private $_thumb = '';
  16. /**
  17. * @desc 进行裁切图 值为'600_100,100_200'
  18. * @var string
  19. */
  20. private $_crop = '';
  21. /**
  22. * @desc 如果文件存在,值为true则强制再次操作,默认为false
  23. * @var string
  24. */
  25. private $_setup = false;
  26. /**
  27. * @desc 图片水平位置 0=>100 1=>100
  28. * @var string
  29. */
  30. private $_position = array();
  31. /**
  32. * @desc 源文件
  33. * @var string
  34. */
  35. private $_source;
  36. /**
  37. * @desc 裁切图片时候需要抛弃的大小
  38. * @var string
  39. */
  40. private $_dropSize = array();
  41. /**
  42. * @desc 目标文件
  43. * @var string
  44. */
  45. private $_dest = array();
  46. /**
  47. * @desc 添加水印图片
  48. * @var array
  49. */
  50. private $_mark = array();
  51. /**
  52. * @desc 添加文字
  53. * @var array
  54. */
  55. private $_txt = array();
  56. /**
  57. * @desc image的源信息
  58. * @var array
  59. */
  60. private $_image = null;
  61. /**
  62. * @desc image的类型
  63. * @var array
  64. */
  65. private $_imageType = null;
  66. /**
  67. * @desc 图片压缩的清晰度
  68. * @var int
  69. */
  70. private $_quality = 20;
  71. /**
  72. * @desc 生成的图片名
  73. * @var int
  74. */
  75. private $_name = null;
  76. /**
  77. * @desc 设置图片库类型
  78. * @param type(string) 类型
  79. * @author leo(suwi.bin)
  80. * @date 2012-03-23
  81. */
  82. public function setType($type)
  83. {
  84. $this->_type = $type;
  85. return $this;
  86. }
  87. /**
  88. * @desc 设置清晰度
  89. * @param quality(int) 清晰度
  90. * @author leo(suwi.bin)
  91. * @date 2012-03-23
  92. */
  93. public function setQuality($quality)
  94. {
  95. $this->_quality = $quality;
  96. return $this;
  97. }
  98. /**
  99. * @desc 设置强制功能
  100. * @param setup(bool) 是否强制再次生成重复的文件
  101. * @author leo(suwi.bin)
  102. * @date 2012-03-23
  103. */
  104. public function setSetup($setup)
  105. {
  106. $this->_setup = $setup;
  107. return $this;
  108. }
  109. /**
  110. * @desc 设置缩略图
  111. * @param *
  112. * @author leo(suwi.bin)
  113. * @date 2012-03-23
  114. */
  115. public function setThumb($thumb)
  116. {
  117. $this->_thumb = $thumb;
  118. return $this;
  119. }
  120. /**
  121. * @desc 设置裁切图
  122. * @param *
  123. * @author leo(suwi.bin)
  124. * @date 2012-03-23
  125. */
  126. public function setCrop($crop)
  127. {
  128. $this->_crop = $crop;
  129. return $this;
  130. }
  131. /**
  132. * @desc 设置生成的文件名
  133. * @param *
  134. * @author leo(suwi.bin)
  135. * @date 2012-03-23
  136. */
  137. public function setName($name)
  138. {
  139. if(is_string($name)) $name = explode(',', $name);
  140. $this->_name = $name;
  141. return $this;
  142. }
  143. /**
  144. * @desc 设置位置
  145. * @param *
  146. * @author leo(suwi.bin)
  147. * @date 2012-03-23
  148. */
  149. public function setPosition($position)
  150. {
  151. $this->_position = $position;
  152. return $this;
  153. }
  154. /**
  155. * @desc 设置水印
  156. * @param *
  157. * @author leo(suwi.bin)
  158. * @date 2012-03-23
  159. */
  160. public function setMark($mark)
  161. {
  162. $this->_mark = $mark;
  163. $this->_check('mark', 'water');
  164. $this->_check('mark', 'position');
  165. return $this;
  166. }
  167. /**
  168. * @desc 设置文字
  169. * @param *
  170. * @author leo(suwi.bin)
  171. * @date 2012-03-23
  172. */
  173. public function setTxt($txt)
  174. {
  175. $this->_txt = $txt;
  176. return $this;
  177. }
  178. /**
  179. * @desc 设置源文件
  180. * @param *
  181. * @author leo(suwi.bin)
  182. * @date 2012-03-23
  183. */
  184. public function setSource($source)
  185. {
  186. $this->_source = $source;
  187. $this->_check('source');
  188. $this->_image();
  189. return $this;
  190. }
  191. /**
  192. * @desc 获取生成的图片名字
  193. * @param *
  194. * @author leo(suwi.bin)
  195. * @date 2012-03-23
  196. */
  197. public function getName($name = false, $key = false)
  198. {
  199. if($this->_name)
  200. {
  201. if($key)
  202. {
  203. return $this->_name[$key];
  204. }
  205. return $this->_name[0];
  206. }
  207. else
  208. {
  209. return $this->_source . $name;
  210. }
  211. }
  212. /**
  213. * @desc 获取目标文件
  214. * @param *
  215. * @author leo(suwi.bin)
  216. * @date 2012-03-23
  217. */
  218. public function getDest($key = false)
  219. {
  220. return ($key && isset($this->_dest[$key])) ? $this->_dest[$key] : $this->_desc;
  221. }
  222. /**
  223. * @desc 获取宽高
  224. * @param *
  225. * @author leo(suwi.bin)
  226. * @date 2012-03-23
  227. */
  228. public function getSize($source)
  229. {
  230. $this->setSource($source);
  231. $this->_image();
  232. return array($this->_image->getImageWidth(), $this->_image->getImageHeight());
  233. }
  234. /**
  235. * @desc 添加水印图
  236. * @param *
  237. * @author leo(suwi.bin)
  238. * @date 2012-03-23
  239. */
  240. public function mark($source, $water, $setup = false, $name = false)
  241. {
  242. if($setup == true)
  243. {
  244. $this->setSetup($setup);
  245. }
  246. $this->setSource($source);
  247. if($name) $this->setName($name);
  248. $this->setMark($water);
  249. //if($position) $this->setPosition($position);
  250. $this->loadMethod('mark');
  251. return $this->getDest('mark');
  252. }
  253. /**
  254. * @desc 对图片进行压缩处理
  255. * @param *
  256. * @author leo(suwi.bin)
  257. * @date 2012-03-23
  258. */
  259. public function compress($source, $quality = 20, $name = false)
  260. {
  261. $this->setSource($source);
  262. if($name) $this->setName($name);
  263. $this->setQuality($quality);
  264. $this->loadMethod('compress');
  265. return $this->getDest('compress');
  266. }
  267. /**
  268. * @desc 构造函数 可批量建立
  269. * @param *
  270. * @author leo(suwi.bin)
  271. * @date 2012-03-23
  272. */
  273. public function __construct($config = array())
  274. {
  275. if($config)
  276. {
  277. $this->init($config);
  278. }
  279. }
  280. /**
  281. * @desc 批量建立
  282. * @param *
  283. * @author leo(suwi.bin)
  284. * @date 2012-03-23
  285. */
  286. public function init($config)
  287. {
  288. $this->setSource($config['source']);
  289. if(isset($config['setup']) && $config['setup'] == true)
  290. {
  291. $this->setSource($config['setup']);
  292. }
  293. unset($config['source']);
  294. foreach($config as $k => $v)
  295. {
  296. $this->$k($config['source'], $v);
  297. }
  298. return $this->getDest();
  299. }
  300. /**
  301. * @desc 建立缩略图(原比例缩略)
  302. * @param *
  303. * @author leo(suwi.bin)
  304. * @date 2012-03-23
  305. */
  306. public function thumb($source, $thumb, $setup = false, $name = false)
  307. {
  308. if($setup == true)
  309. {
  310. $this->setSetup($setup);
  311. }
  312. $this->setSource($source);
  313. if($name) $this->setName($name);
  314. $this->setThumb($thumb);
  315. $this->loadMethod('thumb');
  316. $this->_name = false;
  317. return $this->getDest('thumb');
  318. }
  319. /**
  320. * @desc 建立剪切图(从图中剪切)
  321. * @param *
  322. * @author leo(suwi.bin)
  323. * @date 2012-03-23
  324. */
  325. public function crop($source, $crop, $position = false, $setup = false, $name = false)
  326. {
  327. if($setup == true)
  328. {
  329. $this->setSetup($setup);
  330. }
  331. $this->setSource($source);
  332. if($position) $this->setPosition($position);
  333. if($name) $this->setName($name);
  334. $this->setCrop($crop);
  335. $this->loadMethod('crop');
  336. $this->_name = false;
  337. return $this->getDest('crop');
  338. }
  339. /**
  340. * @desc 建立剪切图(从图中剪切)
  341. * @param *
  342. * @author leo(suwi.bin)
  343. * @date 2012-03-23
  344. */
  345. public function thumbAndCrop($source, $size, $dropSize = array(), $position = false, $setup = true, $name = false)
  346. {
  347. if($setup == true)
  348. {
  349. $this->setSetup($setup);
  350. }
  351. $this->setSource($source);
  352. $this->setThumb($size);
  353. if($position) $this->setPosition($position);
  354. if($name) $this->setName($name);
  355. $this->setCrop($size);
  356. $this->loadMethod('thumb');
  357. $this->_dropSize = $dropSize;
  358. $this->loadMethod('crop');
  359. $this->_name = false;
  360. return $this->getDest('crop');
  361. }
  362. /**
  363. * @desc 加入文字
  364. * @param *
  365. * @author leo(suwi.bin)
  366. * @date 2012-03-23
  367. */
  368. public function txt($source, $txt, $setup = false, $name = false)
  369. {
  370. if($setup == true)
  371. {
  372. $this->setSetup($setup);
  373. }
  374. $this->setSource($source);
  375. if($name) $this->setName($name);
  376. $this->setTxt($txt);
  377. $this->loadMethod('txt');
  378. return $this->getDest('txt');
  379. }
  380. /**
  381. * @desc 载入方法
  382. * @param *
  383. * @author leo(suwi.bin)
  384. * @date 2012-03-23
  385. */
  386. public function loadMethod($method)
  387. {
  388. if($this->_type == 'im' && !class_exists('Imagick')) $this->_type = 'gd';
  389. $method = '_' . $this->_type . '_create_' . $method;
  390. $this->$method();
  391. }
  392. /**
  393. * @desc 对变量进行检测
  394. * @param name(string) 变量名称
  395. * @author leo(suwi.bin)
  396. * @date 2012-04-5
  397. */
  398. private function _check($name, $key = false)
  399. {
  400. $name = '_' . $name;
  401. if($name == '_mark')
  402. {
  403. return $this->_mark[$key];
  404. }
  405. if(isset($this->$name) && $this->$name)
  406. {
  407. if($key == false)
  408. {
  409. return $this->$name;
  410. }
  411. else
  412. {
  413. return $this->$name[$key];
  414. }
  415. }
  416. else
  417. {
  418. $this->_error($name . ' error');
  419. }
  420. }
  421. /**
  422. * @desc 匹配错误
  423. * @param *
  424. * @author leo(suwi.bin)
  425. * @date 2012-03-23
  426. */
  427. private function _error($string, $type = 1)
  428. {
  429. $errstr = '' ;
  430. $errstr .= "Img Tool Error:" . $string . "\n";
  431. return $errstr;
  432. }
  433. /**
  434. * @desc 获取文件源信息
  435. * @param *
  436. * @author leo(suwi.bin)
  437. * @date 2012-03-23
  438. */
  439. private function _image()
  440. {
  441. $this->_check('source');
  442. switch($this->_type)
  443. {
  444. case 'gd' :
  445. $this->_image = $this->_gd_get($this->_source);
  446. break;
  447. case 'im' :
  448. $this->_image = new Imagick($this->_source);
  449. $this->_imageType = strtolower($this->_image->getImageFormat());
  450. break;
  451. }
  452. return $this->_image;
  453. }
  454. /*********************
  455. * gd库函数
  456. *********************/
  457. /**
  458. * @desc 水印
  459. * @param *
  460. * @author leo(suwi.bin)
  461. * @date 2012-03-23
  462. */
  463. private function _gd_create_mark()
  464. {
  465. $this->_check('image');
  466. $this->_check('mark', 'water');
  467. $this->_check('mark', 'position');
  468. $this->_dest['mark'] = $this->getName('_mark.jpg');
  469. if($this->_setup == true || !file_exists($this->_dest['mark']))
  470. {
  471. $water = $this->_gd_get($this->_mark['water']);
  472. $source_x = imagesx($this->_image);
  473. $source_y = imagesy($this->_image);
  474. $water_x = imagesx($water);
  475. $water_y = imagesy($water);
  476. $xy = $this->_get_mark($source_x, $source_y, $water_x, $water_y);
  477. if($xy[2] == false)
  478. {
  479. $this->_gd_destroy($this->_image);
  480. $this->_gd_destroy($water);
  481. return;
  482. }
  483. $im = $this->_gd_copy($water, $water_x, $water_y, 0, 0, $l, $t, $this->_image);
  484. imagejpeg($im, $this->_dest['mark']);
  485. $this->_gd_destroy($this->_image);
  486. $this->_gd_destroy($water);
  487. }
  488. $this->_image = false;
  489. }
  490. /**
  491. * @desc 建立缩略图
  492. * @param *
  493. * @author leo(suwi.bin)
  494. * @date 2012-03-23
  495. */
  496. private function _gd_create_thumb()
  497. {
  498. $this->_check('image');
  499. $this->_check('thumb');
  500. if(!is_array($this->_thumb))
  501. {
  502. $array = explode(',',$this->_thumb);
  503. }
  504. else
  505. {
  506. $array = $this->_thumb;
  507. }
  508. $source_x = imagesx($this->_image);
  509. $source_y = imagesy($this->_image);
  510. $source_w = $source_x/$source_y;
  511. $source_h = $source_y/$source_x;
  512. foreach($array as $k => $v)
  513. {
  514. $this->_dest['thumb'][$k] = $this->getName('_' . $v . '_thumb.jpg', $k);
  515. if($this->_setup == true || !file_exists($this->_dest['thumb'][$k]))
  516. {
  517. $offset = explode('_',$v);
  518. if(isset($offset[2]) && $offset[2] == 1)
  519. {
  520. if($source_x > $offset[0])
  521. {
  522. $dest_x = $offset[0];
  523. $dest_y = $offset[0]*$source_h;
  524. }
  525. elseif($offset[1] > 0 && $source_y > $offset[1])
  526. {
  527. $dest_x = $offset[1]*$source_w;
  528. $dest_y = $offset[1];
  529. }
  530. else
  531. {
  532. $dest_x = $source_x;
  533. $dest_y = $source_y;
  534. }
  535. }
  536. elseif(isset($offset[2]) && $offset[2] == 2)
  537. {
  538. //按照一定比例
  539. if($source_x > $source_y && $source_y > $offset[1])
  540. {
  541. $dest_x = $offset[1]*$source_w;
  542. $dest_y = $offset[1];
  543. }
  544. elseif($source_y > $source_x && $source_x > $offset[0])
  545. {
  546. $dest_x = $offset[0];
  547. $dest_y = $offset[0]*$source_h;
  548. }
  549. else
  550. {
  551. $dest_x = $source_x;
  552. $dest_y = $source_y;
  553. }
  554. }
  555. else
  556. {
  557. $dest_x = $offset[0];
  558. $dest_y = $offset[1];
  559. }
  560. $im = $this->_gd_copy($this->_image,$dest_x,$dest_y,$source_x,$source_y,0,0,false,1);
  561. imagejpeg($im, $this->_dest['thumb'][$k]);
  562. $this->_gd_destroy($im);
  563. }
  564. }
  565. $this->_gd_destroy($this->_image);
  566. $this->_image = false;
  567. }
  568. /**
  569. * @desc 建立剪切图
  570. * @param *
  571. * @author leo(suwi.bin)
  572. * @date 2012-03-23
  573. */
  574. private function _gd_create_crop()
  575. {
  576. $this->_check('image');
  577. $this->_check('crop');
  578. //$this->_check('position');
  579. if(!is_array($this->_crop))
  580. {
  581. $array = explode(',',$this->_crop);
  582. }
  583. else
  584. {
  585. $array = $this->_crop;
  586. }
  587. $source_x = imagesx($this->_image);
  588. $source_y = imagesy($this->_image);
  589. foreach($array as $k => $v)
  590. {
  591. $this->_dest['crop'][$k] = $this->getName('_' . $v . '_crop.jpg', $k);
  592. if($this->_setup == true || !file_exists($this->_dest['crop'][$k]))
  593. {
  594. $offset = explode('_',$v);
  595. $x = 0;
  596. $y = 0;
  597. if($this->_position)
  598. {
  599. $x = $this->_position[0];
  600. $y = $this->_position[1];
  601. }
  602. $im = $this->_gd_copy($this->_image,$offset[0],$offset[1],$offset[0],$offset[1],$x,$y);
  603. imagejpeg($im, $this->_dest['crop'][$k]);
  604. $this->_gd_destroy($im);
  605. }
  606. }
  607. $this->_gd_destroy($this->_image);
  608. $this->_image = false;
  609. }
  610. /**
  611. * @desc 添加水印文字
  612. * @param *
  613. * @author leo(suwi.bin)
  614. * @date 2012-03-23
  615. */
  616. private function _gd_create_txt()
  617. {
  618. $this->_check('source');
  619. $this->_check('image');
  620. //$this->_check('txt','file');
  621. $this->_check('txt','color');
  622. $this->_check('txt','size');
  623. $this->_check('txt','angle');
  624. $this->_check('txt','name');
  625. //$this->_check('txt','left');
  626. //$this->_check('txt','top');
  627. //$this->_check('txt','bgcolor');
  628. //$this->_check('txt','font');
  629. $this->_dest['txt'] = isset($this->_txt['file']) ? $this->_txt['file'] : $this->getName('_txt.jpg');
  630. if($this->_setup == true || !file_exists($this->_dest['txt']))
  631. {
  632. $color = $this->_txt['color'];
  633. $this->_txt['left'] = isset($this->_txt['left']) ? $this->_txt['left'] : 0;
  634. $this->_txt['top'] = isset($this->_txt['top']) ? $this->_txt['top'] : 0;
  635. if(!empty($color) && (strlen($color)==7))
  636. {
  637. $R = hexdec(substr($color,1,2));
  638. $G = hexdec(substr($color,3,2));
  639. $B = hexdec(substr($color,5));
  640. putenv('GDFONTPATH=' . realpath('.'));
  641. $fontFile = isset($this->_txt['font']) ? $this->_txt['font'] : "SIMSUN.TTC";
  642. imagettftext($this->_image, $this->_txt['size'],$this->_txt['angle'], $this->_txt['left'], $this->_txt['top'], imagecolorallocate($this->_image, $R, $G, $B),$fontFile,$this->_txt['name']);
  643. }
  644. imagejpeg($this->_image, $this->_dest['txt']);
  645. $this->_gd_destroy($this->_image);
  646. }
  647. $this->_image = false;
  648. }
  649. /**
  650. * @desc 销毁资源
  651. * @param *
  652. * @author leo(suwi.bin)
  653. * @date 2012-03-23
  654. */
  655. private function _gd_destroy($im)
  656. {
  657. imagedestroy($im);
  658. return;
  659. }
  660. /**
  661. * @desc copy
  662. * @param *
  663. * @author leo(suwi.bin)
  664. * @date 2012-03-23
  665. */
  666. private function _gd_copy($im,$w,$h,$x,$y,$l,$t,$dim = false,$ti = 1)
  667. {
  668. if(false == $dim)
  669. {
  670. $dim = $this->_gd_create($w, $h,$ti); // 创建目标图gd2
  671. imagecopyresized($dim,$im,0,0,$l,$t,$w,$h,$x,$y);
  672. }
  673. else
  674. {
  675. imagecopy($dim, $im, $l,$t, 0, 0, $w,$h);
  676. //imagecopyresampled($dim, $im, $l,$t, 0, 0, $w,$h,$x,$y);
  677. }
  678. return $dim;
  679. }
  680. /**
  681. * @desc 获取数据源
  682. * @param *
  683. * @author leo(suwi.bin)
  684. * @date 2012-03-23
  685. */
  686. private function _gd_get($image)
  687. {
  688. $imgstream = file_get_contents($image);
  689. $im = imagecreatefromstring($imgstream);
  690. return $im;
  691. }
  692. /**
  693. * @desc 创建背景图
  694. * @param *
  695. * @author leo(suwi.bin)
  696. * @date 2012-03-23
  697. */
  698. private function _gd_create($w,$h,$t = 1)
  699. {
  700. $dim = imagecreatetruecolor($w,$h); // 创建目标图gd2
  701. //透明背景
  702. if($t == 2)
  703. {
  704. imagealphablending($dim, false);
  705. imagesavealpha($dim,true);
  706. $transparent = imagecolorallocatealpha($dim, 255, 255, 255, 127);
  707. imagefilledrectangle($dim, 0, 0, $w, $h, $transparent);
  708. }
  709. //空白背景
  710. if($t == 1)
  711. {
  712. $wite = ImageColorAllocate($dim,255,255,255);//白色
  713. imagefilledrectangle($dim, 0, 0, $w,$h, $wite);
  714. imagefilledrectangle($dim, $w, $h, 0,0, $wite);
  715. ImageColorTransparent($dim, $wite);
  716. }
  717. return $dim;
  718. }
  719. /*********************
  720. * im库函数
  721. *********************/
  722. /**
  723. * @desc 建立缩略图
  724. * @param *
  725. * @author leo(suwi.bin)
  726. * @date 2012-03-23
  727. */
  728. private function _im_create_thumb()
  729. {
  730. $this->_check('source');
  731. $this->_check('image');
  732. $this->_check('thumb');
  733. $source_x = $this->_image->getImageWidth();
  734. $source_y = $this->_image->getImageHeight();
  735. $source_w = $source_x/$source_y;
  736. $source_h = $source_y/$source_x;
  737. if(!is_array($this->_thumb))
  738. {
  739. $array = explode(',',$this->_thumb);
  740. }
  741. else
  742. {
  743. $array = $this->_thumb;
  744. }
  745. foreach($array as $k => $v)
  746. {
  747. $this->_dest['thumb'][$k] = $this->getName('_' . $v . '_thumb.jpg', $k);
  748. if($this->_setup == true || !file_exists($this->_dest['thumb'][$k]))
  749. {
  750. $offset = explode('_',$v);
  751. if(isset($offset[2]) && $offset[2] == 1)
  752. {
  753. //完全等比例
  754. if($source_x > $offset[0])
  755. {
  756. $dest_x = $offset[0];
  757. $dest_y = $offset[0]*$source_h;
  758. }
  759. elseif($offset[1] > 0 && $source_y > $offset[1])
  760. {
  761. $dest_x = $offset[1]*$source_w;
  762. $dest_y = $offset[1];
  763. }
  764. else
  765. {
  766. $dest_x = $source_x;
  767. $dest_y = $source_y;
  768. }
  769. }
  770. elseif(isset($offset[2]) && $offset[2] == 2)
  771. {
  772. //按照一定比例
  773. if($offset[0] == 0 && $offset[1] > 0)
  774. {
  775. $dest_x = $offset[1]*$source_w;
  776. $dest_y = $offset[1];
  777. }
  778. elseif($offset[1] > 0 && $source_x > $source_y && $source_y > $offset[1])
  779. {
  780. $dest_x = $offset[1]*$source_w;
  781. $dest_y = $offset[1];
  782. }
  783. elseif($source_y > $source_x && $source_x > $offset[0])
  784. {
  785. $dest_x = $offset[0];
  786. $dest_y = $offset[0]*$source_h;
  787. }
  788. elseif($source_y == $source_x && $offset[0] == $offset[1])
  789. {
  790. $dest_x = $offset[0];
  791. $dest_y = $offset[1];
  792. }
  793. elseif($source_x > $source_y && $source_y < $offset[1])
  794. {
  795. $dest_x = $offset[1]*$source_w;
  796. $dest_y = $offset[1];
  797. }
  798. elseif($source_y > $source_x && $source_x < $offset[0])
  799. {
  800. $dest_x = $offset[0];
  801. $dest_y = $offset[0]*$source_h;
  802. }
  803. elseif($source_x > $offset[0])
  804. {
  805. $dest_x = $offset[0];
  806. $dest_y = $offset[0]*$source_h;
  807. }
  808. else
  809. {
  810. $dest_x = $source_x;
  811. $dest_y = $source_y;
  812. }
  813. }
  814. elseif(isset($offset[2]) && $offset[2] == 3)
  815. {
  816. //按照比例缩放,如有多余则留白(或黑...如果实在留不了白的话)
  817. $b = $offset[0]/$offset[1];
  818. $l = $source_x/$source_y;
  819. if($b > $l)
  820. {
  821. $dest_x = $offset[1]*$source_w;
  822. $dest_y = $offset[1];
  823. }
  824. else
  825. {
  826. $dest_x = $offset[0];
  827. $dest_y = $offset[0]*$source_h;
  828. }
  829. }
  830. elseif(isset($offset[2]) && $offset[2] == 4)
  831. {
  832. //按照一定比例
  833. if($offset[0] == 0 && $offset[1] > 0)
  834. {
  835. $dest_x = $offset[1]*$source_w;
  836. $dest_y = $offset[1];
  837. }
  838. elseif($offset[1] > 0 && $source_x > $source_y && $source_y >= $offset[1])
  839. {
  840. $dest_x = $offset[1]*$source_w;
  841. $dest_y = $offset[1];
  842. }
  843. elseif($source_y > $source_x && $source_x >= $offset[0])
  844. {
  845. $dest_x = $offset[0];
  846. $dest_y = $offset[0]*$source_h;
  847. }
  848. elseif($source_y == $source_x && $offset[0] < $offset[1])
  849. {
  850. $dest_x = $offset[1]*$source_w;
  851. $dest_y = $offset[1];
  852. }
  853. elseif($source_y == $source_x && $offset[0] > $offset[1])
  854. {
  855. $dest_x = $offset[0];
  856. $dest_y = $offset[0]*$source_h;
  857. }
  858. elseif($source_y == $source_x && $offset[0] == $offset[1])
  859. {
  860. $dest_x = $offset[0];
  861. $dest_y = $offset[1];
  862. }
  863. elseif($source_x > $source_y && $source_y < $offset[1])
  864. {
  865. $dest_x = $offset[1]*$source_w;
  866. $dest_y = $offset[1];
  867. }
  868. elseif($source_y > $source_x && $source_x < $offset[0])
  869. {
  870. $dest_x = $offset[0];
  871. $dest_y = $offset[0]*$source_h;
  872. }
  873. else
  874. {
  875. $dest_x = $source_x;
  876. $dest_y = $source_y;
  877. }
  878. }
  879. else
  880. {
  881. //直接放大和缩小
  882. $dest_x = $offset[0];
  883. $dest_y = $offset[1];
  884. }
  885. //echo $dest_y;die;
  886. $this->_image = new Imagick($this->_source);
  887. $this->_image->thumbnailImage($dest_x, $dest_y);
  888. if(isset($offset[2]) && $offset[2] == 3)
  889. {
  890. /* 按照缩略图大小创建一个有颜色的图片 */
  891. $canvas = new Imagick();
  892. $color = new ImagickPixel("white");
  893. $canvas->newImage($offset[0], $offset[1], $color, 'png');
  894. //$canvas->paintfloodfillimage('transparent',2000,NULL,0,0);
  895. /* 计算高度 */
  896. $x = ($offset[0] - $dest_x)/2;
  897. $y = ($offset[1] - $dest_y)/2;
  898. /* 合并图片 */
  899. $canvas->compositeImage($this->_image, imagick::COMPOSITE_OVER, $x, $y);
  900. $canvas->setCompression(Imagick::COMPRESSION_JPEG);
  901. $canvas->setCompressionQuality(100);
  902. $canvas->writeImage($this->_dest['thumb'][$k]);
  903. if(isset($offset[3]) && $offset[3] && $this->_dest['thumb'][$k])
  904. {
  905. $offset[3] = $offset[3] * 1024;
  906. $size = abs(filesize($this->_dest['thumb'][$k]));
  907. if($size > $offset[3])
  908. {
  909. $this->_compress($canvas, $offset[3], 80, $this->_dest['thumb'][$k]);
  910. }
  911. }
  912. $canvas = false;
  913. }
  914. else
  915. {
  916. $this->_image->setCompression(Imagick::COMPRESSION_JPEG);
  917. $this->_image->setCompressionQuality(90);
  918. $this->_image->writeImage($this->_dest['thumb'][$k]);
  919. }
  920. }
  921. }
  922. $this->_name = false;
  923. //$this->_image = false;
  924. }
  925. /**
  926. * @desc 建立原图压缩图
  927. * @param *
  928. * @author leo(suwi.bin)
  929. * @date 2012-03-23
  930. */
  931. private function _im_create_compress()
  932. {
  933. $this->_check('source');
  934. $this->_check('quality');
  935. $this->_dest['compress'] = $this->getName('_compress.jpg');
  936. if(strstr($this->_dest['compress'], '.png'))
  937. {
  938. $this->_image->stripImage();
  939. $this->_image->setImageType(Imagick::IMGTYPE_PALETTE);
  940. $this->_image->writeImage($this->_dest['compress']);
  941. }
  942. else
  943. {
  944. $this->_image->setCompression(Imagick::COMPRESSION_JPEG);
  945. $this->_image->setImageCompressionQuality($this->_quality);
  946. $this->_image->writeImage($this->_dest['compress']);
  947. }
  948. $this->_image = false;
  949. }
  950. /**
  951. * @desc 对图片进行压缩
  952. * @param *
  953. * @author leo(suwi.bin)
  954. * @date 2012-03-23
  955. */
  956. private function _compress($canvas, $max, $num, $file)
  957. {
  958. $num = $num - 10;
  959. $temp = $file . '_temp_'.$num.'.jpg';
  960. $canvas->setCompressionQuality($num);
  961. $canvas->stripImage();
  962. $canvas->writeImage($temp);
  963. $size = abs(filesize($temp));
  964. if($size > $max && $num > 0)
  965. {
  966. unlink($temp);
  967. return $this->_compress($canvas, $max, $num, $file);
  968. }
  969. else
  970. {
  971. $canvas->destroy();
  972. copy($temp, $file);
  973. unlink($temp);
  974. }
  975. }
  976. /**
  977. * @desc 建立裁切图
  978. * @param *
  979. * @author leo(suwi.bin)
  980. * @date 2012-03-23
  981. */
  982. private function _im_create_crop()
  983. {
  984. $this->_check('source');
  985. $this->_check('image');
  986. $this->_check('crop');
  987. if(!is_array($this->_crop))
  988. {
  989. $array = explode(',',$this->_crop);
  990. }
  991. else
  992. {
  993. $array = $this->_crop;
  994. }
  995. foreach($array as $k => $v)
  996. {
  997. if($this->_name)
  998. {
  999. $this->_dest['crop'][$k] = $this->getName('_' . $v . '_crop.jpg', $k);
  1000. }
  1001. else
  1002. {
  1003. $this->_dest['crop'][$k] = (isset($this->_dest['thumb'][$k]) && $this->_dest['thumb'][$k]) ? $this->_dest['thumb'][$k] : $this->getName('_' . $v . '_crop.jpg', $k);
  1004. }
  1005. if($this->_dropSize)
  1006. {
  1007. $this->_image = new Imagick($this->_dest['crop'][$k]);
  1008. $source_x = $this->_image->getImageWidth();
  1009. $source_y = $this->_image->getImageHeight();
  1010. }
  1011. if($this->_setup == true || !file_exists($this->_dest['crop'][$k]))
  1012. {
  1013. $offset = explode('_',$v);
  1014. if(isset($this->_dropSize[$k]) && $this->_dropSize[$k])
  1015. {
  1016. $offset[0] += $this->_dropSize[$k];
  1017. $offset[1] += $this->_dropSize[$k];
  1018. }
  1019. if($this->_position)
  1020. {
  1021. # 加入根据百分比计算裁图
  1022. if($this->_position[0] <= 0)
  1023. {
  1024. $this->_position[0] = $source_x/2 - $offset[0]/2;
  1025. }
  1026. elseif(strstr($this->_position[0], '%'))
  1027. {
  1028. $this->_position[0] = $source_x * intval(str_replace('%', '', $this->_position[0]))/100;
  1029. }
  1030. if($this->_position[1] <= 0)
  1031. {
  1032. $this->_position[1] = $source_y/2 - $offset[1]/2;
  1033. }
  1034. elseif(strstr($this->_position[1], '%'))
  1035. {
  1036. $this->_position[1] = $source_y * intval(str_replace('%', '', $this->_position[1]))/100;
  1037. }
  1038. $x = $this->_position[0];
  1039. $y = $this->_position[1];
  1040. }
  1041. else
  1042. {
  1043. $x = $source_x/2 - $offset[0]/2;
  1044. $y = $source_y/2 - $offset[1]/2;
  1045. }
  1046. if($x < 0)
  1047. {
  1048. $x = 0;
  1049. }
  1050. if($y < 0)
  1051. {
  1052. $y = 0;
  1053. }
  1054. if($this->_imageType == 'gif')
  1055. {
  1056. $this->_im_gif($offset[0], $offset[1], $x, $y);
  1057. }
  1058. else
  1059. {
  1060. $this->_image->cropImage($offset[0], $offset[1], $x, $y);
  1061. }
  1062. if(isset($offset[2]) && $offset[2] == 3 && isset($offset[3]) && $offset[3] > 0)
  1063. {
  1064. $this->_image->writeImage($this->_dest['crop'][$k]);
  1065. $offset[3] = $offset[3] * 1024;
  1066. $size = abs(filesize($this->_dest['crop'][$k]));
  1067. if($size > $offset[3])
  1068. {
  1069. $this->_compress($this->_image, $offset[3], 80, $this->_dest['crop'][$k]);
  1070. }
  1071. }
  1072. else
  1073. {
  1074. $this->_image->setCompression(Imagick::COMPRESSION_JPEG);
  1075. $this->_image->setCompressionQuality(90);
  1076. $this->_image->writeImage($this->_dest['crop'][$k]);
  1077. }
  1078. }
  1079. }
  1080. $this->_image = false;
  1081. }
  1082. private function _im_gif($w, $h, $x, $y, $d = false)
  1083. {
  1084. $canvas = new Imagick();
  1085. $images = $this->_image->coalesceImages();
  1086. foreach($images as $frame)
  1087. {
  1088. $img = new Imagick();
  1089. $img->readImageBlob($frame);
  1090. if($d != false)
  1091. {
  1092. $img->drawImage($d);
  1093. }
  1094. else
  1095. {
  1096. $img->cropImage($w, $h, $x, $y);
  1097. }
  1098. $canvas->addImage($img);
  1099. $canvas->setImageDelay($img->getImageDelay());
  1100. if($d == false) $canvas->setImagePage($w, $h, 0, 0);
  1101. }
  1102. $this->_image->destroy();
  1103. $this->_image = $canvas;
  1104. }
  1105. /**
  1106. * @desc 建立水印
  1107. * @param *
  1108. * @author leo(suwi.bin)
  1109. * @date 2012-03-23
  1110. */
  1111. private function _im_create_mark()
  1112. {
  1113. $this->_check('source');
  1114. $this->_check('image');
  1115. $this->_check('mark', 'water');
  1116. $this->_check('mark', 'position');
  1117. $this->_dest['mark'] = $this->getName('_mark.jpg');
  1118. if($this->_setup == true || !file_exists($this->_dest['mark']))
  1119. {
  1120. $water = new Imagick($this->_mark['water']);
  1121. $draw = new ImagickDraw();
  1122. $source_x = $this->_image->getImageWidth();
  1123. $source_y = $this->_image->getImageHeight();
  1124. $water_x = $water->getImageWidth();
  1125. $water_y = $water->getImageHeight();
  1126. $xy = $this->_get_mark($source_x, $source_y, $water_x, $water_y);
  1127. $draw->composite($water->getImageCompose(), $xy[0], $xy[1], $water_x, $water_y, $water);
  1128. if($this->_imageType == 'gif')
  1129. {
  1130. $this->_im_gif(0, 0, 0, 0, $draw);
  1131. }
  1132. else
  1133. {
  1134. $this->_image->drawImage($draw);
  1135. }
  1136. $this->_image->writeImage($this->_dest['mark']);
  1137. }
  1138. $this->_image = false;
  1139. }
  1140. /**
  1141. * @desc 建立文字
  1142. * @param *
  1143. * @author leo(suwi.bin)
  1144. * @date 2012-03-23
  1145. */
  1146. private function _im_create_txt()
  1147. {
  1148. $this->_check('source');
  1149. $this->_check('image');
  1150. //$this->_check('txt','file');
  1151. $this->_check('txt','color');
  1152. $this->_check('txt','size');
  1153. $this->_check('txt','angle');
  1154. $this->_check('txt','name');
  1155. //$this->_check('txt','left');
  1156. //$this->_check('txt','top');
  1157. //$this->_check('txt','bgcolor');
  1158. //$this->_check('txt','font');
  1159. $this->_dest['txt'] = isset($this->_txt['file']) ? $this->_txt['file'] : $this->getName('_txt.jpg');
  1160. if($this->_setup == true || !file_exists($this->_dest['txt']))
  1161. {
  1162. $draw = new ImagickDraw();
  1163. if(isset($this->_txt['font'])) $draw->setFont($this->_txt['font']);
  1164. if(isset($this->_txt['size'])) $draw->setFontSize($this->_txt['size']);
  1165. if(isset($this->_txt['color'])) $draw->setFillColor($this->_txt['color']);
  1166. if(isset($this->_txt['bgcolor'])) $draw->setTextUnderColor($this->_txt['bgcolor']);
  1167. if($this->_imageType == 'gif')
  1168. {
  1169. foreach($this->_image as $frame)
  1170. {
  1171. $frame->annotateImage($draw, 0, 0, $this->_txt['angle'], $this->_txt['name']);
  1172. }
  1173. }
  1174. else
  1175. {
  1176. $this->_image->annotateImage($draw, 0, 0, $this->_txt['angle'], $this->_txt['name']);
  1177. }
  1178. $this->_image->writeImage($this->_dest['txt']);
  1179. }
  1180. $this->_image = false;
  1181. }
  1182. private function _get_mark($source_x, $source_y, $water_x, $water_y)
  1183. {
  1184. $this->_check('mark', 'position');
  1185. $l = 0;
  1186. $t = 0;
  1187. $state = true;
  1188. if($this->_mark['position'])
  1189. {
  1190. switch($this->_mark['position'])
  1191. {
  1192. case 1:
  1193. //左上
  1194. break;
  1195. case 2:
  1196. //左下
  1197. $t = $source_y - $water_y;
  1198. break;
  1199. case 3:
  1200. //右上
  1201. $l = $source_x - $water_x;
  1202. break;
  1203. case 4:
  1204. //右下
  1205. $l = $source_x - $water_x;
  1206. $t = $source_y - $water_y;
  1207. break;
  1208. case 5:
  1209. //中间
  1210. $l = $source_x/2 - $water_x/2;
  1211. $t = $source_y/2 - $water_y/2;
  1212. break;
  1213. default :
  1214. $state = false;
  1215. break;
  1216. }
  1217. }
  1218. return array($l, $t, $state);
  1219. }
  1220. /**
  1221. * @desc 判断是否网络文件,如果是,则生成一个本地路径
  1222. * @param $file 网络文件地址
  1223. * @param $path 生成的路径,默认为项目根目录下的'data/upload'
  1224. * @author leo(suwi.bin)
  1225. * @date 2012-03-23
  1226. */
  1227. private function _setFileName($file, $path = '/data/upload/')
  1228. {
  1229. if(strstr($file, 'http://'))
  1230. {
  1231. $array = explode('/', $file);
  1232. $filename = $array[count($array)-1];
  1233. $file = siscon::path(siscon::path(siscon::path(siscon::path(PROJECT_ROOT_PATH . $path) . date("Y") . '/') . date("m") . '/') . date("d") . '/') . $filename;
  1234. }
  1235. return $file;
  1236. }
  1237. /**
  1238. * @desc 对网络文件进行拷贝复制到本地
  1239. * @param $file 网络文件地址
  1240. * @param $path 生成的路径,默认为项目根目录下的'data/upload'
  1241. * @author leo(suwi.bin)
  1242. * @date 2012-03-23
  1243. */
  1244. public function copyFile($file, $path = '/data/upload/')
  1245. {
  1246. if(strstr($file, 'http://'))
  1247. {
  1248. $new = $this->_setFileName($file);
  1249. if(!is_file($new))
  1250. {
  1251. $content = file_get_contents($file);
  1252. file_put_contents($new, $content);
  1253. }
  1254. $file = $new;
  1255. }
  1256. return $file;
  1257. }
  1258. }