room_avatar.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2018/8/14
  6. * Time: 上午11:15
  7. */
  8. namespace room;
  9. use Log;
  10. class room_avatar
  11. {
  12. static $avatar_relative_path = '/data/upload/room/avatars';
  13. static $avatar_path = BASE_UPLOAD_PATH . '/room/avatars';
  14. static $down_path = BASE_UPLOAD_PATH . '/room/avatars/tmp';
  15. private $mRoomid;
  16. private $mPath;
  17. private $mDestImage;
  18. private $mInImages;
  19. private $mBaseName;
  20. public function __construct($roomid)
  21. {
  22. $this->mRoomid = $roomid;
  23. $this->mPath = self::$avatar_path . "/{$roomid}.png";
  24. $this->mBaseName = "{$roomid}.png";
  25. $this->mInImages = [];
  26. $bgfile = self::$avatar_path . "/background.png";
  27. $this->mDestImage = $this->create_image($bgfile);
  28. }
  29. public function make($avatars)
  30. {
  31. if(empty($avatars)) return false;
  32. if(count($avatars) <= 0) return false;
  33. foreach ($avatars as $avatar)
  34. {
  35. $pid = posix_getpid();
  36. $filename = self::$down_path . "/{$pid}";
  37. if(file_exists($filename)) unlink($filename);
  38. if($this->down($avatar,$filename) == false) continue;
  39. $image = self::create_image($filename);
  40. if(file_exists($filename)) unlink($filename);
  41. if($image === false) {
  42. Log::record("download error path={$avatar} filename={$filename}",Log::ERR);
  43. continue;
  44. }
  45. $this->mInImages[] = $image;
  46. }
  47. $count = count($this->mInImages);
  48. if($count <= 0) return false;
  49. $posex = $this->layout($count);
  50. $i = 0;
  51. foreach ($this->mInImages as $image)
  52. {
  53. if($i >= count($posex)) break;
  54. $pos = $posex[$i];
  55. imagecopyresized($this->mDestImage, $image, $pos['x'], $pos['y'], 0, 0, $pos['cx'], $pos['cy'], imagesx($image),imagesy($image));
  56. $i++;
  57. }
  58. $ret = imagepng($this->mDestImage, $this->mPath,0);
  59. if($ret === false) return false;
  60. return self::$avatar_relative_path . "/{$this->mBaseName}";
  61. }
  62. private function down($avatar,$filename)
  63. {
  64. $contents = http_request($avatar);
  65. if(empty($contents)) {
  66. Log::record("down {$avatar} empty.",Log::ERR);
  67. return false;
  68. }
  69. $file = fopen($filename,'w+');
  70. fwrite($file,$contents);
  71. fclose($file);
  72. return true;
  73. }
  74. private function layout($count)
  75. {
  76. if($count < 1) return false;
  77. switch ($count) {
  78. case 1: return $this->layout_1();
  79. case 2: return $this->layout_2();
  80. case 3: return $this->layout_3();
  81. case 4: return $this->layout_4();
  82. case 5: return $this->layout_5();
  83. case 6: return $this->layout_6();
  84. case 7: return $this->layout_7();
  85. case 8: return $this->layout_8();
  86. case 9: return $this->layout_9();
  87. default:
  88. return $this->layout_9();
  89. }
  90. }
  91. private function layout_1() {
  92. $cx = 800; $cy = 800;
  93. $result[] = ["x" => 0,'y' => 0,'cx' => $cx,'cy' => $cy];
  94. return $result;
  95. }
  96. private function layout_2()
  97. {
  98. $top_x = 0;
  99. $top_y = 195;
  100. $padding = 20;
  101. $cx = 370; $cy = 370;
  102. $result = [];
  103. $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  104. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  105. return $result;
  106. }
  107. private function layout_3()
  108. {
  109. $top_x = 195; $top_y = 0;
  110. $padding = 20; $cx = 370; $cy = 370;
  111. $result = [];
  112. $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  113. $top_x = 0;
  114. $top_y = 390;
  115. $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  116. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  117. return $result;
  118. }
  119. private function layout_4()
  120. {
  121. $top_x = 0;
  122. $top_y = 0;
  123. $padding = 20;
  124. $cx = 370;
  125. $cy = 370;
  126. $result = [];
  127. $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  128. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  129. $result[] = ["x" => $top_x + $padding,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  130. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  131. return $result;
  132. }
  133. private function layout_5()
  134. {
  135. $top_x = 130;
  136. $top_y = 130;
  137. $padding = 20; $cx = 240; $cy = 240;
  138. $result = [];
  139. $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  140. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  141. $top_x = 0;
  142. $result[] = ["x" => $top_x + $padding,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  143. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  144. $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  145. return $result;
  146. }
  147. private function layout_6()
  148. {
  149. $top_x = 0; $top_y = 130;
  150. $padding = 20; $cx = 240; $cy = 240;
  151. $result = [];
  152. $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  153. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  154. $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  155. $result[] = ["x" => $top_x + $padding,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  156. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  157. $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  158. return $result;
  159. }
  160. private function layout_7()
  161. {
  162. $top_x = 0; $top_y = 0;
  163. $padding = 20; $cx = 240; $cy = 240;
  164. $result = [];
  165. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  166. $result[] = ["x" => $top_x + $padding,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  167. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  168. $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  169. $result[] = ["x" => $top_x + $padding,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
  170. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
  171. $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
  172. return $result;
  173. }
  174. private function layout_8()
  175. {
  176. $top_x = 130; $top_y = 0;
  177. $padding = 20; $cx = 240; $cy = 240;
  178. $result = [];
  179. $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  180. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  181. $top_x = 0; $top_y = 0;
  182. $result[] = ["x" => $top_x + $padding,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  183. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  184. $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  185. $result[] = ["x" => $top_x + $padding,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
  186. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
  187. $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
  188. return $result;
  189. }
  190. private function layout_9()
  191. {
  192. $top_x = 0; $top_y = 0;
  193. $padding = 20; $cx = 240; $cy = 240;
  194. $result = [];
  195. $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  196. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  197. $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
  198. $result[] = ["x" => $top_x + $padding,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  199. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  200. $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
  201. $result[] = ["x" => $top_x + $padding,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
  202. $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
  203. $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
  204. return $result;
  205. }
  206. private static function create_image($file)
  207. {
  208. $imagetype = exif_imagetype($file);
  209. Log::record("create_image type={$imagetype}",Log::DEBUG);
  210. if ($imagetype === false) {
  211. return false;
  212. }
  213. switch ($imagetype)
  214. {
  215. case IMAGETYPE_GIF:
  216. $image = @imagecreatefromgif($file);
  217. break;
  218. case IMAGETYPE_JPEG:
  219. case IMAGETYPE_JPEG2000:
  220. $image = @imagecreatefromjpeg($file);
  221. break;
  222. case IMAGETYPE_PNG:
  223. $image = @imagecreatefrompng($file);
  224. break;
  225. case IMAGETYPE_BMP:
  226. $image = @imagecreatefrombmp($file);
  227. break;
  228. default:
  229. return false;
  230. }
  231. return $image;
  232. }
  233. }