mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-22 14:31:26 +00:00
Update Blurhash util, add default hash for invalid media
This commit is contained in:
parent
8cfe7d79e1
commit
38a37c15af
1 changed files with 13 additions and 4 deletions
|
@ -7,19 +7,28 @@ use App\Media;
|
||||||
|
|
||||||
class Blurhash {
|
class Blurhash {
|
||||||
|
|
||||||
|
const DEFAULT_HASH = 'U4Rfzst8?bt7ogayj[j[~pfQ9Goe%Mj[WBay';
|
||||||
|
|
||||||
public static function generate(Media $media)
|
public static function generate(Media $media)
|
||||||
{
|
{
|
||||||
if(!in_array($media->mime, ['image/png', 'image/jpeg'])) {
|
if(!in_array($media->mime, ['image/png', 'image/jpeg', 'video/mp4'])) {
|
||||||
return;
|
return self::DEFAULT_HASH;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($media->thumbnail_path == null) {
|
||||||
|
return self::DEFAULT_HASH;
|
||||||
}
|
}
|
||||||
|
|
||||||
$file = storage_path('app/' . $media->thumbnail_path);
|
$file = storage_path('app/' . $media->thumbnail_path);
|
||||||
|
|
||||||
if(!is_file($file)) {
|
if(!is_file($file)) {
|
||||||
return;
|
return self::DEFAULT_HASH;
|
||||||
}
|
}
|
||||||
|
|
||||||
$image = imagecreatefromstring(file_get_contents($file));
|
$image = imagecreatefromstring(file_get_contents($file));
|
||||||
|
if(!$image) {
|
||||||
|
return self::DEFAULT_HASH;
|
||||||
|
}
|
||||||
$width = imagesx($image);
|
$width = imagesx($image);
|
||||||
$height = imagesy($image);
|
$height = imagesy($image);
|
||||||
|
|
||||||
|
@ -39,7 +48,7 @@ class Blurhash {
|
||||||
$components_y = 4;
|
$components_y = 4;
|
||||||
$blurhash = BlurhashEngine::encode($pixels, $components_x, $components_y);
|
$blurhash = BlurhashEngine::encode($pixels, $components_x, $components_y);
|
||||||
if(strlen($blurhash) > 191) {
|
if(strlen($blurhash) > 191) {
|
||||||
return;
|
return self::DEFAULT_HASH;
|
||||||
}
|
}
|
||||||
return $blurhash;
|
return $blurhash;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue