Kundello

Untitled

Mar 25th, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. <?php
  2. declare(strict_types = 1);
  3.  
  4. namespace App\Services\Character;
  5.  
  6.  
  7. use App\Exceptions\User\Character\InvalidImageSizeException;
  8. use Illuminate\Http\UploadedFile;
  9. use \PDO;
  10.  
  11.  
  12. /**
  13. * Class UploadedSkin
  14. * Represents a user-loaded skin.
  15. *
  16. * @author D3lph1 <[email protected]>
  17. * @package App\Services\Character
  18. */
  19. class UploadedSkin
  20. {
  21. /**
  22. * @var UploadedFile
  23. */
  24. protected $file;
  25.  
  26. /**
  27. * @var \Intervention\Image\Image
  28. */
  29. protected $image;
  30.  
  31. /**
  32. * UploadedSkin constructor.
  33. *
  34. * @param UploadedFile $file
  35. */
  36. public function __construct(UploadedFile $file)
  37. {
  38. $this->file = $file;
  39. $this->image = \Image::make($file);
  40. }
  41.  
  42. /**
  43. * Check image size on valid.
  44. *
  45. * @param bool $canSetHdSkin
  46. *
  47. * @throws InvalidImageSizeException
  48. * @return bool
  49. */
  50. public function validate($canSetHdSkin)
  51. {
  52. $height = $this->image->getHeight();
  53. $width = $this->image->getWidth();
  54. $ratio = $width / 64;
  55.  
  56. $validHeight = $height / $ratio === 32;
  57. $validWidth = $width / $ratio === 64;
  58.  
  59. if ($canSetHdSkin) {
  60. if ($validHeight && $validWidth && $width <= 1024 && $height <= 512) {
  61. return true;
  62. } else {
  63. throw new InvalidImageSizeException();
  64. }
  65. } else {
  66. if ($validHeight && $validWidth) {
  67. $uuid = is_auth() ? \Sentinel::getUser()->getUUID() : null;
  68. $parent = "";
  69.  
  70. try {
  71. $db_settings = array(
  72. 'host' => '127.0.0.1',
  73. 'name' => '',
  74. 'login' => '',
  75. 'password' => '',
  76. 'charset' => 'utf8'
  77. );
  78.  
  79. $db = new PDO(
  80. "mysql:host={$db_settings['host']};dbname={$db_settings['name']}",
  81. $db_settings['login'],
  82. $db_settings['password'],
  83. array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES {$db_settings['charset']}"));
  84.  
  85. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  86.  
  87. $order = $db->prepare("SELECT * FROM permissions_inheritance WHERE child=:UUID");
  88. $order->execute(array(":UUID"=>$uuid));
  89. $result = $order->fetchAll();
  90.  
  91. foreach ($result as $row) {
  92. if ($row['parent'] === "hdskin1024") {
  93. $parent = "hdskin1024";
  94. } else if ($row['parent'] === "hdskin512" && $parent !== "hdskin1024") {
  95. $parent = "hdskin512";
  96. } else if ($row['parent'] === "hdskin256" && $parent !== "hdskin1024" && $parent !== "hdskin512") {
  97. $parent = "hdskin256";
  98. }
  99. }
  100. } catch(PDOException $e) {
  101. // Отвалилась база данных
  102. }
  103.  
  104. $w = 128;
  105. $h = 64;
  106.  
  107. if ($parent === "hdskin1024") {
  108. $w = 1024;
  109. $h = 512;
  110. }
  111.  
  112. if ($parent === "hdskin512") {
  113. $w = 512;
  114. $h = 256;
  115. }
  116.  
  117. if ($parent === "hdskin256") {
  118. $w = 256;
  119. $h = 128;
  120. }
  121.  
  122. if ($height <= $h && $width <= $w) {
  123. return true;
  124. } else {
  125. throw new InvalidImageSizeException();
  126. }
  127. } else {
  128. throw new InvalidImageSizeException();
  129. }
  130. }
  131. }
  132.  
  133. /**
  134. * Move image file in skins directory.
  135. *
  136. * @param string $username
  137. */
  138. public function move(string $username)
  139. {
  140. $this->file->move(config('l-shop.profile.skins.path'), $username . '.png');
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment