Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- Class AutoIcon {
- protected $host = "127.0.0.1";
- protected $user = "root";
- protected $pass = "1234";
- protected $port = 22;
- protected $iconsDir = "/var/www/lk/icons/";
- protected $cpwFilesDir = "/patcher/files/new/element/surfaces/iconset/";
- protected $cpwDir = "/patcher/";
- protected $serverID = 10;
- var $ssh;
- var $iconGuildTxt;
- public $SizeX, $SizeY, $NumX, $NumY;
- public $iconListFile = array(
- "serverID",
- "clanID"
- );
- Function __construct() {
- // Открываем новое ssh соединение
- $this->SSHConnect();
- // Конвертируем dds картинку в png и прописываем всей папке права 777, для возможности чтения и записи из скрипта
- $this->SSHCommand("convert ".$this->cpwFilesDir."iconlist_guild.dds ".$this->cpwFilesDir."iconlist_guild.png");
- $this->SSHCommand("chmod 777 -R ".$this->cpwFilesDir);
- // Читаем список иконок заодно и размер картинки и размер поля с изображениями
- $this->iconGuildTxt = fopen($this->cpwFilesDir."iconlist_guild.txt", "r");
- $this->SizeX = fgets($this->iconGuildTxt);
- $this->SizeY = fgets($this->iconGuildTxt);
- $this->NumX = fgets($this->iconGuildTxt);
- $this->NumY = fgets($this->iconGuildTxt);
- $a = 0;
- while (!feof($this->iconGuildTxt)) {
- $buffer = fgets($this->iconGuildTxt);
- $tmp = explode("_", $buffer);
- if (count($tmp) > 1 && count($tmp) < 3) {
- $this->iconListFile[$a] = array("serverID" => $tmp[0], "clanID" => preg_replace('/[^0-9,]/', '', $tmp[1]));
- $a++;
- } else {
- $tmp = explode("-", $buffer);
- if (count($tmp) > 1 && count($tmp) < 3) {
- $this->iconListFile[$a] = array("serverID" => $tmp[0], "clanID" => preg_replace('/[^0-9,]/', '', $tmp[1]));
- $a++;
- } else {
- $tmp = explode(" ", $buffer);
- if (count($tmp) > 1 && count($tmp) < 3) {
- $this->iconListFile[$a] = array("serverID" => $tmp[0], "clanID" => preg_replace('/[^0-9,]/', '', $tmp[1]));
- $a++;
- }
- }
- }
- }
- fclose($this->iconGuildTxt);
- // Получаем список иконок кланов которым нужно поставить, ид клана указывается в названии файла, учитываем что они должны быть ТОЛЬКО В PNG
- $fileList = scandir($this->iconsDir);
- // Список с id кланов которым нужны иконки
- foreach ($fileList as $key) {
- if (strlen($key) > 3) {
- $idList[] = str_replace(".png", "", $key);
- }
- }
- // Открываем файл png
- $src = imagecreatefrompng($this->cpwFilesDir."iconlist_guild.png");
- // Теперь самое сложно, если у клана уже есть иконка то нужно ее заменить, если нет то добавить в конец новую
- foreach ($idList as $key) {
- $isset = false;
- $pos = 0;
- for ($a = 0; $a < count($this->iconListFile); $a++) {
- if ($this->iconListFile[$a]["clanID"] == $key && $this->iconListFile[$a]["serverID"] == $this->serverID) {
- $pos = $a;
- $isset = true;
- break;
- }
- }
- if ($isset) {
- // Заменяем существующую
- $y = (preg_replace('/\..*/','', $pos / $this->NumX)) * $this->SizeX;
- $x = $pos % $this->NumX * $this->SizeY;
- $dst = imagecreatefrompng($this->iconsDir.$key.".png");
- // Создаем пустое изображение с прозрачным фоном, что бы заменить существующую иконку клана
- $null = imagecreate($this->SizeX, $this->SizeY);
- imageAlphaBlending($src, false);
- imageSaveAlpha($src, true);
- // Вставляем сначало пустое изображение, а затем иконку клана
- imagecopyresampled($src, $null, $x, $y, 0, 0, (int)$this->SizeX, (int)$this->SizeY, (int)$this->SizeX, (int)$this->SizeY);
- imagecopyresampled($src, $dst, $x, $y, 0, 0, (int)$this->SizeX, (int)$this->SizeY, (int)$this->SizeX, (int)$this->SizeY);
- imageAlphaBlending($src, false);
- imageSaveAlpha($src, true);
- } else {
- // Вставляем новую
- $y = preg_replace('/\..*/','', count($this->iconListFile) / $this->NumY) * $this->SizeY;
- $x = count($this->iconListFile) % $this->NumY * $this->SizeX;
- $dst = imagecreatefrompng($this->iconsDir.$key.".png");
- // Вставляем иконку клана
- imagecopyresampled($src, $dst, $x, $y, 0, 0, (int)$this->SizeX, (int)$this->SizeY, (int)$this->SizeX, (int)$this->SizeY);
- imageAlphaBlending($src, false);
- imageSaveAlpha($src, true);
- file_put_contents($this->cpwFilesDir."iconlist_guild.txt", PHP_EOL.$this->serverID."_".$key.".dds", FILE_APPEND);
- $this->iconListFile[count($this->iconListFile)] = array("serverID" => $this->serverID, "clanID" => $key);
- }
- }
- // Сохраеняем png, затем конвертируем обратно в dds и удаляем png файл, что бы он не попал в обнову
- imagepng($src, $this->cpwFilesDir."iconlist_guild.png");
- $this->SSHCommand("convert ".$this->cpwFilesDir."iconlist_guild.png ".$this->cpwFilesDir."iconlist_guild.dds");
- $this->SSHCommand("rm ".$this->cpwFilesDir."iconlist_guild.png");
- // Ну и создаем обновление которое добавит наши иконки
- $this->SSHCommand("cd ".$this->cpwDir." ./cpw new");
- }
- Function SSHConnect() {
- if(!($this->ssh = ssh2_connect($this->host, $this->port))) {
- die ("No connection");
- } else {
- ssh2_auth_password($this->ssh, $this->user, $this->pass);
- }
- }
- Function SSHCommand($cmd) {
- ssh2_exec($this->ssh, $cmd);
- }
- Function __destruct() {
- unset($this->ssh);
- }
- }
- new AutoIcon();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement