Img.php 47 KB

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