From 9404a36c66c3a7005955791808f08e087fbc2808 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 5 Dec 2020 00:17:45 -0700 Subject: [PATCH] Add Blurhash util --- app/Util/Media/Blurhash.php | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 app/Util/Media/Blurhash.php diff --git a/app/Util/Media/Blurhash.php b/app/Util/Media/Blurhash.php new file mode 100644 index 000000000..2be054e02 --- /dev/null +++ b/app/Util/Media/Blurhash.php @@ -0,0 +1,47 @@ +mime, ['image/png', 'image/jpeg'])) { + return; + } + + $file = storage_path('app/' . $media->thumbnail_path); + + if(!is_file($file)) { + return; + } + + $image = imagecreatefromstring(file_get_contents($file)); + $width = imagesx($image); + $height = imagesy($image); + + $pixels = []; + for ($y = 0; $y < $height; ++$y) { + $row = []; + for ($x = 0; $x < $width; ++$x) { + $index = imagecolorat($image, $x, $y); + $colors = imagecolorsforindex($image, $index); + + $row[] = [$colors['red'], $colors['green'], $colors['blue']]; + } + $pixels[] = $row; + } + + $components_x = 4; + $components_y = 4; + $blurhash = BlurhashEngine::encode($pixels, $components_x, $components_y); + if(strlen($blurhash) > 191) { + return; + } + return $blurhash; + } + +} \ No newline at end of file