diff --git a/app/Http/Controllers/SkinlibController.php b/app/Http/Controllers/SkinlibController.php index 99120973..36b23eaf 100644 --- a/app/Http/Controllers/SkinlibController.php +++ b/app/Http/Controllers/SkinlibController.php @@ -14,6 +14,7 @@ use Illuminate\Http\Request; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Log; use Illuminate\Validation\Rule; +use Intervention\Image\Facades\Image; use League\CommonMark\GithubFlavoredMarkdownConverter; use Storage; @@ -263,19 +264,11 @@ class SkinlibController extends Controller } } - $image = imagecreatefrompng($file); - $imageSanitized = imagecreatetruecolor($size[0], $size[1]); - imagealphablending($imageSanitized, false); - imagesavealpha($imageSanitized, true); - imagecopy($imageSanitized, $image, 0, 0, 0, 0, $size[0], $size[1]); + $image = Image::make($file); + $sanitized = $image->encode('png', 100)->getEncoded(); - ob_start(); - imagepng($imageSanitized); - $fileSanitized = ob_get_contents(); - ob_end_clean(); - - $hash = hash('sha256', $fileSanitized); - $hash = $filter->apply('uploaded_texture_hash', $hash, [$fileSanitized]); + $hash = hash('sha256', $image->encoded); + $hash = $filter->apply('uploaded_texture_hash', $hash, [$image]); /** @var User */ $user = Auth::user(); @@ -291,7 +284,7 @@ class SkinlibController extends Controller return json(trans('skinlib.upload.repeated'), 2, ['tid' => $duplicated->tid]); } - $fileSize = ceil(strlen($fileSanitized) / 1024); + $fileSize = ceil(strlen($sanitized) / 1024); $isPublic = is_string($data['public']) ? $data['public'] === '1' : $data['public']; @@ -306,7 +299,7 @@ class SkinlibController extends Controller return json(trans('skinlib.upload.lack-score'), 1); } - $dispatcher->dispatch('texture.uploading', [$fileSanitized, $name, $hash]); + $dispatcher->dispatch('texture.uploading', [$image, $name, $hash]); $texture = new Texture(); $texture->name = $name; @@ -321,14 +314,14 @@ class SkinlibController extends Controller /** @var FilesystemAdapter */ $disk = Storage::disk('textures'); if ($disk->missing($hash)) { - $disk->put($hash, $fileSanitized); + $disk->put($hash, $sanitized); } $user->score -= $cost; $user->closet()->attach($texture->tid, ['item_name' => $name]); $user->save(); - $dispatcher->dispatch('texture.uploaded', [$texture, $fileSanitized]); + $dispatcher->dispatch('texture.uploaded', [$texture, $image]); return json(trans('skinlib.upload.success', ['name' => $name]), 0, [ 'tid' => $texture->tid, diff --git a/tests/HttpTest/ControllersTest/SkinlibControllerTest.php b/tests/HttpTest/ControllersTest/SkinlibControllerTest.php index 081e8883..9a7f5082 100644 --- a/tests/HttpTest/ControllersTest/SkinlibControllerTest.php +++ b/tests/HttpTest/ControllersTest/SkinlibControllerTest.php @@ -11,6 +11,7 @@ use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Storage; +use Intervention\Image\Image; class SkinlibControllerTest extends TestCase { @@ -453,7 +454,7 @@ class SkinlibControllerTest extends TestCase 'uploaded_texture_hash', function ($hash, $file) use ($texture) { $this->assertEquals($texture->hash, $hash); - $this->assertIsString($file); + $this->assertInstanceOf(Image::class, $file); return true; } @@ -461,7 +462,7 @@ class SkinlibControllerTest extends TestCase Event::assertDispatched( 'texture.uploading', function ($eventName, $payload) use ($texture) { - $this->assertIsString($payload[0]); + $this->assertInstanceOf(Image::class, $payload[0]); $this->assertEquals($texture->name, $payload[1]); $this->assertEquals($texture->hash, $payload[2]); @@ -472,7 +473,7 @@ class SkinlibControllerTest extends TestCase 'texture.uploaded', function ($eventName, $payload) use ($texture) { $this->assertTrue($texture->is($payload[0])); - $this->assertIsString($payload[1]); + $this->assertInstanceOf(Image::class, $payload[1]); return true; }