Core.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php namespace Image\Lib\Tool;
  2. use Dever;
  3. class Core
  4. {
  5. protected $im;
  6. protected $source;
  7. protected $cover = false;
  8. public function __construct($source)
  9. {
  10. if ($source) {
  11. $this->source($source);
  12. }
  13. }
  14. public function source($source)
  15. {
  16. if (is_array($source)) {
  17. $this->im = $this->create($source[0], $source[1], 1);
  18. $this->source = $source[2] ?? false;
  19. } else {
  20. $this->im = $this->get($source);
  21. $this->source = $source;
  22. }
  23. return $this;
  24. }
  25. public function cover($cover)
  26. {
  27. $this->cover = $cover;
  28. return $this;
  29. }
  30. protected function check($file)
  31. {
  32. if ($this->cover || !file_exists($file)) {
  33. return true;
  34. }
  35. return false;
  36. }
  37. protected function getDest($name)
  38. {
  39. if (!$this->source) {
  40. $name = md5($name);
  41. $path = array_slice(str_split($name, 2), 0, 3);
  42. $dest = implode(DIRECTORY_SEPARATOR, $path) . DIRECTORY_SEPARATOR . $name . '.jpg';
  43. return Dever::file('image/' . $dest);
  44. } else {
  45. return $this->source . '_' . $name . '.jpg';
  46. }
  47. }
  48. public function getXy($set, $source_x, $source_y, $source_w, $source_h)
  49. {
  50. $offset = explode('_', $set);
  51. if (isset($offset[2]) && $offset[2] == 1) {
  52. //完全等比例
  53. if ($source_x > $offset[0]) {
  54. $dest_x = $offset[0];
  55. $dest_y = $offset[0]*$source_h;
  56. } elseif ($offset[1] > 0 && $source_y > $offset[1]) {
  57. $dest_x = $offset[1]*$source_w;
  58. $dest_y = $offset[1];
  59. } else {
  60. $dest_x = $source_x;
  61. $dest_y = $source_y;
  62. }
  63. } elseif (isset($offset[2]) && $offset[2] == 2) {
  64. //按照一定比例
  65. if ($offset[0] == 0 && $offset[1] > 0) {
  66. $dest_x = $offset[1]*$source_w;
  67. $dest_y = $offset[1];
  68. } elseif ($offset[1] > 0 && $source_x > $source_y && $source_y > $offset[1]) {
  69. $dest_x = $offset[1]*$source_w;
  70. $dest_y = $offset[1];
  71. } elseif ($source_y > $source_x && $source_x > $offset[0]) {
  72. $dest_x = $offset[0];
  73. $dest_y = $offset[0]*$source_h;
  74. } elseif ($source_y == $source_x && $offset[0] == $offset[1]) {
  75. $dest_x = $offset[0];
  76. $dest_y = $offset[1];
  77. } elseif ($source_x > $source_y && $source_y < $offset[1]) {
  78. $dest_x = $offset[1]*$source_w;
  79. $dest_y = $offset[1];
  80. } elseif($source_y > $source_x && $source_x < $offset[0]) {
  81. $dest_x = $offset[0];
  82. $dest_y = $offset[0]*$source_h;
  83. } elseif($source_x > $offset[0]) {
  84. $dest_x = $offset[0];
  85. $dest_y = $offset[0]*$source_h;
  86. } else {
  87. $dest_x = $source_x;
  88. $dest_y = $source_y;
  89. }
  90. } elseif (isset($offset[2]) && $offset[2] == 3) {
  91. //按照比例缩放,如有多余则留白(或黑...如果实在留不了白的话)
  92. $b = $offset[0]/$offset[1];
  93. $l = $source_x/$source_y;
  94. if ($b > $l) {
  95. $dest_x = $offset[1]*$source_w;
  96. $dest_y = $offset[1];
  97. } else {
  98. $dest_x = $offset[0];
  99. $dest_y = $offset[0]*$source_h;
  100. }
  101. } elseif (isset($offset[2]) && $offset[2] == 4) {
  102. //按照一定比例
  103. if ($offset[0] == 0 && $offset[1] > 0) {
  104. $dest_x = $offset[1]*$source_w;
  105. $dest_y = $offset[1];
  106. } elseif($offset[1] > 0 && $source_x > $source_y && $source_y >= $offset[1]) {
  107. $dest_x = $offset[1]*$source_w;
  108. $dest_y = $offset[1];
  109. } elseif ($source_y > $source_x && $source_x >= $offset[0]) {
  110. $dest_x = $offset[0];
  111. $dest_y = $offset[0]*$source_h;
  112. } elseif ($source_y == $source_x && $offset[0] < $offset[1]) {
  113. $dest_x = $offset[1]*$source_w;
  114. $dest_y = $offset[1];
  115. } elseif ($source_y == $source_x && $offset[0] > $offset[1]) {
  116. $dest_x = $offset[0];
  117. $dest_y = $offset[0]*$source_h;
  118. } elseif ($source_y == $source_x && $offset[0] == $offset[1]) {
  119. $dest_x = $offset[0];
  120. $dest_y = $offset[1];
  121. } elseif ($source_x > $source_y && $source_y < $offset[1]) {
  122. $dest_x = $offset[1]*$source_w;
  123. $dest_y = $offset[1];
  124. } elseif ($source_y > $source_x && $source_x < $offset[0]) {
  125. $dest_x = $offset[0];
  126. $dest_y = $offset[0]*$source_h;
  127. } else {
  128. $dest_x = $source_x;
  129. $dest_y = $source_y;
  130. }
  131. } else {
  132. //直接放大和缩小
  133. $dest_x = $offset[0];
  134. $dest_y = $offset[1];
  135. }
  136. return array($dest_x, $dest_y, $offset);
  137. }
  138. public function position($source_x, $source_y, $dest_x, $dest_y, $position, $offset = 0)
  139. {
  140. $left = 0;
  141. $top = 0;
  142. $state = 1;
  143. if ($position && is_array($position)) {
  144. $left = $position[0];
  145. $top = $position[1];
  146. } elseif ($position) {
  147. switch ($position) {
  148. case 1:
  149. //左上
  150. break;
  151. case 2:
  152. //左下
  153. $top = $source_y - $dest_y;
  154. break;
  155. case 3:
  156. //右上
  157. $left = $source_x - $dest_x;
  158. break;
  159. case 4:
  160. //右下
  161. $left = $source_x - $dest_x;
  162. $top = $source_y - $dest_y;
  163. break;
  164. case 5:
  165. //中间
  166. $left = $source_x/2 - $dest_x/2;
  167. $top = $source_y/2 - $dest_y/2;
  168. break;
  169. case 6:
  170. //上中
  171. $left = $source_x/2 - $dest_x/2;
  172. break;
  173. case 7:
  174. //下中
  175. $left = $source_x/2 - $dest_x/2;
  176. $top = $source_y - $dest_y;
  177. break;
  178. case 8:
  179. //左中
  180. $top = $source_y/2 - $dest_y/2;
  181. break;
  182. case 9:
  183. //右中
  184. $left = $source_x - $dest_x;
  185. $top = $source_y/2 - $dest_y/2;
  186. break;
  187. case 10:
  188. //平铺
  189. $left = -1;
  190. $top = -1;
  191. break;
  192. default :
  193. $state = false;
  194. break;
  195. }
  196. }
  197. if ($offset && is_array($offset)) {
  198. $left = $left + $offset[0];
  199. $top = $top + $offset[1];
  200. } else {
  201. $left = $left + $offset;
  202. $top = $top + $offset;
  203. }
  204. return array($left, $top, $state);
  205. }
  206. public function autowrap(&$autowrap, $fontsize, $angle, $fontface, $string, $width)
  207. {
  208. // 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
  209. $content = "";
  210. // 将字符串拆分成一个个单字 保存到数组 letter 中
  211. for ($i=0;$i<mb_strlen($string);$i++) {
  212. $letter[] = mb_substr($string, $i, 1);
  213. }
  214. foreach ($letter as $l) {
  215. $teststr = $content." ".$l;
  216. $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
  217. // 判断拼接后的字符串是否超过预设的宽度
  218. if (($testbox[2] > $width) && ($content !== "")) {
  219. $content .= "\n";
  220. $autowrap += $fontsize;
  221. }
  222. $content .= $l;
  223. }
  224. return $content;
  225. }
  226. }