Kundello

Untitled

Feb 26th, 2018
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. <?php
  2. declare(strict_types = 1);
  3.  
  4. namespace App\Services\Character;
  5.  
  6. use App\Exceptions\User\Character\InvalidImageSizeException;
  7. use Illuminate\Http\UploadedFile;
  8.  
  9. /**
  10. * Class UploadedSkin
  11. * Represents a user-loaded skin.
  12. *
  13. * @author D3lph1 <[email protected]>
  14. * @package App\Services\Character
  15. */
  16. class UploadedSkin
  17. {
  18. /**
  19. * @var UploadedFile
  20. */
  21. protected $file;
  22.  
  23. /**
  24. * @var \Intervention\Image\Image
  25. */
  26. protected $image;
  27.  
  28. /**
  29. * UploadedSkin constructor.
  30. *
  31. * @param UploadedFile $file
  32. */
  33. public function __construct(UploadedFile $file)
  34. {
  35. $this->file = $file;
  36. $this->image = \Image::make($file);
  37. }
  38.  
  39. /**
  40. * Check image size on valid.
  41. *
  42. * @param bool $canSetHdSkin
  43. *
  44. * @throws InvalidImageSizeException
  45. * @return bool
  46. */
  47. public function validate($canSetHdSkin)
  48. {
  49. $height = $this->image->getHeight();
  50. $width = $this->image->getWidth();
  51. $ratio = $width / 64;
  52.  
  53. $validHeight = $height / $ratio === 32;
  54. $validWidth = $width / $ratio === 64;
  55.  
  56. if ($canSetHdSkin) {
  57. if ($validHeight && $validWidth && $width <= 1024 && $height <= 512) {
  58. return true;
  59. } else {
  60. throw new InvalidImageSizeException();
  61. }
  62. } else {
  63. if ($validHeight && $validWidth) {
  64. if ($height <= 64 && $width <= 128) {
  65. return true;
  66. } else {
  67. throw new InvalidImageSizeException();
  68. }
  69. } else {
  70. throw new InvalidImageSizeException();
  71. }
  72. }
  73. }
  74.  
  75. /**
  76. * Move image file in skins directory.
  77. *
  78. * @param string $username
  79. */
  80. public function move(string $username)
  81. {
  82. $this->file->move(config('l-shop.profile.skins.path'), $username . '.png');
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment