123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2018/8/14
- * Time: 上午11:15
- */
- namespace room;
- use Log;
- class room_avatar
- {
- static $avatar_relative_path = '/data/upload/room/avatars';
- static $avatar_path = BASE_UPLOAD_PATH . '/room/avatars';
- static $down_path = BASE_UPLOAD_PATH . '/room/avatars/tmp';
- private $mRoomid;
- private $mPath;
- private $mDestImage;
- private $mInImages;
- private $mBaseName;
- public function __construct($roomid)
- {
- $this->mRoomid = $roomid;
- $this->mPath = self::$avatar_path . "/{$roomid}.png";
- $this->mBaseName = "{$roomid}.png";
- $this->mInImages = [];
- $bgfile = self::$avatar_path . "/background.png";
- $this->mDestImage = $this->create_image($bgfile);
- }
- public function make($avatars)
- {
- if(empty($avatars)) return false;
- if(count($avatars) <= 0) return false;
- foreach ($avatars as $avatar)
- {
- $pid = posix_getpid();
- $filename = self::$down_path . "/{$pid}";
- if(file_exists($filename)) unlink($filename);
- if($this->down($avatar,$filename) == false) continue;
- $image = self::create_image($filename);
- if(file_exists($filename)) unlink($filename);
- if($image === false) {
- Log::record("download error path={$avatar} filename={$filename}",Log::ERR);
- continue;
- }
- $this->mInImages[] = $image;
- }
- $count = count($this->mInImages);
- if($count <= 0) return false;
- $posex = $this->layout($count);
- $i = 0;
- foreach ($this->mInImages as $image)
- {
- if($i >= count($posex)) break;
- $pos = $posex[$i];
- imagecopyresized($this->mDestImage, $image, $pos['x'], $pos['y'], 0, 0, $pos['cx'], $pos['cy'], imagesx($image),imagesy($image));
- $i++;
- }
- $ret = imagepng($this->mDestImage, $this->mPath,0);
- if($ret === false) return false;
- return self::$avatar_relative_path . "/{$this->mBaseName}";
- }
- private function down($avatar,$filename)
- {
- $contents = http_request($avatar);
- if(empty($contents)) {
- Log::record("down {$avatar} empty.",Log::ERR);
- return false;
- }
- $file = fopen($filename,'w+');
- fwrite($file,$contents);
- fclose($file);
- return true;
- }
- private function layout($count)
- {
- if($count < 1) return false;
- switch ($count) {
- case 1: return $this->layout_1();
- case 2: return $this->layout_2();
- case 3: return $this->layout_3();
- case 4: return $this->layout_4();
- case 5: return $this->layout_5();
- case 6: return $this->layout_6();
- case 7: return $this->layout_7();
- case 8: return $this->layout_8();
- case 9: return $this->layout_9();
- default:
- return $this->layout_9();
- }
- }
- private function layout_1() {
- $cx = 800; $cy = 800;
- $result[] = ["x" => 0,'y' => 0,'cx' => $cx,'cy' => $cy];
- return $result;
- }
- private function layout_2()
- {
- $top_x = 0;
- $top_y = 195;
- $padding = 20;
- $cx = 370; $cy = 370;
- $result = [];
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- return $result;
- }
- private function layout_3()
- {
- $top_x = 195; $top_y = 0;
- $padding = 20; $cx = 370; $cy = 370;
- $result = [];
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $top_x = 0;
- $top_y = 390;
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- return $result;
- }
- private function layout_4()
- {
- $top_x = 0;
- $top_y = 0;
- $padding = 20;
- $cx = 370;
- $cy = 370;
- $result = [];
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- return $result;
- }
- private function layout_5()
- {
- $top_x = 130;
- $top_y = 130;
- $padding = 20; $cx = 240; $cy = 240;
- $result = [];
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $top_x = 0;
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- return $result;
- }
- private function layout_6()
- {
- $top_x = 0; $top_y = 130;
- $padding = 20; $cx = 240; $cy = 240;
- $result = [];
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- return $result;
- }
- private function layout_7()
- {
- $top_x = 0; $top_y = 0;
- $padding = 20; $cx = 240; $cy = 240;
- $result = [];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
- return $result;
- }
- private function layout_8()
- {
- $top_x = 130; $top_y = 0;
- $padding = 20; $cx = 240; $cy = 240;
- $result = [];
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $top_x = 0; $top_y = 0;
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
- return $result;
- }
- private function layout_9()
- {
- $top_x = 0; $top_y = 0;
- $padding = 20; $cx = 240; $cy = 240;
- $result = [];
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + $padding,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 2*$padding + $cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 2 + $cx,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
- $result[] = ["x" => $top_x + $padding * 3 + 2 * $cx,'y' => $top_y + 3*$padding + 2*$cy,'cx' => $cx,'cy' => $cy];
- return $result;
- }
- private static function create_image($file)
- {
- $imagetype = exif_imagetype($file);
- Log::record("create_image type={$imagetype}",Log::DEBUG);
- if ($imagetype === false) {
- return false;
- }
- switch ($imagetype)
- {
- case IMAGETYPE_GIF:
- $image = @imagecreatefromgif($file);
- break;
- case IMAGETYPE_JPEG:
- case IMAGETYPE_JPEG2000:
- $image = @imagecreatefromjpeg($file);
- break;
- case IMAGETYPE_PNG:
- $image = @imagecreatefrompng($file);
- break;
- case IMAGETYPE_BMP:
- $image = @imagecreatefrombmp($file);
- break;
- default:
- return false;
- }
- return $image;
- }
- }
|