【问题标题】:Add svg path on image using vips使用 vips 在图像上添加 svg 路径
【发布时间】:2020-09-16 09:26:56
【问题描述】:

我正在使用 vips PHP 库。我想在我的图像上添加 svg 路径。我为此实现了下面的代码,但它没有给我预期的输出。问题是如何根据实际图像高度和宽度设置 svg 路径。我尝试使用调整大小和缩略图,但它没有给我预期的输出。我还想在我的实际图像块上填充灰色,就像预期的输出图像一样。

使用 imagick 生成的预期输出图像

$background = Vips\Image::newFromFile($arg[1], ['access' => 'sequential']);
$svg = Vips\Image::svgload_buffer('<svg>
  <path 
    d="M0 200 v-200 h200 
    a100,100 90 0,1 0,200
    a100,100 90 0,1 -200,0
    z" stroke="#fff" fill="transparent"/>
</svg>');
// $svg = $svg->resize(2.5);
$svg = $svg->thumbnail_image(700, ['height' => 700, 'size' => 'both']);
$image = $background->composite($svg, 'dest-in');
$image->writeToFile([$arg2], ['background' => [128, 128, 128], 'Q' => 100]);

下面是我添加 svg 路径的图片

我的 vips 输出图片

预期的输出图像

【问题讨论】:

    标签: svg vips


    【解决方案1】:

    试试:

    #!/usr/bin/php
    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    use Jcupitt\Vips;
    
    $background = Vips\Image::newFromFile($argv[1], ['access' => 'sequential']);
    $width = $background->width;
    $height = $background->height;
    $svg = Vips\Image::svgload_buffer(<<<EOF
      <svg
        viewBox="0 0 300 300" 
        width="$width"
        height="$height">
        <path 
        d="M0 200 v-200 h200 
          a100,100 90 0,1 0,200
          a100,100 90 0,1 -200,0
          z" stroke="#fff" fill="white"/>
      </svg>
    EOF);
    $image = $background->colourspace('b-w')->composite($svg, 'dest-in');
    $image->writeToFile($argv[2], ['background' => [128, 128, 128], 'Q' => 100]);
    

    相关 github 问题:https://github.com/libvips/php-vips/issues/109

    【讨论】:

    • 我在背景图像上添加了颜色空间'b-w',但它给了我在上面输出中提到的相同结果。此外,当我应用 viewBox="0 0 700 700" 时,svg 没有变化。我想根据图像的高度和宽度调整 svg 的高度和宽度
    猜你喜欢
    • 2018-05-13
    • 2012-03-29
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    相关资源
    最近更新 更多