Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG output, text is not centered vertically #66

Closed
neoteknic opened this issue Nov 15, 2018 · 8 comments
Closed

SVG output, text is not centered vertically #66

neoteknic opened this issue Nov 15, 2018 · 8 comments

Comments

@neoteknic
Copy link
Contributor

With SVG output, text is not centered vertically.
Chrome : not well centered
Edge : text is on top

Solution : correct default value + add parameters to give an offset X, Y to the text.

version : 2.1.0

@neoteknic
Copy link
Contributor Author

  • suggestion : is it possible to vectorize text instead of output it in SVG ?

@uyab
Copy link
Contributor

uyab commented Dec 18, 2018

Hi @neoteknic, sorry I little bit busy in recent weeks. Any help welcome for this issue :)

@neoteknic
Copy link
Contributor Author

image
Maybe a font issue, look at this image on Google chrome windows 10 x64.

@neoteknic
Copy link
Contributor Author

image

Chrome ; Firefox ; Edge
Browser issue ? but possible to fire in Avatar.php toSvg() maybe.

@fannyfan414
Copy link

I extend Avatar class and in method toSvg add 4px to "y" coordinate.
This line

$svg .= '<text x="' . $center . '" y="' . ($center + 4);

With this modification content is now aligned correctly

<?php
/**
 * Created by PhpStorm.
 * User: Workstation
 * Date: 11.09.2019
 * Time: 23:21
 */

namespace App\Services\AvatarGenerator;


use Laravolt\Avatar\Avatar;

class AvatarGenerator extends Avatar
{
    public function toSvg()
    {
        $this->buildInitial();

        $x = $y = $this->borderSize / 2;
        $width = $height = $this->width - $this->borderSize;
        $radius = ($this->width - $this->borderSize) / 2;
        $center = $this->width / 2;

        $svg = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" width="' . $this->width . '" height="' . $this->height . '">';

        if ($this->shape == 'square') {
            $svg .= '<rect x="' . $x
                . '" y="' . $y
                . '" width="' . $width . '" height="' . $height
                . '" stroke="' . $this->borderColor
                . '" stroke-width="' . $this->borderSize
                . '" fill="' . $this->background . '" />';
        } elseif ($this->shape == 'circle') {
            $svg .= '<circle cx="' . $center
                . '" cy="' . $center
                . '" r="' . $radius
                . '" stroke="' . $this->borderColor
                . '" stroke-width="' . $this->borderSize
                . '" fill="' . $this->background . '" />';
        }

        $svg .= '<text x="' . $center . '" y="' . ($center + 4);
        $svg .= '" font-size="' . $this->fontSize;

        if ($this->fontFamily) {
            $svg .= '" font-family="' . $this->fontFamily;
        }

        $svg .= '" fill="' . $this->foreground . '" alignment-baseline="middle" text-anchor="middle" dominant-baseline="central">';
        $svg .= $this->getInitial();
        $svg .= '</text>';

        $svg .= '</svg>';

        return $svg;
    }

}

@uyab
Copy link
Contributor

uyab commented May 28, 2020

Just found https://github.com/meyfa/php-svg, will try to refactor the svg part using it.

@danpalmieri
Copy link

Poor (but fast 😶) workaround:
{!! Str::replaceFirst(' y="50"', ' y="54"', Avatar::create('Susilo Bambang Yudhoyono')->toSvg()) !!}

@uyab
Copy link
Contributor

uyab commented Jun 16, 2021

Fixed in 4.1.4

@uyab uyab closed this as completed Jun 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants