Completed
Push — master ( c879ba...b7c46f )
by
unknown
30:20 queued 02:07
created
lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php 1 patch
Indentation   +89 added lines, -89 removed lines patch added patch discarded remove patch
@@ -28,93 +28,93 @@
 block discarded – undo
28 28
  * @template-implements IEventListener<AMetadataEvent>
29 29
  */
30 30
 class GenerateBlurhashMetadata implements IEventListener {
31
-	private const COMPONENTS_X = 4;
32
-	private const COMPONENTS_Y = 3;
33
-
34
-	public function __construct(
35
-		private IPreview $preview,
36
-	) {
37
-	}
38
-
39
-	/**
40
-	 * @throws NotPermittedException
41
-	 * @throws GenericFileException
42
-	 * @throws LockedException
43
-	 */
44
-	public function handle(Event $event): void {
45
-		if (!($event instanceof MetadataLiveEvent)
46
-			&& !($event instanceof MetadataBackgroundEvent)) {
47
-			return;
48
-		}
49
-
50
-		$file = $event->getNode();
51
-		if (!($file instanceof File)) {
52
-			return;
53
-		}
54
-
55
-		$currentEtag = $file->getEtag();
56
-		$metadata = $event->getMetadata();
57
-		if ($metadata->getEtag('blurhash') === $currentEtag) {
58
-			return;
59
-		}
60
-
61
-		// too heavy to run on the live thread, request a rerun as a background job
62
-		if ($event instanceof MetadataLiveEvent) {
63
-			$event->requestBackgroundJob();
64
-			return;
65
-		}
66
-
67
-		if (!str_starts_with($file->getMimetype(), 'image/')) {
68
-			return;
69
-		}
70
-
71
-		// Preview are disabled, so we skip generating the blurhash.
72
-		if (!$this->preview->isAvailable($file)) {
73
-			return;
74
-		}
75
-
76
-		$preview = $this->preview->getPreview($file, 64, 64, cacheResult: false);
77
-		$image = @imagecreatefromstring($preview->getContent());
78
-
79
-		if (!$image) {
80
-			return;
81
-		}
82
-
83
-		$metadata->setString('blurhash', $this->generateBlurHash($image))
84
-			->setEtag('blurhash', $currentEtag);
85
-	}
86
-
87
-	/**
88
-	 * @param GdImage $image
89
-	 *
90
-	 * @return string
91
-	 */
92
-	public function generateBlurHash(GdImage $image): string {
93
-		$width = imagesx($image);
94
-		$height = imagesy($image);
95
-
96
-		$pixels = [];
97
-		for ($y = 0; $y < $height; ++$y) {
98
-			$row = [];
99
-			for ($x = 0; $x < $width; ++$x) {
100
-				$index = imagecolorat($image, $x, $y);
101
-				$colors = imagecolorsforindex($image, $index);
102
-				$row[] = [$colors['red'], $colors['green'], $colors['blue']];
103
-			}
104
-
105
-			$pixels[] = $row;
106
-		}
107
-
108
-		return Blurhash::encode($pixels, self::COMPONENTS_X, self::COMPONENTS_Y);
109
-	}
110
-
111
-	/**
112
-	 * @param IEventDispatcher $eventDispatcher
113
-	 *
114
-	 * @return void
115
-	 */
116
-	public static function loadListeners(IEventDispatcher $eventDispatcher): void {
117
-		$eventDispatcher->addServiceListener(MetadataLiveEvent::class, self::class);
118
-		$eventDispatcher->addServiceListener(MetadataBackgroundEvent::class, self::class);
119
-	}
31
+    private const COMPONENTS_X = 4;
32
+    private const COMPONENTS_Y = 3;
33
+
34
+    public function __construct(
35
+        private IPreview $preview,
36
+    ) {
37
+    }
38
+
39
+    /**
40
+     * @throws NotPermittedException
41
+     * @throws GenericFileException
42
+     * @throws LockedException
43
+     */
44
+    public function handle(Event $event): void {
45
+        if (!($event instanceof MetadataLiveEvent)
46
+            && !($event instanceof MetadataBackgroundEvent)) {
47
+            return;
48
+        }
49
+
50
+        $file = $event->getNode();
51
+        if (!($file instanceof File)) {
52
+            return;
53
+        }
54
+
55
+        $currentEtag = $file->getEtag();
56
+        $metadata = $event->getMetadata();
57
+        if ($metadata->getEtag('blurhash') === $currentEtag) {
58
+            return;
59
+        }
60
+
61
+        // too heavy to run on the live thread, request a rerun as a background job
62
+        if ($event instanceof MetadataLiveEvent) {
63
+            $event->requestBackgroundJob();
64
+            return;
65
+        }
66
+
67
+        if (!str_starts_with($file->getMimetype(), 'image/')) {
68
+            return;
69
+        }
70
+
71
+        // Preview are disabled, so we skip generating the blurhash.
72
+        if (!$this->preview->isAvailable($file)) {
73
+            return;
74
+        }
75
+
76
+        $preview = $this->preview->getPreview($file, 64, 64, cacheResult: false);
77
+        $image = @imagecreatefromstring($preview->getContent());
78
+
79
+        if (!$image) {
80
+            return;
81
+        }
82
+
83
+        $metadata->setString('blurhash', $this->generateBlurHash($image))
84
+            ->setEtag('blurhash', $currentEtag);
85
+    }
86
+
87
+    /**
88
+     * @param GdImage $image
89
+     *
90
+     * @return string
91
+     */
92
+    public function generateBlurHash(GdImage $image): string {
93
+        $width = imagesx($image);
94
+        $height = imagesy($image);
95
+
96
+        $pixels = [];
97
+        for ($y = 0; $y < $height; ++$y) {
98
+            $row = [];
99
+            for ($x = 0; $x < $width; ++$x) {
100
+                $index = imagecolorat($image, $x, $y);
101
+                $colors = imagecolorsforindex($image, $index);
102
+                $row[] = [$colors['red'], $colors['green'], $colors['blue']];
103
+            }
104
+
105
+            $pixels[] = $row;
106
+        }
107
+
108
+        return Blurhash::encode($pixels, self::COMPONENTS_X, self::COMPONENTS_Y);
109
+    }
110
+
111
+    /**
112
+     * @param IEventDispatcher $eventDispatcher
113
+     *
114
+     * @return void
115
+     */
116
+    public static function loadListeners(IEventDispatcher $eventDispatcher): void {
117
+        $eventDispatcher->addServiceListener(MetadataLiveEvent::class, self::class);
118
+        $eventDispatcher->addServiceListener(MetadataBackgroundEvent::class, self::class);
119
+    }
120 120
 }
Please login to merge, or discard this patch.