Use a svg file as default user avatar instead of SVG. Also changed path generation logic

This should also fix the path issue described in issue #446
This commit is contained in:
Jan Böhmer 2023-12-10 21:25:40 +01:00
parent e2437d4c33
commit bcaf96ed59
3 changed files with 19 additions and 21 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

View file

@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg width="100%" height="100%" viewBox="0 0 20 20" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;"><g id="Ebene1"><rect x="0.021" y="-0.023" width="19.977" height="20" style="fill:#95bbdf;"/></g><path d="M10,10c2.194,0 4,-1.806 4,-4c0,-2.194 -1.806,-4 -4,-4c-2.194,0 -4,1.806 -4,4c0,2.194 1.806,4 4,4Zm-1.428,1.5c-3.078,0 -5.572,2.494 -5.572,5.572c0,0.512 0.416,0.928 0.928,0.928l12.144,0c0.512,0 0.928,-0.416 0.928,-0.928c0,-3.078 -2.494,-5.572 -5.572,-5.572l-2.856,0Z" style="fill:#fff;fill-rule:nonzero;"/></svg>

After

Width:  |  Height:  |  Size: 856 B

View file

@ -20,9 +20,9 @@ declare(strict_types=1);
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
namespace App\Services\UserSystem; namespace App\Services\UserSystem;
use Imagine\Exception\RuntimeException;
use App\Entity\Attachments\Attachment; use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\AttachmentType; use App\Entity\Attachments\AttachmentType;
use App\Entity\Attachments\UserAttachment; use App\Entity\Attachments\UserAttachment;
@ -30,16 +30,23 @@ use App\Entity\UserSystem\User;
use App\Services\Attachments\AttachmentSubmitHandler; use App\Services\Attachments\AttachmentSubmitHandler;
use App\Services\Attachments\AttachmentURLGenerator; use App\Services\Attachments\AttachmentURLGenerator;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Liip\ImagineBundle\Service\FilterService;
use Symfony\Component\Asset\Packages; use Symfony\Component\Asset\Packages;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
class UserAvatarHelper class UserAvatarHelper
{ {
public const IMG_DEFAULT_AVATAR_PATH = '/img/default_avatar.png'; /**
* Path to the default avatar image (must not start with a slash, or the asset package will not work)
*/
public const IMG_DEFAULT_AVATAR_PATH = 'img/default_avatar.svg';
public function __construct(private readonly bool $use_gravatar, private readonly Packages $packages, private readonly AttachmentURLGenerator $attachmentURLGenerator, private readonly FilterService $filterService, private readonly EntityManagerInterface $entityManager, private readonly AttachmentSubmitHandler $submitHandler) public function __construct(
{ private readonly bool $use_gravatar,
private readonly Packages $packages,
private readonly AttachmentURLGenerator $attachmentURLGenerator,
private readonly EntityManagerInterface $entityManager,
private readonly AttachmentSubmitHandler $submitHandler
) {
} }
@ -78,13 +85,8 @@ class UserAvatarHelper
return $this->getGravatar($user, 50); //50px wide picture return $this->getGravatar($user, 50); //50px wide picture
} }
try { //Otherwise serve the default image (its an SVG, so we dont need to thumbnail it)
//Otherwise we can serve the relative path via Asset component return $this->packages->getUrl(self::IMG_DEFAULT_AVATAR_PATH);
return $this->filterService->getUrlOfFilteredImage(self::IMG_DEFAULT_AVATAR_PATH, 'thumbnail_xs');
} catch (RuntimeException) {
//If the filter fails, we can not serve the thumbnail and fall back to the original image and log an warning
return $this->packages->getUrl(self::IMG_DEFAULT_AVATAR_PATH);
}
} }
public function getAvatarMdURL(User $user): string public function getAvatarMdURL(User $user): string
@ -100,20 +102,15 @@ class UserAvatarHelper
return $this->getGravatar($user, 150); return $this->getGravatar($user, 150);
} }
try { //Otherwise serve the default image (its an SVG, so we dont need to thumbnail it)
//Otherwise we can serve the relative path via Asset component return $this->packages->getUrl(self::IMG_DEFAULT_AVATAR_PATH);
return $this->filterService->getUrlOfFilteredImage(self::IMG_DEFAULT_AVATAR_PATH, 'thumbnail_xs');
} catch (RuntimeException) {
//If the filter fails, we can not serve the thumbnail and fall back to the original image and log an warning
return $this->packages->getUrl(self::IMG_DEFAULT_AVATAR_PATH);
}
} }
/** /**
* Get either a Gravatar URL or complete image tag for a specified email address. * Get either a Gravatar URL or complete image tag for a specified email address.
* *
* @param User $user The user for which the gravator should be generated * @param User $user The user for which the gravator should be generated
* @param int $s Size in pixels, defaults to 80px [ 1 - 2048 ] * @param int $s Size in pixels, defaults to 80px [ 1 - 2048 ]
* @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ] * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
@ -131,7 +128,7 @@ class UserAvatarHelper
$url = 'https://www.gravatar.com/avatar/'; $url = 'https://www.gravatar.com/avatar/';
$url .= md5(strtolower(trim($email))); $url .= md5(strtolower(trim($email)));
return $url . "?s=$s&d=$d&r=$r"; return $url."?s=$s&d=$d&r=$r";
} }
/** /**