+ $image = New Image($this->asString());
+ if (empty($image) || !$this->isValid()) {
+ return '';
+ }
+
+ $width = $image->getWidth();
+ $height = $image->getHeight();
+
+ if (max($width, $height) > 90) {
+ $image->scaleDown(90);
+ $width = $image->getWidth();
+ $height = $image->getHeight();
+ }
+
+ if (empty($width) || empty($height)) {
+ return '';
+ }
+
+ $pixels = [];
+ for ($y = 0; $y < $height; ++$y) {
+ $row = [];
+ for ($x = 0; $x < $width; ++$x) {
+ if ($image->isImagick()) {
+ try {
+ $colors = $image->image->getImagePixelColor($x, $y)->getColor();
+ } catch (\Throwable $th) {
+ return '';
+ }
+ $row[] = [$colors['r'], $colors['g'], $colors['b']];
+ } else {
+ $index = imagecolorat($image->image, $x, $y);
+ $colors = @imagecolorsforindex($image->image, $index);
+ $row[] = [$colors['red'], $colors['green'], $colors['blue']];
+ }
+ }
+ $pixels[] = $row;
+ }
+
+ // The components define the amount of details (1 to 9).
+ $components_x = 9;
+ $components_y = 9;
+
+ return Blurhash::encode($pixels, $components_x, $components_y);