mirror of
https://github.com/friendica/friendica
synced 2025-04-27 03:50:11 +00:00
Add more BlurHash to avoid not being able to display some picture
This commit is contained in:
parent
183e9dc7b2
commit
bf7df13855
9 changed files with 101 additions and 28 deletions
|
@ -34,11 +34,15 @@ use Friendica\Core\Worker;
|
|||
use Friendica\Database\Database;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\DI;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientAccept;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
||||
use Friendica\Network\HTTPException;
|
||||
use Friendica\Network\Probe;
|
||||
use Friendica\Object\Image;
|
||||
use Friendica\Protocol\Activity;
|
||||
use Friendica\Protocol\ActivityPub;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use Friendica\Util\HTTPSignature;
|
||||
use Friendica\Util\Images;
|
||||
use Friendica\Util\Network;
|
||||
use Friendica\Util\Proxy;
|
||||
|
@ -2193,7 +2197,7 @@ class Contact
|
|||
*/
|
||||
public static function updateAvatar(int $cid, string $avatar, bool $force = false, bool $create_cache = false)
|
||||
{
|
||||
$contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'xmpp', 'addr', 'nurl', 'url', 'network', 'uri-id'],
|
||||
$contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'blurhash', 'xmpp', 'addr', 'nurl', 'url', 'network', 'uri-id'],
|
||||
['id' => $cid, 'self' => false]);
|
||||
if (!DBA::isResult($contact)) {
|
||||
return;
|
||||
|
@ -2203,8 +2207,19 @@ class Contact
|
|||
|
||||
// Only update the cached photo links of public contacts when they already are cached
|
||||
if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro']) && !$create_cache) {
|
||||
if ($contact['avatar'] != $avatar) {
|
||||
self::update(['avatar' => $avatar], ['id' => $cid]);
|
||||
if (($contact['avatar'] != $avatar) || empty($contact['blurhash'])) {
|
||||
$update_fields = ['avatar' => $avatar];
|
||||
$fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
|
||||
|
||||
$img_str = $fetchResult->getBody();
|
||||
if (!empty($img_str)) {
|
||||
$image = new Image($img_str, Images::getMimeTypeByData($img_str));
|
||||
if ($image->isValid()) {
|
||||
$update_fields['blurhash'] = $image->getBlurHash();
|
||||
}
|
||||
}
|
||||
|
||||
self::update($update_fields, ['id' => $cid]);
|
||||
Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]);
|
||||
}
|
||||
return;
|
||||
|
@ -2275,7 +2290,7 @@ class Contact
|
|||
if ($update) {
|
||||
$photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
|
||||
if ($photos) {
|
||||
$fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()];
|
||||
$fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'blurhash' => $photos[3], 'avatar-date' => DateTimeFormat::utcNow()];
|
||||
$update = !empty($fields);
|
||||
Logger::debug('Created new cached avatars', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
|
||||
} else {
|
||||
|
|
|
@ -313,6 +313,28 @@ class Photo
|
|||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a photo array for a given image data string
|
||||
*
|
||||
* @param string $image_data Image data
|
||||
* @param string $mimetype Image mime type. Is guessed by file name when empty.
|
||||
*
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function createPhotoForImageData(string $image_data, string $mimetype = ''): array
|
||||
{
|
||||
$fields = self::getFields();
|
||||
$values = array_fill(0, count($fields), '');
|
||||
|
||||
$photo = array_combine($fields, $values);
|
||||
$photo['data'] = $image_data;
|
||||
$photo['type'] = $mimetype ?: Images::getMimeTypeByData($image_data);
|
||||
$photo['cacheable'] = false;
|
||||
|
||||
return $photo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a photo array for a system resource image
|
||||
*
|
||||
|
@ -647,7 +669,11 @@ class Photo
|
|||
$micro = Contact::getDefaultAvatar($contact, Proxy::SIZE_MICRO);
|
||||
}
|
||||
|
||||
return [$image_url, $thumb, $micro];
|
||||
$photo = DBA::selectFirst(
|
||||
'photo', ['blurhash'], ['uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'photo-type' => self::CONTACT_AVATAR]
|
||||
);
|
||||
|
||||
return [$image_url, $thumb, $micro, $photo['blurhash']];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -28,7 +28,10 @@ use Friendica\Database\DBA;
|
|||
use Friendica\DI;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientAccept;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
||||
use Friendica\Util\HTTPSignature;
|
||||
use Friendica\Util\Images;
|
||||
use Friendica\Util\Proxy;
|
||||
use Friendica\Object\Image;
|
||||
|
||||
/**
|
||||
* Class Link
|
||||
|
@ -72,12 +75,14 @@ class Link
|
|||
if (!empty($link['id'])) {
|
||||
$id = $link['id'];
|
||||
Logger::info('Found', ['id' => $id, 'uri-id' => $uriId, 'url' => $url]);
|
||||
} else {
|
||||
$mime = self::fetchMimeType($url);
|
||||
} else {
|
||||
$fields = self::fetchMimeType($url);
|
||||
$fields['uri-id'] = $uriId;
|
||||
$fields['url'] = $url;
|
||||
|
||||
DBA::insert('post-link', ['uri-id' => $uriId, 'url' => $url, 'mimetype' => $mime], Database::INSERT_IGNORE);
|
||||
DBA::insert('post-link', $fields, Database::INSERT_IGNORE);
|
||||
$id = DBA::lastInsertId();
|
||||
Logger::info('Inserted', ['id' => $id, 'uri-id' => $uriId, 'url' => $url]);
|
||||
Logger::info('Inserted', $fields);
|
||||
}
|
||||
|
||||
if (empty($id)) {
|
||||
|
@ -114,19 +119,28 @@ class Link
|
|||
*
|
||||
* @param string $url URL to fetch
|
||||
* @param string $accept Comma-separated list of expected response MIME type(s)
|
||||
* @return string Discovered MIME type or empty string on failure
|
||||
* @return array Discovered MIME type and blurhash or empty array on failure
|
||||
*/
|
||||
private static function fetchMimeType(string $url, string $accept = HttpClientAccept::DEFAULT): string
|
||||
private static function fetchMimeType(string $url, string $accept = HttpClientAccept::DEFAULT): array
|
||||
{
|
||||
$timeout = DI::config()->get('system', 'xrd_timeout');
|
||||
|
||||
$curlResult = DI::httpClient()->head($url, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]);
|
||||
$curlResult = HTTPSignature::fetchRaw($url, 0, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]);
|
||||
if (!$curlResult->isSuccess()) {
|
||||
return [];
|
||||
}
|
||||
$fields = ['mimetype' => $curlResult->getHeader('Content-Type')[0]];
|
||||
|
||||
if ($curlResult->isSuccess() && empty($media['mimetype'])) {
|
||||
return $curlResult->getHeader('Content-Type')[0] ?? '';
|
||||
$img_str = $curlResult->getBody();
|
||||
$image = new Image($img_str, Images::getMimeTypeByData($img_str));
|
||||
if ($image->isValid()) {
|
||||
$fields['mimetype'] = $image->getType();
|
||||
$fields['width'] = $image->getWidth();
|
||||
$fields['height'] = $image->getHeight();
|
||||
$fields['blurhash'] = $image->getBlurHash();
|
||||
}
|
||||
|
||||
return '';
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue