Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types = 1);
- namespace App\Services\Character;
- use App\Exceptions\User\Character\InvalidImageSizeException;
- use Illuminate\Http\UploadedFile;
- use \PDO;
- /**
- * Class UploadedSkin
- * Represents a user-loaded skin.
- *
- * @author D3lph1 <[email protected]>
- * @package App\Services\Character
- */
- class UploadedSkin
- {
- /**
- * @var UploadedFile
- */
- protected $file;
- /**
- * @var \Intervention\Image\Image
- */
- protected $image;
- /**
- * UploadedSkin constructor.
- *
- * @param UploadedFile $file
- */
- public function __construct(UploadedFile $file)
- {
- $this->file = $file;
- $this->image = \Image::make($file);
- }
- /**
- * Check image size on valid.
- *
- * @param bool $canSetHdSkin
- *
- * @throws InvalidImageSizeException
- * @return bool
- */
- public function validate($canSetHdSkin)
- {
- $height = $this->image->getHeight();
- $width = $this->image->getWidth();
- $ratio = $width / 64;
- $validHeight = $height / $ratio === 32;
- $validWidth = $width / $ratio === 64;
- if ($canSetHdSkin) {
- if ($validHeight && $validWidth && $width <= 1024 && $height <= 512) {
- return true;
- } else {
- throw new InvalidImageSizeException();
- }
- } else {
- if ($validHeight && $validWidth) {
- $uuid = is_auth() ? \Sentinel::getUser()->getUUID() : null;
- $parent = "";
- try {
- $db_settings = array(
- 'host' => '127.0.0.1',
- 'name' => '',
- 'login' => '',
- 'password' => '',
- 'charset' => 'utf8'
- );
- $db = new PDO(
- "mysql:host={$db_settings['host']};dbname={$db_settings['name']}",
- $db_settings['login'],
- $db_settings['password'],
- array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES {$db_settings['charset']}"));
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $order = $db->prepare("SELECT * FROM permissions_inheritance WHERE child=:UUID");
- $order->execute(array(":UUID"=>$uuid));
- $result = $order->fetchAll();
- foreach ($result as $row) {
- if ($row['parent'] === "hdskin1024") {
- $parent = "hdskin1024";
- } else if ($row['parent'] === "hdskin512" && $parent !== "hdskin1024") {
- $parent = "hdskin512";
- } else if ($row['parent'] === "hdskin256" && $parent !== "hdskin1024" && $parent !== "hdskin512") {
- $parent = "hdskin256";
- }
- }
- } catch(PDOException $e) {
- // Отвалилась база данных
- }
- $w = 128;
- $h = 64;
- if ($parent === "hdskin1024") {
- $w = 1024;
- $h = 512;
- }
- if ($parent === "hdskin512") {
- $w = 512;
- $h = 256;
- }
- if ($parent === "hdskin256") {
- $w = 256;
- $h = 128;
- }
- if ($height <= $h && $width <= $w) {
- return true;
- } else {
- throw new InvalidImageSizeException();
- }
- } else {
- throw new InvalidImageSizeException();
- }
- }
- }
- /**
- * Move image file in skins directory.
- *
- * @param string $username
- */
- public function move(string $username)
- {
- $this->file->move(config('l-shop.profile.skins.path'), $username . '.png');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment