+ $mimetext = '';
+ if (!empty($url)) {
+ $mime = ParseUrl::getContentType($url, HttpClientAccept::IMAGE);
+ if (!empty($mime)) {
+ $mimetext = $mime[0] . '/' . $mime[1];
+ } else {
+ // Only update federated accounts that hadn't failed before and hadn't been updated recently
+ $update = in_array($contact['network'], Protocol::FEDERATED) && !$contact['failed']
+ && ((time() - strtotime($contact['updated']) > 86400));
+ if ($update) {
+ $curlResult = DI::httpClient()->head($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::IMAGE]);
+ $update = !$curlResult->isSuccess() && ($curlResult->getReturnCode() == 404);
+ Logger::debug('Got return code for avatar', ['return code' => $curlResult->getReturnCode(), 'cid' => $id, 'url' => $contact['url'], 'avatar' => $url]);
+ }
+ if ($update) {
+ Logger::info('Invalid file, contact update initiated', ['cid' => $id, 'url' => $contact['url'], 'avatar' => $url]);
+ Worker::add(Worker::PRIORITY_LOW, 'UpdateContact', $id);
+ } else {
+ Logger::info('Invalid file', ['cid' => $id, 'url' => $contact['url'], 'avatar' => $url]);
+ }
+ }
+ if (!empty($mimetext) && ($mime[0] != 'image') && ($mimetext != 'application/octet-stream')) {
+ Logger::info('Unexpected Content-Type', ['mime' => $mimetext, 'url' => $url]);
+ $mimetext = '';
+ } if (!empty($mimetext)) {
+ Logger::debug('Expected Content-Type', ['mime' => $mimetext, 'url' => $url]);
+ }
+ }
+ if (empty($mimetext) && !empty($contact['blurhash'])) {
+ $image = New Image('', 'image/png');
+ $image->getFromBlurHash($contact['blurhash'], $customsize, $customsize);
+ return MPhoto::createPhotoForImageData($image->asString());
+ } elseif (empty($mimetext)) {
+ if ($customsize <= Proxy::PIXEL_MICRO) {
+ $url = Contact::getDefaultAvatar($contact ?: [], Proxy::SIZE_MICRO);
+ } elseif ($customsize <= Proxy::PIXEL_THUMB) {
+ $url = Contact::getDefaultAvatar($contact ?: [], Proxy::SIZE_THUMB);
+ } else {
+ $url = Contact::getDefaultAvatar($contact ?: [], Proxy::SIZE_SMALL);
+ }
+ }
+ return MPhoto::createPhotoForExternalResource($url, 0, $mimetext, $contact['blurhash'], $customsize, $customsize);
+ case 'header':
+ $fields = ['uid', 'url', 'header', 'network', 'gsid'];
+ $contact = Contact::getById($id, $fields);