Imagemagick - 图像中文本周围的奇怪片段

Imagemagick - Strange fragments around text in image

我正在使用 Imagemagick 动态生成 OpenGraph 图像。
我正在逐步处理该图像,意思是:

  1. 将图片调整为 1200 像素宽度
  2. 对图像进行灰度化
  3. 使图像透明
  4. 将透明图像与滤镜颜色相乘

创建背景图片后,我开始添加元素。

  1. 添加主线
  2. -> 8. 添加一些其他元素

不幸的是,在生成该图像后,文本和我覆盖的图像周围出现了一些奇怪的碎片。

知道如何避免这种情况吗?

我附上了一些图片来演示这个问题。

编辑:
希望对您有所帮助

Link to SRC Image

$imagename = uniqid() . ".jpg";
$image = $og_upload_path . '/' . $imagename;

$alpha = 15
$filterColor = "#08e2dd"; // Light Blue

// Asset images for processing the opengraph image
$white_background = $image_path . "/white.png";

$src_image = [Link to src image see above];

// Resize Image to 1200px width
    system("convert '" . $src_image . "' -resize 1200 '" . $image . "'");

    // Grayscale Image
    system("convert '" . $image . "' -colorspace Gray '" . $image . "'");

    // Make image transparent
    system("composite -blend " . $alpha . " '" . $image . "' '" . $white_background . "' '" . $image . "'");

    // Multiply Image with filter color
    system("convert '" . $image . "' \ "
      . " \( -clone 0 -fill '" . $filterColor . "' -colorize 100 \) \ "
      . " -compose multiply \ "
      . " -composite '" . $image . "'"
    );

    // Add MainLine Text
    system("convert '" . $image . "' -size 1000x -background transparent \ "
      . " -fill '" . $textColor_MainLine . "' \ "
      . " -pointsize " . $fontSize_MainLine . " \ "
      . " -font " . $path_fontfile . " \ "
      . " -gravity center caption:" . $text_MainLine . " \ "
      . " -gravity center -composite '" . $image . "'"
    );

JPG 是一种有损图像格式,ImageMagick 的 convert 实用程序 doesn't retain the highest quality by default:

For the JPEG and MPEG image formats, quality is 1 (lowest image quality and highest compression) to 100 (best quality but least effective compression). The default is to use the estimated quality of your input image if it can be determined, otherwise 92. When the quality is greater than 90, then the chroma channels are not downsampled. Use the -sampling-factor option to specify the factors for chroma downsampling.

您可以在命令中设置 -quality 100,但最好以无损图像格式处理图像,然后转换为有损格式(如 JPG)以获得更小的文件大小。

尽可能长时间保持 PNG 格式,只在最后阶段进入 JPG。还。尽可能减少对 convert 的调用,以避免解压缩和重新压缩。因此,您可以像这样一步完成对 convert 的前 4 次调用:

convert https://s3.amazonaws.com/f.cl.ly/items/0E0V2M29313B3b0p1x2W/og_image.png \
   -resize 1200                                                                  \
   -colorspace gray                                                              \
   -evaluate multiply 0.15 -evaluate add 55700                                   \
   \( +clone -fill "#08e2dd" -colorize 100 \) -compose multiply -composite       \
   output.png

我重写了你的 composite 命令,将 85% 的白色混合为乘以 0.15 并添加 65,535(即 55700)的 85%,这是 16 位量化中的白色。这样你就不必无缘无故地保留一个 white.png 文件(因为我们知道所有像素无论如何都是 65535)并且允许我们用 convert 做所有事情而不是必须保存和使用 composite 并保存并恢复为 convert

我会让你检查质量和处理时间,然后自己添加最后一步。

感谢大家的快速回复。

最后Facebook的压缩确实是问题所在。如果你手动上传它,它会变得模糊而且非常讨厌,但如果你只是将它用作 OpenGraph 图像,那就太棒了。 回答和评论对我帮助很大!