用文本覆盖图像并转换为图像 [英] Overlay image with text and convert to image

查看:128
本文介绍了用文本覆盖图像并转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文本添加到jpg中以创建新图像.

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

我知道您可以在php中使用GD Libraries栅格化副本,但是您可以对其进行分层吗?我的网站是用PHP编写的,但是我愿意使用第三方插件.

答案:(旧帖子),但我需要解决方案

使用GD和Freetype2(如果已安装),则可以使用以下步骤将文本添加到JPEG.

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

  2. 使用Freetype2库通过功能 imagefttext() (请注意,您还可以使用 imagettftext() (如果您仅安装了Freetype而没有安装Freetype2).

  3. 使用 imagejpeg() <保存修改的图像/p>

示例:

[我从字面上看是在浏览器中键入此内容,从不运行它-因此,如果需要修改,我们深表歉意.]

/**
 * 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);
}

NB.为了进行调试,我将删除@符号.

I want to add text to a jpg creating a new image.

There will be image_1.jpg already on the server and I want to take the user submitted copy and put it on top of image_1.jpg creating a new image that combines the copy and the original image into a new rasterized jpg

I know you can use GD Libraries in php to rasterize copy but can you layer it? My site is written in PHP but I am open to using 3rd party plug-ins.

ANSWER:(OLD POST) but what I need http://blog.rafaeldohms.com.br/2008/02/12/adding-text-to-images-in-real-time-with-php/

解决方案

Using GD and Freetype2, if both installed, then you can add text to a JPEG using the following steps.

  1. create an image resource from the file using imagecreatefromjpeg()

  2. add text to that image using the Freetype2 library, via the function imagefttext() (note you can also use the function imagettftext() if you only have Freetype installed and not Freetype2).

  3. save the modified image using imagejpeg()

Example:

[I've literally just typed this in to the browser, never run it - so if it needs amendment, apologies.]

/**
 * 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);
}

NB. For debugging, I would remove the @ symbols.

这篇关于用文本覆盖图像并转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆