【问题标题】:Overlay image with text and convert to image用文本覆盖图像并转换为图像
【发布时间】:2011-09-27 13:24:31
【问题描述】:

我想将文本添加到 jpg 创建新图像。

服务器上已经有 image_1.jpg,我想获取用户提交的副本并将其放在 image_1.jpg 之上创建一个新图像,将副本和原始图像组合成一个新的光栅化 jpg

我知道你可以在php 中使用GD Libraries 来光栅化副本,但你可以分层吗?我的网站是用 PHP 编写的,但我愿意使用 3rd 方插件。

回答:(旧帖子)但我需要http://blog.rafaeldohms.com.br/2008/02/12/adding-text-to-images-in-real-time-with-php/

【问题讨论】:

  • 如果分层,您期望输出什么格式?游戏机? XCF?米夫?很确定 ImageMagick 可以处理图层,以前从未在 PHP 中做过。
  • 如果可能,应将最终文件光栅化并保存为可以通过电子邮件发送的任何文件格式。 jpg gif png
  • 图层?你在哪里需要一个层?不只是将文本放在图像上吗?在 GD 中什么还不足以完成这项工作?到目前为止,您的实际代码是什么?在我看来根本没有问题,请参阅roseindia.net/tutorial/php/phpgd/About-drawtext.html - 是的:stackoverflow.com/…
  • 没有代码,只是想确保它是可能的。服务器上已经有 image_1.jpg,我想将用户提交的副本放在 image_1.jpg 之上,创建一个新图像,将副本和原始图像组合成一个新的光栅化 jpg。
  • 应该只是使用imagecreatefromjpeg()读取图像,使用imagefttext()在图像上绘制文本,然后使用imagejpeg()写出新文件的情况。

标签: php text gd


【解决方案1】:

使用 GD 和 Freetype2,如果两者都安装了,那么您可以使用以下步骤将文本添加到 JPEG。

  1. 使用imagecreatefromjpeg()从文件创建图像资源

  2. 使用 Freetype2 库通过函数 imagefttext() 向该图像添加文本(请注意,如果您只安装了 Freetype 而未安装 Freetype2,您也可以使用函数 imagettftext())。

    李>
  3. 使用imagejpeg()保存修改后的图像

示例:

[我只是在浏览器中输入了这个,从不运行它——所以如果它需要修改,道歉。]

/**
 * Annotate an image with text using the GD2 and Freetype2 libraries
 *
 * @author Orbling@StackOverflow
 *
 * @param string $sourceFileName Source image path
 * @param string $destinationFileName Destination image path
 * @param string $text Text to use for annotation
 * @param string $font Font definition file path
 * @param float $fontSize Point size of text
 * @param array $fontColour Font colour definition, expects
                            array('r' => #, 'g' => #, 'b' => #),
                            defaults to black
 * @param int $x x-coordinate of text annotation
 * @param int $y y-coordinate of text annotation
 * @param float $rotation Angle of rotation for text annotation,
                          in degrees, anticlockwise from left-to-right
 * @param int $outputQuality JPEG quality for output image
 *
 * @return bool Success status 
 */
function imageannotate($sourceFileName, $destinationFileName,
                       $text, $font, $fontSize, array $fontColour = NULL,
                       $x, $y, $rotation = 0, $outputQuality = 90) {
    $image = @imagecreatefromjpeg($sourceFileName);

    if ($image === false) {
        return false;
    }

    if (is_array($fontColour) && array_key_exists('r', $fontColour)
                              && array_key_exists('g', $fontColour)
                              && array_key_exists('b', $fontColour)) {
        $colour = imagecolorallocate($image, $fontColour['r'],
                                             $fontColour['g'],
                                             $fontColour['b']);

        if ($colour === false) {
            return false;
        }
    } else {
        $colour = @imagecolorallocate($image, 0, 0, 0);
    }

    if (@imagefttext($image, $fontSize, $rotation,
                     $x, $y, $colour, $font, $text) === false) {
        return false;
    }

    return @imagejpeg($image, $destinationFileName, $outputQuality);
}

注意。为了调试,我会删除@ 符号。

【讨论】:

  • 我没有把我的变成一个函数。很有帮助的答案。谢谢+1
  • @Denoteone:很好。请注意,如果您将NULL 传递给$destinationFileName,那么由于imagejpeg() 的工作方式,它会将图像输出到stdout。因此,如果您想编写一个返回图像的脚本,只需为 JPEG 输出适当的内容类型标头,然后输出文件。
  • 哇,你的代码加上这个 (stackoverflow.com/a/1558771/470749) 今晚对我很有帮助。谢谢!
猜你喜欢
  • 2020-08-24
  • 1970-01-01
  • 2020-12-13
  • 2020-05-30
  • 1970-01-01
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 2021-06-08
相关资源
最近更新 更多