Advertisement
Guest User

Untitled

a guest
Apr 12th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.81 KB | None | 0 0
  1. <?php
  2. Class AutoIcon {
  3.    
  4.     protected $host = "127.0.0.1";
  5.     protected $user = "root";
  6.     protected $pass = "1234";
  7.     protected $port = 22;
  8.  
  9.     protected $iconsDir = "/var/www/lk/icons/";
  10.     protected $cpwFilesDir = "/patcher/files/new/element/surfaces/iconset/";
  11.     protected $cpwDir = "/patcher/";
  12.     protected $serverID = 10;
  13.    
  14.     var $ssh;
  15.    
  16.     var $iconGuildTxt;
  17.    
  18.     public $SizeX, $SizeY, $NumX, $NumY;
  19.     public $iconListFile = array(
  20.         "serverID",
  21.         "clanID"
  22.     );
  23.    
  24.    
  25.     Function __construct() {
  26.         // Открываем новое ssh соединение
  27.         $this->SSHConnect();
  28.         // Конвертируем dds картинку в png и прописываем всей папке права 777, для возможности чтения и записи из скрипта
  29.         $this->SSHCommand("convert ".$this->cpwFilesDir."iconlist_guild.dds ".$this->cpwFilesDir."iconlist_guild.png");
  30.         $this->SSHCommand("chmod 777 -R ".$this->cpwFilesDir);
  31.         // Читаем список иконок заодно и размер картинки и размер поля с изображениями
  32.         $this->iconGuildTxt = fopen($this->cpwFilesDir."iconlist_guild.txt", "r");
  33.         $this->SizeX = fgets($this->iconGuildTxt);
  34.         $this->SizeY = fgets($this->iconGuildTxt);
  35.         $this->NumX = fgets($this->iconGuildTxt);
  36.         $this->NumY = fgets($this->iconGuildTxt);
  37.         $a = 0;
  38.         while (!feof($this->iconGuildTxt)) {
  39.             $buffer = fgets($this->iconGuildTxt);
  40.             $tmp = explode("_", $buffer);
  41.             if (count($tmp) > 1 && count($tmp) < 3) {
  42.                 $this->iconListFile[$a] = array("serverID" => $tmp[0], "clanID" => preg_replace('/[^0-9,]/', '', $tmp[1]));
  43.                 $a++;
  44.             } else {
  45.                 $tmp = explode("-", $buffer);
  46.                 if (count($tmp) > 1 && count($tmp) < 3) {
  47.                     $this->iconListFile[$a] = array("serverID" => $tmp[0], "clanID" => preg_replace('/[^0-9,]/', '', $tmp[1]));
  48.                     $a++;
  49.                 } else {
  50.                     $tmp = explode(" ", $buffer);
  51.                     if (count($tmp) > 1 && count($tmp) < 3) {
  52.                         $this->iconListFile[$a] = array("serverID" => $tmp[0], "clanID" => preg_replace('/[^0-9,]/', '', $tmp[1]));
  53.                         $a++;
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.         fclose($this->iconGuildTxt);
  59.         // Получаем список иконок кланов которым нужно поставить, ид клана указывается в названии файла, учитываем что они должны быть ТОЛЬКО В PNG
  60.         $fileList = scandir($this->iconsDir);
  61.         // Список с id кланов которым нужны иконки
  62.         foreach ($fileList as $key) {
  63.             if (strlen($key) > 3) {
  64.                 $idList[] = str_replace(".png", "", $key);
  65.             }
  66.         }
  67.         // Открываем файл png
  68.         $src = imagecreatefrompng($this->cpwFilesDir."iconlist_guild.png");
  69.         // Теперь самое сложно, если у клана уже есть иконка то нужно ее заменить, если нет то добавить в конец новую
  70.         foreach ($idList as $key) {
  71.             $isset = false;
  72.             $pos = 0;
  73.             for ($a = 0; $a < count($this->iconListFile); $a++) {
  74.                 if ($this->iconListFile[$a]["clanID"] == $key && $this->iconListFile[$a]["serverID"] == $this->serverID) {
  75.                     $pos = $a;
  76.                     $isset = true;
  77.                     break;
  78.                 }
  79.             }
  80.             if ($isset) {
  81.                 // Заменяем существующую
  82.                 $y = (preg_replace('/\..*/','', $pos / $this->NumX)) * $this->SizeX;
  83.                 $x = $pos % $this->NumX * $this->SizeY;
  84.                 $dst = imagecreatefrompng($this->iconsDir.$key.".png");
  85.                 // Создаем пустое изображение с прозрачным фоном, что бы заменить существующую иконку клана
  86.                 $null = imagecreate($this->SizeX, $this->SizeY);
  87.                 imageAlphaBlending($src, false);
  88.                 imageSaveAlpha($src, true);
  89.                 // Вставляем сначало пустое изображение, а затем иконку клана
  90.                 imagecopyresampled($src, $null, $x, $y, 0, 0, (int)$this->SizeX, (int)$this->SizeY, (int)$this->SizeX, (int)$this->SizeY);
  91.                 imagecopyresampled($src, $dst, $x, $y, 0, 0, (int)$this->SizeX, (int)$this->SizeY, (int)$this->SizeX, (int)$this->SizeY);
  92.                 imageAlphaBlending($src, false);
  93.                 imageSaveAlpha($src, true);
  94.  
  95.             } else {
  96.                 // Вставляем новую
  97.                 $y = preg_replace('/\..*/','', count($this->iconListFile) / $this->NumY) * $this->SizeY;
  98.                 $x = count($this->iconListFile) % $this->NumY * $this->SizeX;
  99.                 $dst = imagecreatefrompng($this->iconsDir.$key.".png");
  100.                 // Вставляем иконку клана
  101.                 imagecopyresampled($src, $dst, $x, $y, 0, 0, (int)$this->SizeX, (int)$this->SizeY, (int)$this->SizeX, (int)$this->SizeY);
  102.                 imageAlphaBlending($src, false);
  103.                 imageSaveAlpha($src, true);
  104.                 file_put_contents($this->cpwFilesDir."iconlist_guild.txt", PHP_EOL.$this->serverID."_".$key.".dds", FILE_APPEND);
  105.                 $this->iconListFile[count($this->iconListFile)] = array("serverID" => $this->serverID, "clanID" => $key);
  106.             }
  107.         }
  108.         // Сохраеняем png, затем конвертируем обратно в dds и удаляем png файл, что бы он не попал в обнову
  109.         imagepng($src, $this->cpwFilesDir."iconlist_guild.png");
  110.         $this->SSHCommand("convert ".$this->cpwFilesDir."iconlist_guild.png ".$this->cpwFilesDir."iconlist_guild.dds");
  111.         $this->SSHCommand("rm ".$this->cpwFilesDir."iconlist_guild.png");
  112.         // Ну и создаем обновление которое добавит наши иконки
  113.         $this->SSHCommand("cd ".$this->cpwDir." ./cpw new");
  114.     }
  115.  
  116.  
  117.     Function SSHConnect() {
  118.         if(!($this->ssh = ssh2_connect($this->host, $this->port))) {
  119.             die ("No connection");
  120.         } else {
  121.             ssh2_auth_password($this->ssh, $this->user, $this->pass);
  122.         }
  123.     }
  124.    
  125.     Function SSHCommand($cmd) {
  126.         ssh2_exec($this->ssh, $cmd);
  127.     }
  128.    
  129.     Function __destruct() {
  130.         unset($this->ssh);
  131.     }
  132. }
  133.  
  134. new AutoIcon();
  135. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement